[Bugfix][Frontend] Enforce parallel_tool_calls=false in the required-tool grammar (#50502)
Signed-off-by: ostring <ostring@qq.com> Signed-off-by: sfeng33 <4florafeng@gmail.com> Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
@@ -49,7 +49,10 @@ from vllm.tool_parsers.streaming import (
|
||||
extract_named_tool_call_streaming,
|
||||
extract_required_tool_call_streaming,
|
||||
)
|
||||
from vllm.tool_parsers.structural_tag_registry import resolve_tool_strictness
|
||||
from vllm.tool_parsers.structural_tag_registry import (
|
||||
limit_to_single_tool_call,
|
||||
resolve_tool_strictness,
|
||||
)
|
||||
from vllm.tool_parsers.tool_strict_level import ToolStrictLevel
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -496,13 +499,20 @@ class DelegatingParser(Parser):
|
||||
|
||||
structured_outputs = request.extract_structured_outputs()
|
||||
is_auto = request.tool_choice == "auto"
|
||||
single_call = request.parallel_tool_calls is False
|
||||
strict_level = self.tool_strict_level
|
||||
if single_call and not (is_auto and structured_outputs is not None):
|
||||
# Limiting the call count needs the call envelope in the grammar.
|
||||
# Auto with structured outputs keeps its format-only grammar, so the
|
||||
# flag never makes calls possible that are impossible without it.
|
||||
strict_level = max(strict_level, ToolStrictLevel.FUNCTION)
|
||||
|
||||
resolved_tools = None
|
||||
if tool_parser.structural_tag_model is not None:
|
||||
resolved_tools = resolve_tool_strictness(
|
||||
request.tools,
|
||||
request.tool_choice,
|
||||
self.tool_strict_level,
|
||||
strict_level,
|
||||
)
|
||||
|
||||
output_format = None
|
||||
@@ -529,7 +539,7 @@ class DelegatingParser(Parser):
|
||||
tools_structural_tag = tool_parser.get_structural_tag(
|
||||
tag_request,
|
||||
reasoning=False,
|
||||
strict_level=self.tool_strict_level,
|
||||
strict_level=strict_level,
|
||||
)
|
||||
else:
|
||||
tools_structural_tag = None
|
||||
@@ -544,6 +554,8 @@ class DelegatingParser(Parser):
|
||||
)
|
||||
return request
|
||||
|
||||
if single_call:
|
||||
limit_to_single_tool_call(tools_structural_tag)
|
||||
structural_tag = tools_structural_tag
|
||||
if output_format is not None:
|
||||
structural_tag = StructuralTag(
|
||||
|
||||
@@ -141,7 +141,9 @@ class ToolParser:
|
||||
return request
|
||||
|
||||
json_schema_from_tool = get_json_schema_from_tools(
|
||||
tool_choice=request.tool_choice, tools=request.tools
|
||||
tool_choice=request.tool_choice,
|
||||
tools=request.tools,
|
||||
parallel_tool_calls=request.parallel_tool_calls,
|
||||
)
|
||||
# Set structured output params for tool calling
|
||||
if json_schema_from_tool is not None:
|
||||
|
||||
@@ -1,25 +1,8 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from xgrammar.structural_tag import TriggeredTagsFormat
|
||||
|
||||
from vllm.parser.engine.registered_adapters import MiMoParserToolAdapter
|
||||
from vllm.tool_parsers.tool_strict_level import ToolStrictLevel
|
||||
|
||||
|
||||
class MiMoToolParser(MiMoParserToolAdapter): # type: ignore[valid-type, misc]
|
||||
structural_tag_model = "mimo"
|
||||
|
||||
def get_structural_tag(
|
||||
self, request, *, reasoning=False, strict_level=ToolStrictLevel.AUTO
|
||||
):
|
||||
tag = super().get_structural_tag(
|
||||
request, reasoning=reasoning, strict_level=strict_level
|
||||
)
|
||||
if (
|
||||
tag is not None
|
||||
and request.parallel_tool_calls is False
|
||||
and isinstance(tag.format, TriggeredTagsFormat)
|
||||
):
|
||||
tag.format.stop_after_first = True
|
||||
return tag
|
||||
|
||||
@@ -9,6 +9,7 @@ from openai.types.responses.response import ToolChoice as ResponsesToolChoice
|
||||
from openai.types.responses.tool import Tool as ResponsesTool
|
||||
from openai.types.responses.tool_choice_allowed import ToolChoiceAllowed
|
||||
from openai.types.responses.tool_choice_function import ToolChoiceFunction
|
||||
from pydantic import BaseModel
|
||||
from xgrammar import StructuralTag, normalize_tool_choice
|
||||
from xgrammar import get_model_structural_tag as get_xgrammar_model_structural_tag
|
||||
from xgrammar.openai_tool_call_schema import (
|
||||
@@ -27,6 +28,7 @@ from xgrammar.structural_tag import (
|
||||
StarFormat,
|
||||
TagFormat,
|
||||
TagsWithSeparatorFormat,
|
||||
TokenTriggeredTagsFormat,
|
||||
TriggeredTagsFormat,
|
||||
)
|
||||
|
||||
@@ -197,6 +199,34 @@ def get_model_structural_tag(
|
||||
)
|
||||
|
||||
|
||||
_TOOL_CALL_LIST_FORMATS = (
|
||||
TriggeredTagsFormat,
|
||||
TokenTriggeredTagsFormat,
|
||||
TagsWithSeparatorFormat,
|
||||
)
|
||||
|
||||
|
||||
def limit_to_single_tool_call(tag: StructuralTag) -> StructuralTag:
|
||||
"""Stop every tool-call list in ``tag`` after its first call, in place.
|
||||
|
||||
Enforces ``parallel_tool_calls=false`` in the grammar. In every registered
|
||||
tag, ``_TOOL_CALL_LIST_FORMATS`` repeat tool calls, never arguments.
|
||||
"""
|
||||
|
||||
def visit(node: Any) -> None:
|
||||
if isinstance(node, BaseModel):
|
||||
if isinstance(node, _TOOL_CALL_LIST_FORMATS):
|
||||
node.stop_after_first = True
|
||||
for name in type(node).model_fields:
|
||||
visit(getattr(node, name))
|
||||
elif isinstance(node, (list, tuple)):
|
||||
for item in node:
|
||||
visit(item)
|
||||
|
||||
visit(tag.format)
|
||||
return tag
|
||||
|
||||
|
||||
def _dump_tool_for_xgrammar(
|
||||
tool: ChatCompletionToolsParam | ResponsesTool,
|
||||
) -> dict[str, Any]:
|
||||
|
||||
@@ -347,6 +347,7 @@ def _get_tool_schema_defs(
|
||||
|
||||
def _get_json_schema_from_tools(
|
||||
tools: list[Tool],
|
||||
parallel_tool_calls: bool | None = None,
|
||||
) -> dict:
|
||||
fn_tool_schemas: list[dict[str, Any]] = []
|
||||
fn_tools: list[Tool] = []
|
||||
@@ -361,7 +362,7 @@ def _get_json_schema_from_tools(
|
||||
elif _is_function_tool(tool):
|
||||
fn_tool_schemas.append(_get_tool_schema_from_tool(tool))
|
||||
fn_tools.append(tool)
|
||||
json_schema = {
|
||||
json_schema: dict[str, Any] = {
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"items": {
|
||||
@@ -369,6 +370,10 @@ def _get_json_schema_from_tools(
|
||||
"anyOf": fn_tool_schemas,
|
||||
},
|
||||
}
|
||||
# Only an explicit `false` opts out of parallel tool calls; `None` means the
|
||||
# field was never set and the default (true) applies.
|
||||
if parallel_tool_calls is False:
|
||||
json_schema["maxItems"] = 1
|
||||
json_schema_defs = _get_tool_schema_defs(fn_tools)
|
||||
if json_schema_defs:
|
||||
json_schema["$defs"] = json_schema_defs
|
||||
@@ -378,6 +383,7 @@ def _get_json_schema_from_tools(
|
||||
def get_json_schema_from_tools(
|
||||
tool_choice: str | ToolChoiceFunction | ChatCompletionNamedToolChoiceParam,
|
||||
tools: list[Tool] | None,
|
||||
parallel_tool_calls: bool | None = None,
|
||||
) -> str | dict | None:
|
||||
# tool_choice: "none"
|
||||
if tool_choice in ("none", None) or tools is None:
|
||||
@@ -413,7 +419,7 @@ def get_json_schema_from_tools(
|
||||
return chat_tool_map[tool_name].function.parameters
|
||||
# tool_choice: "required"
|
||||
if tool_choice == "required":
|
||||
return _get_json_schema_from_tools(tools)
|
||||
return _get_json_schema_from_tools(tools, parallel_tool_calls)
|
||||
# tool_choice: "auto"
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user