[Bugfix] Route Step3p5 forced tool choices through XML parser (#51810)

Signed-off-by: taking-lying-flat <1615405@qq.com>
Signed-off-by: Flora Feng <4florafeng@gmail.com>
Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
cherry77-cloud
2026-09-29 21:58:46 +00:00
committed by GitHub
co-authored by Flora Feng
parent 4e7af3b0de
commit 5e887a078b
3 changed files with 83 additions and 82 deletions
+25 -21
View File
@@ -5,6 +5,7 @@ import json
from collections.abc import Generator
import pytest
from openai.types.responses import FunctionTool
from vllm.entrypoints.generate.base.protocol import (
DeltaMessage,
@@ -386,26 +387,29 @@ TX
assert extracted_tool_calls.tool_calls[0].function.name == "get_current_weather"
def test_extract_tool_calls_type_conversion(step3p5_tokenizer):
"""Test parameter type conversion based on tool schema."""
tools = [
ChatCompletionToolsParam(
type="function",
function={
"name": "test_types",
"parameters": {
"type": "object",
"properties": {
"int_param": {"type": "integer"},
"float_param": {"type": "float"},
"bool_param": {"type": "boolean"},
"str_param": {"type": "string"},
"obj_param": {"type": "object"},
},
},
},
)
]
@pytest.mark.parametrize("api", ["chat", "responses"])
def test_extract_tool_calls_type_conversion(step3p5_tokenizer, api):
"""Test parameter type conversion based on Chat and Responses tool schemas."""
parameters = {
"type": "object",
"properties": {
"int_param": {"type": "integer"},
"float_param": {"type": "float"},
"bool_param": {"type": "boolean"},
"str_param": {"type": "string"},
"obj_param": {"type": "object"},
},
}
tools: list[ChatCompletionToolsParam | FunctionTool] = (
[
ChatCompletionToolsParam(
type="function",
function={"name": "test_types", "parameters": parameters},
)
]
if api == "chat"
else [FunctionTool(type="function", name="test_types", parameters=parameters)]
)
model_output = """<tool_call>
<function=test_types>
@@ -428,7 +432,7 @@ hello world
</tool_call>"""
parser = Step3p5ToolParser(step3p5_tokenizer, tools=tools)
request = ChatCompletionRequest(model=MODEL, messages=[], tools=tools)
request = ChatCompletionRequest(model=MODEL, messages=[])
extracted_tool_calls = parser.extract_tool_calls(model_output, request=request)
args = json.loads(extracted_tool_calls.tool_calls[0].function.arguments)
@@ -47,6 +47,7 @@ from vllm.tool_parsers.mimo_tool_parser import MiMoToolParser
from vllm.tool_parsers.minimax_m2_tool_parser import MinimaxM2ToolParser
from vllm.tool_parsers.plamo3_engine_tool_parser import Plamo3EngineToolParser
from vllm.tool_parsers.qwen3_engine_tool_parser import Qwen3EngineToolParser
from vllm.tool_parsers.step3p5_tool_parser import Step3p5ToolParser
from vllm.tool_parsers.structural_tag_registry import (
SUPPORTED_STRUCTURAL_TAG_MODELS,
VLLM_BUILTIN_STRUCTURAL_TAG_MODELS,
@@ -669,6 +670,7 @@ def test_get_model_structural_tag_supports_named_tool_choice(
(Llama3JsonToolParser, "llama"),
(MinimaxM2ToolParser, "minimax"),
(Qwen3EngineToolParser, "qwen_3_coder"),
(Step3p5ToolParser, "qwen_3_coder"),
(MiMoToolParser, "mimo"),
],
)
@@ -677,6 +679,47 @@ def test_tool_parsers_declare_matching_xgrammar_builtin_model(parser_cls, model)
assert not parser_cls.supports_required_and_named
@pytest.mark.parametrize(
"tool_choice",
["required", {"type": "function", "function": {"name": "get_weather"}}],
)
def test_step3p5_forced_tool_choice_round_trips_native_xml(
sample_tools: list[ChatCompletionToolsParam], tool_choice
):
"""Forced Step calls must be constrained to, and parsed from, Step XML."""
class StepParser(DelegatingParser):
tool_parser_cls = Step3p5ToolParser
request = ChatCompletionRequest(
messages=[],
model="m",
# Named tool choice validation reads tools in their JSON form.
tools=[tool.model_dump(exclude_none=True) for tool in sample_tools],
tool_choice=tool_choice,
)
parser = StepParser(MagicMock(), tools=sample_tools)
request = parser.adjust_request(request)
assert request.structured_outputs.json is None
grammar = Grammar.from_structural_tag(request.structured_outputs.structural_tag)
# Rendered exactly as the Step-3.5/3.7 chat templates render tool calls.
output = (
"<tool_call>\n<function=get_weather>\n"
"<parameter=city>\nDallas\n</parameter>\n"
"</function>\n</tool_call>"
)
assert _is_grammar_accept_string(grammar, output)
assert not _is_grammar_accept_string(grammar, "It is sunny in Dallas.")
_, content, tool_calls = parser.parse(output, request, enable_auto_tools=True)
assert not content
assert [(call.name, json.loads(call.arguments)) for call in tool_calls] == [
("get_weather", {"city": "Dallas"})
]
def test_tool_parsers_without_structural_tag_support_required_and_named():
class NonStructuralTagToolParser(ToolParser):
pass
+15 -61
View File
@@ -22,7 +22,11 @@ from vllm.entrypoints.openai.chat_completion.protocol import (
from vllm.logger import init_logger
from vllm.tokenizers import TokenizerLike
from vllm.tool_parsers.abstract_tool_parser import Tool, ToolParser
from vllm.tool_parsers.utils import safe_literal_eval
from vllm.tool_parsers.utils import (
find_tool_name,
find_tool_properties,
safe_literal_eval,
)
logger = init_logger(__name__)
@@ -421,43 +425,14 @@ class StreamingXMLToolCallParser:
def _validate_function_name(self, func_name: str) -> bool:
"""Check if function name exists in tool definitions."""
if not self.tools:
return False
for tool in self.tools:
if (
hasattr(tool, "type")
and tool.type == "function"
and hasattr(tool, "function")
and hasattr(tool.function, "name")
and tool.function.name == func_name
):
return True
return False
return find_tool_name(self.tools, func_name)
def _validate_parameter_name(self, param_name: str) -> bool:
"""Check if parameter exists in current function's tool definition."""
if not self.tools or not self.current_function_name:
return True
for tool in self.tools:
if (
hasattr(tool, "type")
and tool.type == "function"
and hasattr(tool, "function")
and hasattr(tool.function, "name")
and tool.function.name == self.current_function_name
):
if not hasattr(tool.function, "parameters"):
return True
params = tool.function.parameters
if isinstance(params, dict):
properties = params.get("properties", params)
return param_name in properties
break
return True
properties = find_tool_properties(self.tools, self.current_function_name)
return not properties or param_name in properties
def _should_skip_element(self, element: str) -> bool:
"""Determine whether an element should be skipped.
@@ -1190,34 +1165,10 @@ class StreamingXMLToolCallParser:
"""
if not self.tools or not self.current_function_name:
return "string"
for tool in self.tools:
if not hasattr(tool, "type") or not (
hasattr(tool, "function") and hasattr(tool.function, "name")
):
continue
if (
tool.type == "function"
and tool.function.name == self.current_function_name
):
if not hasattr(tool.function, "parameters"):
return "string"
params = tool.function.parameters
if isinstance(params, dict) and "properties" in params:
properties = params["properties"]
if param_name in properties and isinstance(
properties[param_name], dict
):
return self.repair_param_type(
str(properties[param_name].get("type", "string"))
)
elif isinstance(params, dict) and param_name in params:
param_config = params[param_name]
if isinstance(param_config, dict):
return self.repair_param_type(
str(param_config.get("type", "string"))
)
break
properties = find_tool_properties(self.tools, self.current_function_name)
param_config = properties.get(param_name)
if isinstance(param_config, dict):
return self.repair_param_type(str(param_config.get("type", "string")))
return "string"
def repair_param_type(self, param_type: str) -> str:
@@ -1357,6 +1308,9 @@ class StreamingXMLToolCallParser:
class Step3p5ToolParser(ToolParser):
# Step-3.5/3.7 chat templates use the Qwen3-Coder XML tool-call format.
structural_tag_model = "qwen_3_coder"
def __init__(self, tokenizer: TokenizerLike, tools: list[Tool] | None = None):
super().__init__(tokenizer, tools)
self.parser = StreamingXMLToolCallParser()