[Frontend] Add Streaming Parser Engine and new MinimaxM2 Parser (#45701)

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
This commit is contained in:
Chauncey
2026-06-16 13:38:17 -04:00
committed by GitHub
parent 44b2512767
commit f00e163f35
12 changed files with 588 additions and 481 deletions
+264
View File
@@ -0,0 +1,264 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import json
import pytest
from tests.parser.engine.conftest import make_mock_tokenizer
from tests.parser.engine.streaming_helpers import (
collect_function_name,
collect_tool_arguments,
simulate_tool_streaming,
)
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionToolsParam,
FunctionDefinition,
)
from vllm.parser.minimax_m2 import (
THINK_END,
TOOL_CALL_END,
TOOL_CALL_START,
MinimaxM2Parser,
)
@pytest.fixture
def mock_tokenizer():
return make_mock_tokenizer(
{
THINK_END: 99,
TOOL_CALL_START: 100,
TOOL_CALL_END: 101,
}
)
@pytest.fixture
def parser(mock_tokenizer):
return MinimaxM2Parser(mock_tokenizer)
def make_tools(*names: str):
return [
ChatCompletionToolsParam(
function=FunctionDefinition(
name=name,
parameters={
"type": "object",
"properties": {},
},
),
)
for name in names
]
class TestNonStreaming:
def test_no_tool_calls(self, parser, mock_request):
result = parser.extract_tool_calls(
"</think>This is a regular response without tool calls.",
mock_request,
)
assert result.tools_called is False
assert result.tool_calls == []
assert result.content == "This is a regular response without tool calls."
def test_single_tool_call(self, parser, mock_request):
result = parser.extract_tool_calls(
'<minimax:tool_call><invoke name="get_weather">'
'<parameter name="city">Seattle</parameter>'
"</invoke></minimax:tool_call>",
mock_request,
)
assert result.tools_called is True
assert len(result.tool_calls) == 1
assert result.tool_calls[0].function.name == "get_weather"
assert json.loads(result.tool_calls[0].function.arguments) == {
"city": "Seattle",
}
def test_multiple_invokes(self, parser, mock_request):
result = parser.extract_tool_calls(
"<minimax:tool_call>"
'<invoke name="search"><parameter name="q">OpenAI</parameter></invoke>'
'<invoke name="search"><parameter name="q">vLLM</parameter></invoke>'
"</minimax:tool_call>",
mock_request,
)
assert result.tools_called is True
assert [tc.function.name for tc in result.tool_calls] == ["search", "search"]
assert json.loads(result.tool_calls[0].function.arguments) == {"q": "OpenAI"}
assert json.loads(result.tool_calls[1].function.arguments) == {"q": "vLLM"}
def test_schema_type_coercion(self, mock_tokenizer, mock_request):
tools = [
ChatCompletionToolsParam(
function=FunctionDefinition(
name="forecast",
parameters={
"type": "object",
"properties": {
"days": {"type": "integer"},
"include_hourly": {"type": "boolean"},
},
},
),
)
]
parser = MinimaxM2Parser(mock_tokenizer, tools=tools)
mock_request.tools = tools
result = parser.extract_tool_calls(
'<minimax:tool_call><invoke name="forecast">'
'<parameter name="days">5</parameter>'
'<parameter name="include_hourly">true</parameter>'
"</invoke></minimax:tool_call>",
mock_request,
)
assert json.loads(result.tool_calls[0].function.arguments) == {
"days": 5,
"include_hourly": True,
}
def test_invalid_tool_name_is_rejected(self, mock_tokenizer, mock_request):
tools = make_tools("search")
parser = MinimaxM2Parser(mock_tokenizer)
mock_request.tools = tools
result = parser.extract_tool_calls(
'<minimax:tool_call><invoke name="img_gen">'
'<parameter name="prompt">a cat</parameter>'
"</invoke></minimax:tool_call>",
mock_request,
)
assert result.tools_called is False
assert result.tool_calls == []
def test_mixed_tool_names_only_return_valid(self, mock_tokenizer, mock_request):
tools = make_tools("search")
parser = MinimaxM2Parser(mock_tokenizer)
mock_request.tools = tools
result = parser.extract_tool_calls(
"<minimax:tool_call>"
'<invoke name="img_gen"><parameter name="prompt">cat</parameter></invoke>'
'<invoke name="search"><parameter name="query">news</parameter></invoke>'
"</minimax:tool_call>",
mock_request,
)
assert result.tools_called is True
assert [tc.function.name for tc in result.tool_calls] == ["search"]
assert json.loads(result.tool_calls[0].function.arguments) == {
"query": "news",
}
class TestStreaming:
def test_streaming_single_tool_call(self, parser, mock_request):
results = simulate_tool_streaming(
parser,
mock_request,
[
"<minimax:tool_call>",
'<invoke name="get_weather">',
'<parameter name="city">Seattle</parameter>',
"</invoke></minimax:tool_call>",
],
)
assert collect_function_name(results) == "get_weather"
assert json.loads(collect_tool_arguments(results)) == {
"city": "Seattle",
}
def test_streaming_multiple_invokes(self, parser, mock_request):
results = simulate_tool_streaming(
parser,
mock_request,
[
"<minimax:tool_call>",
'<invoke name="a"><parameter name="x">1</parameter></invoke>',
'<invoke name="b"><parameter name="y">2</parameter></invoke>',
"</minimax:tool_call>",
],
)
tool_names = [
tc.function.name
for delta, _ in results
if delta and delta.tool_calls
for tc in delta.tool_calls
if tc.function and tc.function.name
]
assert tool_names == ["a", "b"]
def test_streaming_invoke_prefix_split_before_quote(self, parser, mock_request):
results = simulate_tool_streaming(
parser,
mock_request,
[
"<minimax:tool_call>",
"<invoke name=",
'"get_weather">',
'<parameter name="city">Seattle</parameter>',
"</invoke></minimax:tool_call>",
],
)
assert collect_function_name(results) == "get_weather"
assert json.loads(collect_tool_arguments(results)) == {
"city": "Seattle",
}
def test_streaming_invalid_tool_name_is_rejected(
self, mock_tokenizer, mock_request
):
tools = make_tools("search")
parser = MinimaxM2Parser(mock_tokenizer)
mock_request.tools = tools
results = simulate_tool_streaming(
parser,
mock_request,
[
"<minimax:tool_call>",
'<invoke name="img_gen">',
'<parameter name="prompt">cat</parameter>',
"</invoke></minimax:tool_call>",
],
)
assert collect_function_name(results) is None
assert collect_tool_arguments(results) == ""
class TestReasoning:
def test_extract_reasoning_without_start_token(self, parser, mock_request):
reasoning, content = parser.extract_reasoning(
"This is reasoning</think>This is content",
mock_request,
)
assert reasoning == "This is reasoning"
assert content == "This is content"
def test_extract_reasoning_without_end_token(self, parser, mock_request):
reasoning, content = parser.extract_reasoning(
"This is still reasoning",
mock_request,
)
assert reasoning == "This is still reasoning"
assert content is None
def test_extract_content_ids_without_end_token(self, parser):
assert parser.extract_content_ids([1, 2, 3]) == []
def test_extract_content_ids_after_end_token(self, parser):
assert parser.extract_content_ids([1, 99, 2, 3]) == [2, 3]
+75
View File
@@ -30,6 +30,7 @@ from vllm.entrypoints.openai.chat_completion.protocol import (
)
from vllm.parser.engine.registered_adapters import (
Gemma4Parser,
MinimaxM2Parser,
NemotronV3Parser,
Qwen3Parser,
)
@@ -393,6 +394,79 @@ def _build_qwen3(
return sample
# ── MiniMax M2 (XML invoke format, starts in REASONING) ──────────────
_MINIMAX_M2_VOCAB: dict[str, int] = {
"<think>": 50,
"</think>": 51,
"<minimax:tool_call>": 60,
"</minimax:tool_call>": 61,
}
def _minimax_m2_arg_value(value: Any) -> str:
if isinstance(value, bool):
return "true" if value else "false"
if isinstance(value, (int, float)):
return str(value)
if isinstance(value, str):
return value
return json.dumps(value, ensure_ascii=False)
def _minimax_m2_tool_segments(tool_calls: list[ToolCallSpec]) -> list[tuple[str, bool]]:
segs: list[tuple[str, bool]] = [("<minimax:tool_call>", True)]
for tc in tool_calls:
segs.append((f'<invoke name="{tc.name}">', False))
for key, value in tc.arguments.items():
segs.append(
(
f'<parameter name="{key}">'
f"{_minimax_m2_arg_value(value)}"
"</parameter>",
False,
)
)
segs.append(("</invoke>", False))
segs.append(("</minimax:tool_call>", True))
return segs
def _minimax_m2_segments(scenario: Scenario) -> list[tuple[str, bool]]:
segs: list[tuple[str, bool]] = []
if scenario.reasoning is not None:
segs.append((scenario.reasoning, False))
if scenario.content is not None or scenario.tool_calls:
segs.append(("</think>", True))
if scenario.content is not None:
segs.append((scenario.content, False))
if scenario.tool_calls:
segs.extend(_minimax_m2_tool_segments(scenario.tool_calls))
return segs
def _build_minimax_m2(scenario: Scenario, validate: bool = True) -> Sample:
expected_reasoning: str | None
if scenario.reasoning is not None:
expected_reasoning = scenario.reasoning.rstrip()
else:
expected_reasoning = ""
sample = _make_sample(
sample_id=f"minimax_m2-{scenario.id}",
description=scenario.description,
vocab=_MINIMAX_M2_VOCAB,
segments=_minimax_m2_segments(scenario),
expected_reasoning=expected_reasoning,
expected_content=_qwen3_expected_content(scenario),
expected_tool_calls=_expected_tc(scenario),
tools=_expected_tools(scenario),
)
if validate:
_validate_sample(sample, MinimaxM2Parser)
return sample
# ── Gemma4 (channel reasoning, custom arg format) ────────────────────
_GEMMA4_VOCAB: dict[str, int] = {
@@ -502,6 +576,7 @@ def _build_nemotron_v3(scenario: Scenario, validate: bool = True) -> Sample:
_BUILDERS: dict[str, Any] = {
"qwen3": _build_qwen3,
"gemma4": _build_gemma4,
"minimax_m2": _build_minimax_m2,
"nemotron_v3": _build_nemotron_v3,
}
@@ -59,14 +59,6 @@ MULTIPLE_LINES = {
"is_reasoning_end": True,
}
# Case: only end token (empty reasoning, immediate response)
SHORTEST_REASONING_NO_STREAMING = {
"output": "</think>This is the response",
"reasoning": "",
"content": "This is the response",
"is_reasoning_end": True,
}
# Case: only end token streaming (reasoning is None because it's just the token)
SHORTEST_REASONING_STREAMING = {
"output": "</think>This is the response",
@@ -75,14 +67,6 @@ SHORTEST_REASONING_STREAMING = {
"is_reasoning_end": True,
}
# Case: empty output
EMPTY = {
"output": "",
"reasoning": "",
"content": None,
"is_reasoning_end": False,
}
# Case: empty streaming
EMPTY_STREAMING = {
"output": "",
@@ -149,21 +133,11 @@ TEST_CASES = [
MULTIPLE_LINES,
id="multiple_lines_streaming",
),
pytest.param(
False,
SHORTEST_REASONING_NO_STREAMING,
id="shortest_reasoning",
),
pytest.param(
True,
SHORTEST_REASONING_STREAMING,
id="shortest_reasoning_streaming",
),
pytest.param(
False,
EMPTY,
id="empty",
),
pytest.param(
True,
EMPTY_STREAMING,
@@ -18,7 +18,6 @@ pytestmark = pytest.mark.cpu_test
# Token IDs matching FakeTokenizer.vocab
TC_START_ID = 1
TC_END_ID = 2
EOS_ID = 99
class FakeTokenizer:
@@ -34,6 +33,10 @@ class FakeTokenizer:
def get_vocab(self):
return self.vocab
def decode(self, token_ids):
id_to_token = {v: k for k, v in self.vocab.items()}
return "".join(id_to_token.get(token_id, "") for token_id in token_ids)
@pytest.fixture
def parser():
@@ -121,7 +124,6 @@ class TestContentStreaming:
"""No tool call tokens — all text is streamed as content."""
results = _feed(parser, ["Hello ", "world"])
assert _collect_content(results) == "Hello world"
assert not parser.prev_tool_call_arr
def test_content_before_tool_call(self, parser):
"""Text before <minimax:tool_call> is streamed as content."""
@@ -135,7 +137,6 @@ class TestContentStreaming:
],
)
assert _collect_content(results) == "Let me check. "
assert len(parser.prev_tool_call_arr) == 1
def test_empty_delta_no_crash(self, parser):
"""Empty delta_text with no token IDs returns None."""
@@ -262,45 +263,6 @@ class TestMultipleInvokes:
assert tc[1]["name"] == "get_stock"
# ---------------------------------------------------------------------------
# Internal state: prev_tool_call_arr
# ---------------------------------------------------------------------------
class TestInternalState:
"""Verify prev_tool_call_arr is correct."""
def test_prev_tool_call_arr_single(self, parser):
_feed(
parser,
[
'<minimax:tool_call><invoke name="fn">'
'<parameter name="a">1</parameter>'
"</invoke></minimax:tool_call>",
],
)
assert len(parser.prev_tool_call_arr) == 1
assert parser.prev_tool_call_arr[0]["name"] == "fn"
assert parser.prev_tool_call_arr[0]["arguments"] == {"a": "1"}
def test_prev_tool_call_arr_multiple(self, parser):
"""prev_tool_call_arr records each invoke with correct arguments."""
_feed(
parser,
[
"<minimax:tool_call>",
'<invoke name="search"><parameter name="q">hello</parameter></invoke>',
'<invoke name="search"><parameter name="q">world</parameter></invoke>',
"</minimax:tool_call>",
],
)
assert len(parser.prev_tool_call_arr) == 2
assert parser.prev_tool_call_arr[0]["name"] == "search"
assert parser.prev_tool_call_arr[0]["arguments"] == {"q": "hello"}
assert parser.prev_tool_call_arr[1]["name"] == "search"
assert parser.prev_tool_call_arr[1]["arguments"] == {"q": "world"}
# ---------------------------------------------------------------------------
# DeltaMessage structure
# ---------------------------------------------------------------------------
@@ -324,7 +286,7 @@ class TestDeltaMessageFormat:
tc = tc_deltas[0]
assert tc.index == 0
assert tc.type == "function"
assert tc.id is not None and tc.id.startswith("call_")
assert tc.id is not None
assert tc.function.name == "fn"
assert json.loads(tc.function.arguments) == {"k": "v"}
@@ -344,72 +306,6 @@ class TestDeltaMessageFormat:
assert indices == [0, 1]
# ---------------------------------------------------------------------------
# Phase 3: EOS handling
# ---------------------------------------------------------------------------
class TestEOSHandling:
"""Tests for the end-of-stream phase."""
def test_eos_after_tool_calls(self, parser):
"""EOS token (empty delta, non-special token id) returns content=''."""
results = _feed(
parser,
[
"<minimax:tool_call>",
'<invoke name="fn"><parameter name="k">v</parameter></invoke>',
"</minimax:tool_call>",
# EOS: empty delta_text, non-special token id
("", [EOS_ID]),
],
)
# Last result should be the EOS empty-content signal
assert results[-1].content == ""
def test_end_token_ignored(self, parser):
"""</minimax:tool_call> special token should NOT trigger EOS."""
results = _feed(
parser,
[
"<minimax:tool_call>",
'<invoke name="fn"><parameter name="k">v</parameter></invoke>',
# </minimax:tool_call> arrives as special token
("", [TC_END_ID]),
],
)
# The tool call delta should be emitted, but no EOS signal
assert not any(r.content == "" and r.tool_calls is None for r in results)
# ---------------------------------------------------------------------------
# Start token detection via token IDs
# ---------------------------------------------------------------------------
class TestSpecialTokenDetection:
"""Start token arrives as a special token (not in delta_text)."""
def test_start_token_via_id(self, parser):
"""<minimax:tool_call> detected via delta_token_ids, not text."""
results = _feed(parser, ["Hello "])
assert _collect_content(results) == "Hello "
# Start token as special token (empty delta_text)
previous = "Hello "
result = parser.extract_tool_calls_streaming(
previous_text=previous,
current_text=previous,
delta_text="",
previous_token_ids=[],
current_token_ids=[],
delta_token_ids=[TC_START_ID],
request=None,
)
assert result is None # no content to emit
assert parser.is_tool_call_started is True
# ---------------------------------------------------------------------------
# Large chunks (stream_interval > 1)
# ---------------------------------------------------------------------------
@@ -419,7 +315,7 @@ class TestLargeChunks:
"""Simulate stream_interval > 1 where many tokens arrive at once."""
def test_header_and_params_in_separate_chunks(self, parser):
"""Header in chunk 1, all params + close in chunk 2, then EOS."""
"""Header in chunk 1, all params + close in chunk 2."""
chunk1 = '<minimax:tool_call><invoke name="get_weather">'
chunk2 = (
'<parameter name="city">Seattle</parameter>'
@@ -432,7 +328,6 @@ class TestLargeChunks:
[
chunk1,
chunk2,
("", [EOS_ID]),
],
)
@@ -441,12 +336,6 @@ class TestLargeChunks:
parsed = json.loads(tc[0]["arguments"])
assert parsed == {"city": "Seattle", "days": "5"}
assert len(parser.prev_tool_call_arr) == 1
assert parser.prev_tool_call_arr[0]["arguments"] == {
"city": "Seattle",
"days": "5",
}
class TestAnyOfNullableParam:
"""Regression: anyOf nullable parameter parsing (PR #32342)."""