[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:
@@ -5,7 +5,7 @@ from copy import deepcopy
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import regex as re
|
import regex as re
|
||||||
from openai.types.responses import FunctionTool, WebSearchTool
|
from openai.types.responses import FunctionTool, ToolChoiceFunction, WebSearchTool
|
||||||
from pydantic import TypeAdapter
|
from pydantic import TypeAdapter
|
||||||
|
|
||||||
from vllm.entrypoints.openai.chat_completion.protocol import (
|
from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||||
@@ -456,3 +456,27 @@ class TestParallelToolCallsConstraint:
|
|||||||
)
|
)
|
||||||
assert isinstance(schema, dict)
|
assert isinstance(schema, dict)
|
||||||
assert "maxItems" not in schema
|
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": {}}
|
||||||
|
|||||||
@@ -308,14 +308,18 @@ def find_tool_name(
|
|||||||
return False
|
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(
|
def _get_tool_schema_from_name_and_params(
|
||||||
name: str, params: dict[str, Any] | None
|
name: str, params: dict[str, Any] | None
|
||||||
) -> dict:
|
) -> dict:
|
||||||
params = params if params else {"type": "object", "properties": {}}
|
|
||||||
return {
|
return {
|
||||||
"properties": {
|
"properties": {
|
||||||
"name": {"type": "string", "enum": [name]},
|
"name": {"type": "string", "enum": [name]},
|
||||||
"parameters": params,
|
"parameters": _params_or_empty_object(params),
|
||||||
},
|
},
|
||||||
"required": ["name", "parameters"],
|
"required": ["name", "parameters"],
|
||||||
}
|
}
|
||||||
@@ -403,7 +407,7 @@ def get_json_schema_from_tools(
|
|||||||
responses_tool_map.setdefault(name.rsplit("__", 1)[1], params)
|
responses_tool_map.setdefault(name.rsplit("__", 1)[1], params)
|
||||||
if tool_name not in responses_tool_map:
|
if tool_name not in responses_tool_map:
|
||||||
raise ValueError(f"Tool '{tool_name}' has not been passed in `tools`.")
|
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)
|
# tool_choice: Forced Function (ChatCompletion)
|
||||||
if (not isinstance(tool_choice, str)) and isinstance(
|
if (not isinstance(tool_choice, str)) and isinstance(
|
||||||
tool_choice, ChatCompletionNamedToolChoiceParam
|
tool_choice, ChatCompletionNamedToolChoiceParam
|
||||||
@@ -416,7 +420,7 @@ def get_json_schema_from_tools(
|
|||||||
}
|
}
|
||||||
if tool_name not in chat_tool_map:
|
if tool_name not in chat_tool_map:
|
||||||
raise ValueError(f"Tool '{tool_name}' has not been passed in `tools`.")
|
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"
|
# tool_choice: "required"
|
||||||
if tool_choice == "required":
|
if tool_choice == "required":
|
||||||
return _get_json_schema_from_tools(tools, parallel_tool_calls)
|
return _get_json_schema_from_tools(tools, parallel_tool_calls)
|
||||||
|
|||||||
Reference in New Issue
Block a user