-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
[Frontend] Improve error message in tool_choice validation #19239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
057de82
18b15bd
4d93bdd
82acd45
4777d07
ece4d39
569104c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -700,20 +700,16 @@ def check_tool_usage(cls, data): | |
# it matches a valid tool | ||
if isinstance(data["tool_choice"], dict): | ||
valid_tool = False | ||
specified_function = data["tool_choice"].get("function") | ||
if not specified_function: | ||
function = data["tool_choice"].get("function") | ||
if not isinstance(function, dict) or ( | ||
(function_name := function.get("name")) | ||
and not isinstance(function_name, str)): | ||
raise ValueError( | ||
"Expected field `function` in `tool_choice`." | ||
" Correct usage: `{\"type\": \"function\"," | ||
" \"function\": {\"name\": \"my_function\"}}`") | ||
specified_function_name = specified_function.get("name") | ||
if not specified_function_name: | ||
raise ValueError( | ||
"Expected field `name` in `function` in `tool_choice`." | ||
"Correct usage: `{\"type\": \"function\", " | ||
"\"function\": {\"name\": \"my_function\"}}`") | ||
f'Invalid value for `function`: `{function}` in ' | ||
'`tool_choice`! Correct usage: `{"type": "function",' | ||
' "function": {"name": "my_function"}}`') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This condition checks if Also, consider adding a check to ensure that the function_name_valid = isinstance(function, dict) and \
isinstance(function.get("name"), str)
if not function_name_valid:
raise ValueError(
f'Invalid value for `function`: `{function}` in '
'`tool_choice`! Correct usage: `{"type": "function",'
' "function": {"name": "my_function"}}`') |
||
for tool in data["tools"]: | ||
if tool["function"]["name"] == specified_function_name: | ||
if tool["function"]["name"] == function_name: | ||
valid_tool = True | ||
break | ||
if not valid_tool: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There two types of error: 1) function is not dict, 2) function_name is not str, can distinguish them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not seem to add more value to show different error messages to users for these two cases, as the key is to show the correct usage to users. Feels original code is duplicated so consolidating them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same sentiment with Lu Fang here. I think we should be very explicit about the error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aarnphm ok updated, thanks for review