diff --git a/tests/tool_use/test_tool_choice_required.py b/tests/tool_use/test_tool_choice_required.py index aaa499da16..a859659417 100644 --- a/tests/tool_use/test_tool_choice_required.py +++ b/tests/tool_use/test_tool_choice_required.py @@ -5,7 +5,7 @@ from copy import deepcopy import pytest import regex as re -from openai.types.responses import FunctionTool, WebSearchTool +from openai.types.responses import FunctionTool, ToolChoiceFunction, WebSearchTool from pydantic import TypeAdapter from vllm.entrypoints.openai.chat_completion.protocol import ( @@ -456,3 +456,27 @@ class TestParallelToolCallsConstraint: ) assert isinstance(schema, dict) assert "maxItems" not in schema + + +class TestForcedNamedToolChoiceEmptyParams: + """A forced named tool_choice with missing/empty parameters must still + constrain the generated arguments to a JSON object, like the + `tool_choice="required"` path, instead of leaving them unconstrained.""" + + @pytest.mark.parametrize("params", [None, {}]) + def test_chat_empty_params_constrains_object(self, params): + tool = ChatCompletionToolsParam.model_validate( + {"type": "function", "function": {"name": "ping", "parameters": params}} + ) + choice = ChatCompletionNamedToolChoiceParam.model_validate( + {"type": "function", "function": {"name": "ping"}} + ) + schema = get_json_schema_from_tools(choice, [tool]) + assert schema == {"type": "object", "properties": {}} + + @pytest.mark.parametrize("params", [None, {}]) + def test_responses_empty_params_constrains_object(self, params): + tool = FunctionTool(type="function", name="ping", parameters=params) + choice = ToolChoiceFunction(type="function", name="ping") + schema = get_json_schema_from_tools(choice, [tool]) + assert schema == {"type": "object", "properties": {}} diff --git a/vllm/tool_parsers/utils.py b/vllm/tool_parsers/utils.py index 081e58da04..a2992261da 100644 --- a/vllm/tool_parsers/utils.py +++ b/vllm/tool_parsers/utils.py @@ -308,14 +308,18 @@ def find_tool_name( return False +def _params_or_empty_object(params: dict[str, Any] | None) -> dict[str, Any]: + # Empty/missing parameters still constrain arguments to a JSON object. + return params if params else {"type": "object", "properties": {}} + + def _get_tool_schema_from_name_and_params( name: str, params: dict[str, Any] | None ) -> dict: - params = params if params else {"type": "object", "properties": {}} return { "properties": { "name": {"type": "string", "enum": [name]}, - "parameters": params, + "parameters": _params_or_empty_object(params), }, "required": ["name", "parameters"], } @@ -403,7 +407,7 @@ def get_json_schema_from_tools( responses_tool_map.setdefault(name.rsplit("__", 1)[1], params) if tool_name not in responses_tool_map: raise ValueError(f"Tool '{tool_name}' has not been passed in `tools`.") - return responses_tool_map[tool_name] + return _params_or_empty_object(responses_tool_map[tool_name]) # tool_choice: Forced Function (ChatCompletion) if (not isinstance(tool_choice, str)) and isinstance( tool_choice, ChatCompletionNamedToolChoiceParam @@ -416,7 +420,7 @@ def get_json_schema_from_tools( } if tool_name not in chat_tool_map: raise ValueError(f"Tool '{tool_name}' has not been passed in `tools`.") - return chat_tool_map[tool_name].function.parameters + return _params_or_empty_object(chat_tool_map[tool_name].function.parameters) # tool_choice: "required" if tool_choice == "required": return _get_json_schema_from_tools(tools, parallel_tool_calls)