A high-throughput and memory-efficient inference and serving engine for LLMs
Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git
Synced Nov 15, 06:00 UTC
[Frontend] Improve error message in tool_choice validation (#19239)
Signed-off-by: 22quinn <33176974+22quinn@users.noreply.github.com>
22quinn committed
Jun 11, 2025 at 23:13 UTC
7e3e74c97c9ba8a30b05063af42035c58c2501e8
1 file changed
+15
-11
vllm/entrypoints/openai/protocol.py
+15
-11
@@ -700,22 +700,26 @@ class ChatCompletionRequest(OpenAIBaseModel):
700
701
# ensure that if "tool_choice" is specified as an object,
702
# it matches a valid tool
703
+ correct_usage_message = 'Correct usage: `{"type": "function",' \
704
+ ' "function": {"name": "my_function"}}`'
705
if isinstance(data["tool_choice"], dict):
706
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):
709
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:
718
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}")
721
for tool in data["tools"]:
718
- if tool["function"]["name"] == specified_function_name:
722
+ if tool["function"]["name"] == function_name:
723
valid_tool = True
724
break
725
if not valid_tool: