Skip to content

Commit 7e3e74c

Browse files
authored
[Frontend] Improve error message in tool_choice validation (#19239)
Signed-off-by: 22quinn <33176974+22quinn@users.noreply.github.com>
1 parent 3f6341b commit 7e3e74c

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

vllm/entrypoints/openai/protocol.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -700,22 +700,26 @@ def check_tool_usage(cls, data):
700700

701701
# ensure that if "tool_choice" is specified as an object,
702702
# it matches a valid tool
703+
correct_usage_message = 'Correct usage: `{"type": "function",' \
704+
' "function": {"name": "my_function"}}`'
703705
if isinstance(data["tool_choice"], dict):
704706
valid_tool = False
705-
specified_function = data["tool_choice"].get("function")
706-
if not specified_function:
707+
function = data["tool_choice"].get("function")
708+
if not isinstance(function, dict):
707709
raise ValueError(
708-
"Expected field `function` in `tool_choice`."
709-
" Correct usage: `{\"type\": \"function\","
710-
" \"function\": {\"name\": \"my_function\"}}`")
711-
specified_function_name = specified_function.get("name")
712-
if not specified_function_name:
710+
f"Invalid value for `function`: `{function}` in "
711+
f"`tool_choice`! {correct_usage_message}")
712+
if "name" not in function:
713+
raise ValueError(f"Expected field `name` in `function` in "
714+
f"`tool_choice`! {correct_usage_message}")
715+
function_name = function["name"]
716+
if not isinstance(function_name,
717+
str) or len(function_name) == 0:
713718
raise ValueError(
714-
"Expected field `name` in `function` in `tool_choice`."
715-
"Correct usage: `{\"type\": \"function\", "
716-
"\"function\": {\"name\": \"my_function\"}}`")
719+
f"Invalid `name` in `function`: `{function_name}`"
720+
f" in `tool_choice`! {correct_usage_message}")
717721
for tool in data["tools"]:
718-
if tool["function"]["name"] == specified_function_name:
722+
if tool["function"]["name"] == function_name:
719723
valid_tool = True
720724
break
721725
if not valid_tool:

0 commit comments

Comments
 (0)