[Bugfix][Frontend] Constrain forced named tool choice with empty parameters to a JSON object (#45290)

Signed-off-by: Ting Sun <suntcrick@gmail.com>
Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
Ting SUN
2026-09-30 05:44:35 +00:00
committed by GitHub
co-authored by Flora Feng
parent 6945d142e5
commit fcf4750466
2 changed files with 33 additions and 5 deletions
+25 -1
View File
@@ -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": {}}
+8 -4
View File
@@ -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)