From 6cbbea872141851421dcad5fca009aa6cec93dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C4=8Cadek?= Date: Tue, 29 Sep 2026 09:11:56 +0200 Subject: [PATCH] [Bugfix] Hoist $defs/definitions in Cohere parser tool schema composition (#49602) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Václav Čadek Co-authored-by: Cursor Agent --- tests/parser/cohere/test_structural_tags.py | 96 +++++++++++++++++++++ vllm/parser/cohere_command.py | 29 +++---- 2 files changed, 109 insertions(+), 16 deletions(-) diff --git a/tests/parser/cohere/test_structural_tags.py b/tests/parser/cohere/test_structural_tags.py index 27440d52af..5083215d3b 100644 --- a/tests/parser/cohere/test_structural_tags.py +++ b/tests/parser/cohere/test_structural_tags.py @@ -10,6 +10,7 @@ from types import SimpleNamespace import pytest from pydantic import ValidationError +from xgrammar.testing import _is_grammar_accept_string from vllm.entrypoints.generate.base.protocol import ( JsonSchemaResponseFormat, @@ -20,9 +21,11 @@ from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionReque from vllm.entrypoints.openai.responses.protocol import ResponsesRequest from vllm.parser.cohere_command import ( CohereCommandParser, + CohereNormalizedTool, _has_effective_tools, _response_format_type, _schema_dict_from_structured_outputs, + collect_tool_schema, convert_schema_to_structural_tags, ) from vllm.sampling_params import StructuredOutputsParams @@ -41,6 +44,48 @@ GET_WEATHER_TOOL = { }, }, } +NESTED_DEFS_PARAMETERS = { + "$defs": { + "Location": { + "type": "object", + "properties": { + "city": {"type": "string"}, + "country": {"type": "string"}, + }, + "required": ["city"], + } + }, + "type": "object", + "properties": {"location": {"$ref": "#/$defs/Location"}}, + "required": ["location"], +} +NESTED_DEFS_TOOL = { + "type": "function", + "function": {"name": "get_weather", "parameters": NESTED_DEFS_PARAMETERS}, +} +RECURSIVE_DEFS_PARAMETERS = { + "$defs": { + "Finding": { + "type": "object", + "properties": { + "note": {"type": "string"}, + "related": {"type": "array", "items": {"$ref": "#/$defs/Finding"}}, + }, + } + }, + "type": "object", + "properties": {"finding": {"$ref": "#/$defs/Finding"}}, +} +DRAFT07_DEFINITIONS_PARAMETERS = { + "definitions": { + "Location": { + "type": "object", + "properties": {"city": {"type": "string"}}, + } + }, + "type": "object", + "properties": {"location": {"$ref": "#/definitions/Location"}}, +} VALID_STRUCTURAL_TAG = { "type": "structural_tag", "format": { @@ -360,3 +405,54 @@ class TestAdjustRequestTools: types = _content_types(o.structured_outputs.structural_tag) assert "grammar" in types assert "json_schema" in types + + +class TestCollectToolSchemaDefs: + def test_nested_defs_tool_adjust_request(self, parser) -> None: + o = parser.adjust_request( + _make_chat_request(tools=[NESTED_DEFS_TOOL], tool_choice="auto"), + ) + assert "grammar" in _content_types(o.structured_outputs.structural_tag) + + @staticmethod + def _tool_calls_json(name: str, parameters: dict) -> str: + return json.dumps( + [{"tool_call_id": "0", "tool_name": name, "parameters": parameters}] + ) + + def test_recursive_defs(self) -> None: + grammar = collect_tool_schema( + [CohereNormalizedTool(name="report", parameters=RECURSIVE_DEFS_PARAMETERS)] + ) + nested = {"note": "a", "related": [{"note": "b", "related": []}]} + assert _is_grammar_accept_string( + grammar, self._tool_calls_json("report", {"finding": nested}) + ) + bad = {"note": 1, "related": []} + assert not _is_grammar_accept_string( + grammar, self._tool_calls_json("report", {"finding": bad}) + ) + + def test_draft07_definitions(self) -> None: + grammar = collect_tool_schema( + [ + CohereNormalizedTool( + name="get_weather", parameters=DRAFT07_DEFINITIONS_PARAMETERS + ) + ] + ) + assert _is_grammar_accept_string( + grammar, + self._tool_calls_json("get_weather", {"location": {"city": "Prague"}}), + ) + assert not _is_grammar_accept_string( + grammar, + self._tool_calls_json("get_weather", {"location": {"city": 1}}), + ) + + def test_input_parameters_not_mutated(self) -> None: + params = json.loads(json.dumps(NESTED_DEFS_PARAMETERS)) + collect_tool_schema( + [CohereNormalizedTool(name="get_weather", parameters=params)] + ) + assert params == NESTED_DEFS_PARAMETERS diff --git a/vllm/parser/cohere_command.py b/vllm/parser/cohere_command.py index c37cc569ac..a1df491401 100644 --- a/vllm/parser/cohere_command.py +++ b/vllm/parser/cohere_command.py @@ -127,22 +127,19 @@ def collect_tool_schema(tool_schema: list[CohereNormalizedTool]) -> str: tool_dictionary: dict[str, str] = {} for tool in tool_schema: tool_name = tool["name"] - tool_parameters = json.dumps(tool["parameters"]) - json_schema = f"""{{ - "type": "object", - "properties": {{ - "tool_call_id": {{ - "type": "string", - "pattern": "^[0-9]+$" - }}, - "tool_name": {{ - "type": "string", - "const": "{tool_name}" - }}, - "parameters": {tool_parameters} - }} - }}""" - tool_grammar = str(xgr.Grammar.from_json_schema(json_schema)) + tool_parameters = dict(tool["parameters"]) + json_schema: dict[str, Any] = { + "type": "object", + "properties": { + "tool_call_id": {"type": "string", "pattern": "^[0-9]+$"}, + "tool_name": {"type": "string", "const": tool_name}, + "parameters": tool_parameters, + }, + } + for defs_key in ("$defs", "definitions"): + if defs_key in tool_parameters: + json_schema[defs_key] = tool_parameters.pop(defs_key) + tool_grammar = str(xgr.Grammar.from_json_schema(json.dumps(json_schema))) for match in re.findall(r"\b(\w+)\s*::=", tool_grammar): tool_grammar = re.sub( rf"\b{re.escape(match)}\b", tool_name + match, tool_grammar