[Frontend] Support strict MiMo-V2.6 tool calling (#58019)

Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: yuchuan <yuchuan.7streams@gmail.com>
Co-authored-by: Bugen Zhao <i@bugenzhao.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Ubospica <ubospica@gmail.com>
Signed-off-by: yuchuan <yuchuan.7streams@gmail.com>
Signed-off-by: Bugen Zhao <i@bugenzhao.com>
This commit is contained in:
Yixin Dong
2026-09-29 12:47:47 +00:00
committed by GitHub
co-authored by Codex yuchuan Bugen Zhao Claude Opus 5.5
parent 79a0307d74
commit dfc8e0f3e2
17 changed files with 243 additions and 15 deletions
+24
View File
@@ -1175,3 +1175,27 @@ class TestNestedSchemaCoercion:
assert questions[0]["question"] == "Pick a color"
assert questions[0]["multiSelect"] is False
assert questions[0]["answer"] is None
@pytest.mark.parametrize("value", ["\n北京 &amp;\n", "null", "42", "", "a\nb"])
@pytest.mark.parametrize("chunk_size", [1, 7, 1000])
def test_mimo_preserves_verbatim_parameter_values(
mock_tokenizer, mock_request, value, chunk_size
):
from vllm.parser.mimo import MiMoParser
text = (
f"<tool_call><function=run><parameter=text>{value}</parameter>"
"</function></tool_call>"
)
parser = MiMoParser(mock_tokenizer, chat_template_kwargs={"enable_thinking": False})
result = parser.extract_tool_calls(text, mock_request)
assert json.loads(result.tool_calls[0].function.arguments) == {"text": value}
parser = MiMoParser(mock_tokenizer, chat_template_kwargs={"enable_thinking": False})
results = simulate_tool_streaming(
parser,
mock_request,
[text[i : i + chunk_size] for i in range(0, len(text), chunk_size)],
)
assert collect_function_name(results) == "run"
assert json.loads(collect_tool_arguments(results)) == {"text": value}
+39 -3
View File
@@ -15,6 +15,7 @@ from __future__ import annotations
import functools
import json
from collections.abc import Callable
from dataclasses import dataclass, replace
from typing import Any
@@ -39,6 +40,7 @@ from vllm.parser.engine.registered_adapters import (
GraniteThinkingParser,
InklingParser,
KimiK2Parser,
MiMoParser,
MinimaxM2Parser,
NemotronV3Parser,
Plamo3Parser,
@@ -356,7 +358,12 @@ def _qwen3_tool_segments(tc: ToolCallSpec) -> list[tuple[str, bool]]:
]
def _qwen3_segments(scenario: Scenario) -> list[tuple[str, bool]]:
def _qwen3_segments(
scenario: Scenario,
tool_segments: Callable[[ToolCallSpec], list[tuple[str, bool]]] = (
_qwen3_tool_segments
),
) -> list[tuple[str, bool]]:
segs: list[tuple[str, bool]] = []
if scenario.reasoning is not None:
segs.append((scenario.reasoning, False))
@@ -369,7 +376,7 @@ def _qwen3_segments(scenario: Scenario) -> list[tuple[str, bool]]:
segs.append((scenario.content, False))
if scenario.tool_calls:
for tc in scenario.tool_calls:
segs.extend(_qwen3_tool_segments(tc))
segs.extend(tool_segments(tc))
return segs
@@ -389,6 +396,9 @@ def _build_qwen3(
parser_cls: type = Qwen3Parser,
strip_trailing_ws: bool = False,
validate: bool = True,
tool_segments: Callable[[ToolCallSpec], list[tuple[str, bool]]] = (
_qwen3_tool_segments
),
) -> Sample:
expected_reasoning: str | None
if scenario.reasoning is not None:
@@ -403,7 +413,7 @@ def _build_qwen3(
sample_id=f"{name}-{scenario.id}",
description=scenario.description,
vocab=_QWEN3_VOCAB,
segments=_qwen3_segments(scenario),
segments=_qwen3_segments(scenario, tool_segments),
expected_reasoning=expected_reasoning,
expected_content=_qwen3_expected_content(scenario),
expected_tool_calls=_expected_tc(scenario),
@@ -414,6 +424,31 @@ def _build_qwen3(
return sample
# ── MiMo (compact Qwen3 XML, no newlines between tags) ───────────────
def _mimo_tool_segments(tc: ToolCallSpec) -> list[tuple[str, bool]]:
parts = [f"<function={tc.name}>"]
for key, value in tc.arguments.items():
parts.append(f"<parameter={key}>{_qwen3_arg_value(value)}</parameter>")
parts.append("</function>")
return [
("<tool_call>", True),
("".join(parts), False),
("</tool_call>", True),
]
def _build_mimo(scenario: Scenario, validate: bool = True) -> Sample:
return _build_qwen3(
scenario,
name="mimo",
parser_cls=MiMoParser,
validate=validate,
tool_segments=_mimo_tool_segments,
)
# ── MiniMax M2 (XML invoke format, starts in REASONING) ──────────────
_MINIMAX_M2_VOCAB: dict[str, int] = {
@@ -1232,6 +1267,7 @@ _BUILDERS: dict[str, Any] = {
"glm47_moe": _build_glm47_moe,
"kimi_k2": _build_kimi_k2,
"qwen3": _build_qwen3,
"mimo": _build_mimo,
"inkling": _build_inkling,
"plamo3": _build_plamo3,
}
@@ -40,6 +40,7 @@ from vllm.tool_parsers.hermes_tool_parser import Hermes2ProToolParser
from vllm.tool_parsers.kimi_k2_tool_parser import KimiK2ToolParser
from vllm.tool_parsers.kimi_k3_tool_parser import KimiK3ToolParser
from vllm.tool_parsers.llama_tool_parser import Llama3JsonToolParser
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
@@ -665,6 +666,7 @@ def test_get_model_structural_tag_supports_named_tool_choice(
(Llama3JsonToolParser, "llama"),
(MinimaxM2ToolParser, "minimax"),
(Qwen3EngineToolParser, "qwen_3_coder"),
(MiMoToolParser, "mimo"),
],
)
def test_tool_parsers_declare_matching_xgrammar_builtin_model(parser_cls, model):
@@ -727,7 +729,7 @@ def test_get_structural_tag_disables_reasoning(
@pytest.mark.parametrize(
"parser_cls", [Qwen3EngineToolParser, DeepSeekV41EngineToolParser]
"parser_cls", [Qwen3EngineToolParser, DeepSeekV41EngineToolParser, MiMoToolParser]
)
def test_unified_parser_get_structural_tag_disables_reasoning(
parser_cls,
@@ -1167,3 +1169,78 @@ def test_tool_strict_level_from_name():
ToolStrictLevel.from_name("strict")
with pytest.raises(ValueError, match="expected one of auto, function, parameter"):
ToolStrictLevel.from_name("off")
@pytest.mark.parametrize("policy", ["auto", "required", "named"])
def test_mimo_strict_compact_xml(policy):
tools = [
ChatCompletionToolsParam(
type="function",
function={
"name": "run",
"strict": True,
"parameters": {
"type": "object",
"properties": {"n": {"type": "integer", "minimum": 1}},
"required": ["n"],
"additionalProperties": False,
},
},
)
]
choice = (
ChatCompletionNamedToolChoiceParam(
function=ChatCompletionNamedFunction(name="run")
)
if policy == "named"
else policy
)
tag = get_model_structural_tag("mimo", tools, choice, reasoning=False)
grammar = Grammar.from_structural_tag(tag)
call = "<tool_call><function=run><parameter=n>2</parameter></function></tool_call>"
assert _is_grammar_accept_string(grammar, call)
assert _is_grammar_accept_string(grammar, call * 2) == (policy != "named")
assert _is_grammar_accept_string(grammar, "answer") == (policy == "auto")
for invalid in [
call.replace("function=run", "function=unknown"),
call.replace("<parameter=n>2</parameter>", ""),
call.replace(">2</parameter>", ">bad</parameter>"),
call.replace(">2</parameter>", ">0</parameter>"),
call.replace("<parameter=n>", "<parameter=other>"),
call.replace("</function>", "<parameter=n>3</parameter></function>"),
"<think>plan</think>" + call,
]:
assert not _is_grammar_accept_string(grammar, invalid)
tools[0].function.strict = False
relaxed = Grammar.from_structural_tag(
get_model_structural_tag("mimo", tools, "required", reasoning=False)
)
assert _is_grammar_accept_string(
relaxed, call.replace("<parameter=n>", "<parameter=other>")
)
@pytest.mark.parametrize("policy", ["auto", "required"])
def test_mimo_request_limits_parallel_calls(sample_tools_strict, policy):
from vllm.parser.parser_manager import ParserManager
request = ChatCompletionRequest(
model="mimo",
messages=[],
tools=[t.model_dump() for t in sample_tools_strict],
tool_choice=policy,
parallel_tool_calls=False,
)
parser_cls = ParserManager.get_parser(
tool_parser_name="mimo", enable_auto_tools=True
)
parser = parser_cls(MagicMock(), tools=sample_tools_strict)
adjusted = parser.adjust_request(request)
grammar = Grammar.from_structural_tag(adjusted.structured_outputs.structural_tag)
call = (
"<tool_call><function=get_weather><parameter=city>北京</parameter>"
"</function></tool_call>"
)
assert _is_grammar_accept_string(grammar, call)
assert not _is_grammar_accept_string(grammar, call * 2)
assert not _is_grammar_accept_string(grammar, call + "extra text")