From 7e3e74c97c9ba8a30b05063af42035c58c2501e8 Mon Sep 17 00:00:00 2001 From: 22quinn <33176974+22quinn@users.noreply.github.com> Date: Wed, 11 Jun 2025 23:13:00 -0600 Subject: [PATCH] [Frontend] Improve error message in tool_choice validation (#19239) Signed-off-by: 22quinn <33176974+22quinn@users.noreply.github.com> --- vllm/entrypoints/openai/protocol.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/vllm/entrypoints/openai/protocol.py b/vllm/entrypoints/openai/protocol.py index 79f0f200c74ed..4dad41bc4edb5 100644 --- a/vllm/entrypoints/openai/protocol.py +++ b/vllm/entrypoints/openai/protocol.py @@ -700,22 +700,26 @@ class ChatCompletionRequest(OpenAIBaseModel): # ensure that if "tool_choice" is specified as an object, # it matches a valid tool + correct_usage_message = 'Correct usage: `{"type": "function",' \ + ' "function": {"name": "my_function"}}`' 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): 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: + f"Invalid value for `function`: `{function}` in " + f"`tool_choice`! {correct_usage_message}") + if "name" not in function: + raise ValueError(f"Expected field `name` in `function` in " + f"`tool_choice`! {correct_usage_message}") + function_name = function["name"] + if not isinstance(function_name, + str) or len(function_name) == 0: raise ValueError( - "Expected field `name` in `function` in `tool_choice`." - "Correct usage: `{\"type\": \"function\", " - "\"function\": {\"name\": \"my_function\"}}`") + f"Invalid `name` in `function`: `{function_name}`" + f" in `tool_choice`! {correct_usage_message}") 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: