[Mini-vLLM] Cut dead subsystems: models, entrypoints, backends, utils, tests

- Drop dead model modules, keeping files live imports require
- Remove anthropic, cohere, codegen entrypoints and batch STT modes
- Delete unused attention backends, MoE experts, and NPU/XPU util leaves
- Strip dead third_party trees and ROCm leaves with no CUDA path
- Remove mooncake EC connectors: dependency missing since branch base
- Re-audit verified by full import sweep and Qwen3.5 smoke test

Co-authored-by: Hinata <hinata@idihore.id>
Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
2026-10-19 00:20:48 +07:00
co-authored by Hinata
parent 6a95f62e2f
commit 81074e94a3
940 changed files with 11 additions and 380533 deletions
-311
View File
@@ -1,311 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Tests for DeepSeek V3.2 parser engine semantics.
V3.2 uses the same DSML parameter format as V4 but wraps tool calls in
``<|DSML|function_calls>`` instead of ``<|DSML|tool_calls>`` and has
no reasoning (``<think>``/``</think>``) support.
"""
import json
import pytest
from tests.parser.engine.conftest import make_mock_tokenizer
from tests.parser.engine.streaming_helpers import (
collect_content,
collect_function_name,
collect_tool_arguments,
simulate_tool_streaming,
)
from vllm.parser.deepseek_v4 import (
DSML_INVOKE_END,
DSML_INVOKE_NAME_END,
DSML_INVOKE_PREFIX,
)
from vllm.parser.deepseek_v32 import (
DSML_FUNC_END,
DSML_FUNC_START,
DeepSeekV32Parser,
)
from vllm.parser.engine.parser_engine_config import ParserState
_PARAM_OPEN = '|DSML|parameter name="{name}" string="{is_str}">'
_PARAM_CLOSE = "</|DSML|parameter>"
def _param(name: str, is_str: str, value: str) -> str:
return f"<{_PARAM_OPEN.format(name=name, is_str=is_str)}{value}{_PARAM_CLOSE}"
def _invoke(name: str, *params: str) -> str:
body = "\n".join(params)
return (
f"{DSML_INVOKE_PREFIX}{name}{DSML_INVOKE_NAME_END}\n{body}\n{DSML_INVOKE_END}"
)
def _func_calls(*invocations: str) -> str:
body = "\n".join(invocations)
return f"{DSML_FUNC_START}\n{body}\n{DSML_FUNC_END}"
def _make_tool(name, properties):
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionToolsParam,
)
return ChatCompletionToolsParam(
type="function",
function={
"name": name,
"parameters": {
"type": "object",
"properties": properties,
},
},
)
@pytest.fixture
def mock_tokenizer():
return make_mock_tokenizer({})
@pytest.fixture
def mock_request():
from unittest.mock import MagicMock
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionRequest,
)
req = MagicMock(spec=ChatCompletionRequest)
req.tools = []
req.tool_choice = "auto"
return req
# ── Non-streaming extraction ────────────────────────────────────────
class TestNonStreaming:
def test_no_tool_call(self, mock_tokenizer, mock_request):
parser = DeepSeekV32Parser(mock_tokenizer)
result = parser.extract_tool_calls("Hello world", mock_request)
assert not result.tools_called
assert result.content == "Hello world"
def test_single_tool(self, mock_tokenizer, mock_request):
text = _func_calls(
_invoke("get_weather", _param("city", "true", "SF")),
)
parser = DeepSeekV32Parser(mock_tokenizer)
result = parser.extract_tool_calls(text, mock_request)
assert result.tools_called
assert len(result.tool_calls) == 1
assert result.tool_calls[0].function.name == "get_weather"
args = json.loads(result.tool_calls[0].function.arguments)
assert args == {"city": "SF"}
def test_parallel_tools(self, mock_tokenizer, mock_request):
text = _func_calls(
_invoke("get_weather", _param("city", "true", "SF")),
_invoke("get_weather", _param("city", "true", "NYC")),
)
parser = DeepSeekV32Parser(mock_tokenizer)
result = parser.extract_tool_calls(text, mock_request)
assert result.tools_called
assert len(result.tool_calls) == 2
assert json.loads(result.tool_calls[0].function.arguments) == {"city": "SF"}
assert json.loads(result.tool_calls[1].function.arguments) == {"city": "NYC"}
def test_content_before_tool_call(self, mock_tokenizer, mock_request):
text = "Let me check. " + _func_calls(
_invoke("search", _param("q", "true", "vllm")),
)
parser = DeepSeekV32Parser(mock_tokenizer)
result = parser.extract_tool_calls(text, mock_request)
assert result.tools_called
assert result.content is not None
assert "Let me check" in result.content
def test_non_string_params_json_parsed(self, mock_tokenizer, mock_request):
text = _func_calls(
_invoke(
"toggle",
_param("enabled", "false", "true"),
_param("count", "false", "42"),
),
)
parser = DeepSeekV32Parser(mock_tokenizer)
result = parser.extract_tool_calls(text, mock_request)
args = json.loads(result.tool_calls[0].function.arguments)
assert args["enabled"] is True
assert args["count"] == 42
def test_wrapper_unwrapping(self, mock_tokenizer, mock_request):
tool = _make_tool("get_weather", {"location": {"type": "string"}})
mock_request.tools = [tool]
text = _func_calls(
_invoke(
"get_weather",
_param("arguments", "false", '{"location":"Beijing"}'),
),
)
parser = DeepSeekV32Parser(mock_tokenizer, tools=[tool])
result = parser.extract_tool_calls(text, mock_request)
args = json.loads(result.tool_calls[0].function.arguments)
assert args == {"location": "Beijing"}
# ── Initial state ────────────────────────────────────────────────────
class TestInitialState:
def test_always_content(self, mock_tokenizer):
parser = DeepSeekV32Parser(mock_tokenizer)
cfg = parser.parser_engine_config
assert cfg.initial_state == ParserState.CONTENT
def test_ignores_thinking_kwargs(self, mock_tokenizer):
parser = DeepSeekV32Parser(
mock_tokenizer,
chat_template_kwargs={"thinking": True, "enable_thinking": True},
)
cfg = parser.parser_engine_config
assert cfg.initial_state == ParserState.CONTENT
# ── Streaming ────────────────────────────────────────────────────────
class TestStreaming:
def test_split_parameter_start_is_buffered(self, mock_tokenizer, mock_request):
chunks = [
DSML_FUNC_START,
f"{DSML_INVOKE_PREFIX}edit{DSML_INVOKE_NAME_END}",
f"<{_PARAM_OPEN.format(name='expr', is_str='true')}a<b><|DSML|parameter",
' name="date" string="true">tomorrow',
_PARAM_CLOSE,
DSML_INVOKE_END,
DSML_FUNC_END,
]
results = simulate_tool_streaming(
DeepSeekV32Parser(mock_tokenizer), mock_request, chunks
)
arguments = collect_tool_arguments(results)
assert "<|DSML|parameter" not in arguments
assert json.loads(arguments) == {"expr": "a<b>", "date": "tomorrow"}
def test_single_tool_streaming(self, mock_tokenizer, mock_request):
text = _func_calls(
_invoke("get_weather", _param("city", "true", "SF")),
)
parser = DeepSeekV32Parser(mock_tokenizer)
results = simulate_tool_streaming(parser, mock_request, list(text))
assert collect_function_name(results) == "get_weather"
args_json = collect_tool_arguments(results)
assert json.loads(args_json) == {"city": "SF"}
def test_content_before_tool_streaming(self, mock_tokenizer, mock_request):
text = "Checking... " + _func_calls(
_invoke("fn", _param("k", "true", "v")),
)
parser = DeepSeekV32Parser(mock_tokenizer)
results = simulate_tool_streaming(parser, mock_request, list(text))
content = collect_content(results)
assert "Checking" in content
def test_parallel_tools_streaming(self, mock_tokenizer, mock_request):
text = _func_calls(
_invoke("fn_a", _param("x", "true", "1")),
_invoke("fn_b", _param("y", "true", "2")),
)
parser = DeepSeekV32Parser(mock_tokenizer)
results = simulate_tool_streaming(parser, mock_request, list(text))
names = []
for delta, _ in results:
if delta and delta.tool_calls:
for tc in delta.tool_calls:
if tc.function and tc.function.name:
names.append(tc.function.name)
assert "fn_a" in names
assert "fn_b" in names
def test_no_tool_content_only(self, mock_tokenizer, mock_request):
text = "Just some text, no tools."
parser = DeepSeekV32Parser(mock_tokenizer)
results = simulate_tool_streaming(parser, mock_request, list(text))
content = collect_content(results)
assert "Just some text" in content
args = collect_tool_arguments(results)
assert args == ""
def test_streaming_wrapper_unwrap_consistency(self, mock_tokenizer, mock_request):
tool = _make_tool("get_weather", {"location": {"type": "string"}})
mock_request.tools = [tool]
parser = DeepSeekV32Parser(mock_tokenizer, tools=[tool])
chunks = [
DSML_FUNC_START,
_invoke(
"get_weather",
_param("arguments", "false", '{"location": "NYC"}'),
),
DSML_FUNC_END,
]
results = simulate_tool_streaming(parser, mock_request, chunks)
streamed_args = collect_tool_arguments(results)
final_delta, _ = results[-1]
finish_delta = parser.finish_streaming()
extracted = parser._build_extracted_result(final_delta, finish_delta)
assert extracted.tools_called is True
assert len(extracted.tool_calls) == 1
final_args = extracted.tool_calls[0].function.arguments
assert json.loads(final_args) == {"location": "NYC"}
assert '"arguments"' not in streamed_args
assert final_args.startswith(streamed_args)
def test_missing_invoke_end(self, mock_tokenizer, mock_request):
text = (
f"{DSML_FUNC_START}\n"
f"{DSML_INVOKE_PREFIX}fn{DSML_INVOKE_NAME_END}\n"
f"{_param('k', 'true', 'v')}\n"
f"{DSML_FUNC_END}"
)
parser = DeepSeekV32Parser(mock_tokenizer)
results = simulate_tool_streaming(parser, mock_request, list(text))
assert collect_function_name(results) == "fn"
args = json.loads(collect_tool_arguments(results))
assert args == {"k": "v"}
class TestMissingFunctionCallsWrapper:
"""An invoke without the ``<|DSML|function_calls>`` wrapper is still a
tool call (V3.2 counterpart of #48931)."""
def test_non_streaming(self, mock_tokenizer, mock_request):
text = _invoke("fn", _param("k", "true", "v"))
parser = DeepSeekV32Parser(mock_tokenizer)
result = parser.extract_tool_calls(text, mock_request)
assert result.tools_called is True
assert result.tool_calls[0].function.name == "fn"
assert json.loads(result.tool_calls[0].function.arguments) == {"k": "v"}
assert result.content is None
def test_streaming(self, mock_tokenizer, mock_request):
text = "Sure.\n" + _invoke("fn", _param("k", "true", "v"))
parser = DeepSeekV32Parser(mock_tokenizer)
results = simulate_tool_streaming(parser, mock_request, list(text))
assert collect_function_name(results) == "fn"
assert json.loads(collect_tool_arguments(results)) == {"k": "v"}
assert "DSML" not in collect_content(results)
File diff suppressed because it is too large Load Diff
-148
View File
@@ -1,148 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import json
import pytest
from tests.parser.engine.replay_harness import (
DUMMY_TOOLS,
MockTokenizer,
_test_request,
collect_output,
replay_streaming,
)
from vllm.parser.deepseek_v41 import deepseek_v41_config
from vllm.parser.parser_manager import ParserManager
CALLS = (
'\n\n<|DSML| calls>\n<|DSML| invoke name="get_weather">\n'
'<|DSML| parameter name="city" string="true">杭州</|DSML| parameter>\n'
'<|DSML| parameter name="count" string="false">42</|DSML| parameter>\n'
'</|DSML| invoke>\n<|DSML| invoke name="add">\n'
'<|DSML| parameter name="x" string="false">1.5</|DSML| parameter>\n'
'<|DSML| parameter name="y" string="false">2.25</|DSML| parameter>\n'
"</|DSML| invoke>\n</|DSML| calls>"
)
def tokenizer_for(text, special_calls):
vocab = {"<think>": 50, "</think>": 51}
if special_calls:
vocab |= {"<|DSML| calls>": 52, "</|DSML| calls>": 53}
tokens: list[tuple[int, str]] = []
while text:
special = next((marker for marker in vocab if text.startswith(marker)), None)
piece = special or text[0]
tokens.append((vocab[piece] if special else 100 + len(tokens), piece))
text = text[len(piece) :]
return MockTokenizer(vocab, tokens), tokens
def parser_for(tokenizer, controls):
cls = ParserManager.get_parser(
tool_parser_name="deepseek_v41",
reasoning_parser_name="deepseek_v41",
enable_auto_tools=True,
)
return cls(tokenizer, chat_template_kwargs=controls)
@pytest.mark.parametrize("thinking", [False, True])
@pytest.mark.parametrize("special_calls", [False, True])
@pytest.mark.parametrize("chunk_size", [1, 7, 10000])
def test_registered_adapters_parse_parallel_calls_across_chunks(
thinking,
special_calls,
chunk_size,
):
text = ("Plan.</think>" if thinking else "") + "Checking." + CALLS
tokenizer, tokens = tokenizer_for(text, special_calls)
parser = parser_for(tokenizer, {"thinking": thinking})
output = collect_output(
replay_streaming(
parser,
tokens,
chunk_size=chunk_size,
finished_on_last=True,
tools=DUMMY_TOOLS,
prompt_token_ids=[50 if thinking else 51],
)
)
assert output.reasoning == ("Plan." if thinking else "")
assert output.content.strip() == "Checking."
assert [call["name"] for call in output.tool_calls] == ["get_weather", "add"]
assert [json.loads(call["arguments"]) for call in output.tool_calls] == [
{"city": "杭州", "count": 42},
{"x": 1.5, "y": 2.25},
]
@pytest.mark.parametrize("thinking", [False, True])
def test_registered_adapters_parse_complete_output(thinking):
text = ("Plan.</think>" if thinking else "") + CALLS
tokenizer, tokens = tokenizer_for(text, False)
parser = parser_for(tokenizer, {"thinking": thinking})
reasoning, content, calls = parser.parse(
text,
_test_request(tools=DUMMY_TOOLS),
enable_auto_tools=True,
model_output_token_ids=[tid for tid, _ in tokens],
)
assert (reasoning or "") == ("Plan." if thinking else "")
assert not (content or "").strip()
assert [call.name for call in calls] == ["get_weather", "add"]
assert json.loads(calls[0].arguments) == {"city": "杭州", "count": 42}
@pytest.mark.parametrize(
("controls", "text", "expected_reasoning", "reasoning_tokens"),
[
({"thinking": False}, "12", "", 0),
({"enable_thinking": False}, "12", "", 0),
({"thinking": True, "reasoning_effort": "none"}, "12", "", 0),
({}, "</think>12", "", 0),
({}, "Plan.</think>12", "Plan.", 5),
],
)
def test_reasoning_adapter_controls_and_usage(
controls,
text,
expected_reasoning,
reasoning_tokens,
):
tokenizer, tokens = tokenizer_for(text, False)
parser = parser_for(tokenizer, controls)
output = collect_output(
replay_streaming(
parser,
tokens,
chunk_size=1,
finished_on_last=True,
)
)
assert output.reasoning == expected_reasoning
assert output.content == "12"
assert (
parser.reasoning_parser.count_reasoning_tokens([tid for tid, _ in tokens])
== reasoning_tokens
)
assert parser.is_reasoning_end([50, 100, 51])
assert not parser.is_reasoning_end([51, 100, 50])
def test_python_argument_conversion_and_partial_values():
converter = deepseek_v41_config().arg_converter
raw = (
'<|DSML| parameter name="object" string="false">'
'{"a": [true, null]}</|DSML| parameter>'
'<|DSML| parameter name="bad" string="false">[broken</|DSML| parameter>'
'<|DSML| parameter name="bare">7</|DSML| parameter>'
'<|DSML| parameter name="text" string="true"> a<b'
)
assert json.loads(converter(raw, True)) == {
"object": {"a": [True, None]},
"bad": "[broken",
"bare": 7,
"text": " a<b",
}
File diff suppressed because it is too large Load Diff
-230
View File
@@ -1,230 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Reasoning-boundary tests for the GLM-4.7 parser.
GLM can transition straight from reasoning into a tool call without ever
emitting ``</think>``, so ``<tool_call>`` is an implicit reasoning
terminator. These cover that path plus the multi-turn prompts where an
earlier turn's markers must not be read as the current turn's state. The
streaming tests check that long tool arguments stream the same JSON as a
non-streaming parse, without converting the whole argument on every delta.
"""
import dataclasses
import pytest
from tests.parser.engine.conftest import make_mock_tokenizer
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionToolsParam,
)
from vllm.parser.glm47_moe import (
THINK_END,
THINK_START,
TOOL_CALL_END,
TOOL_CALL_START,
Glm47MoeParser,
_glm47_arg_value_extender,
glm47_moe_config,
)
THINK_S, THINK_E = 1000, 1001
TOOL_S, TOOL_E = 1002, 1003
ASSISTANT, OBSERVATION, USER = 1004, 1005, 1006
TEXT = 42 # stand-in for an ordinary reasoning/content token
VOCAB = {
THINK_START: THINK_S,
THINK_END: THINK_E,
TOOL_CALL_START: TOOL_S,
TOOL_CALL_END: TOOL_E,
"<|assistant|>": ASSISTANT,
"<|observation|>": OBSERVATION,
"<|user|>": USER,
}
@pytest.fixture
def parser():
return Glm47MoeParser(make_mock_tokenizer(VOCAB))
@pytest.fixture
def no_thinking_parser():
return Glm47MoeParser(
make_mock_tokenizer(VOCAB),
chat_template_kwargs={"thinking": False},
)
class TestIsReasoningEnd:
def test_open_reasoning(self, parser):
assert not parser.is_reasoning_end([THINK_S, TEXT])
def test_think_end(self, parser):
assert parser.is_reasoning_end([THINK_S, TEXT, THINK_E, TEXT])
def test_tool_call_without_think_end(self, parser):
"""Reasoning -> tool call with no ``</think>`` still ends reasoning."""
assert parser.is_reasoning_end([THINK_S, TEXT, TOOL_S, TEXT])
def test_previous_turn_tool_call_ignored(self, parser):
"""A finished tool call from an earlier turn says nothing about the
turn currently being generated."""
prompt = [THINK_S, TEXT, TOOL_S, TEXT, TOOL_E, OBSERVATION, TEXT, ASSISTANT]
assert not parser.is_reasoning_end(prompt)
def test_previous_turn_think_end_ignored(self, parser):
prompt = [THINK_S, TEXT, THINK_E, TEXT, USER, TEXT, ASSISTANT]
assert not parser.is_reasoning_end(prompt)
def test_empty_input(self, parser):
assert not parser.is_reasoning_end([])
def test_thinking_disabled(self, no_thinking_parser):
assert no_thinking_parser.is_reasoning_end([THINK_S, TEXT])
class TestExtractContentIds:
def test_think_end_wins_over_later_tool_call(self, parser):
"""Content between ``</think>`` and a tool call must survive."""
ids = [THINK_S, TEXT, THINK_E, 20, 21, TOOL_S, 30, TOOL_E]
assert parser.extract_content_ids(ids) == [20, 21, TOOL_S, 30, TOOL_E]
def test_every_tool_call_after_think_end_kept(self, parser):
ids = [THINK_E, 20, TOOL_S, 30, TOOL_E, 21, TOOL_S, 31, TOOL_E]
assert parser.extract_content_ids(ids) == ids[1:]
def test_falls_back_to_tool_call(self, parser):
"""Without ``</think>``, content starts at the opener itself so the
tool parser still receives a well-formed call."""
ids = [THINK_S, TEXT, TOOL_S, 30, TOOL_E]
assert parser.extract_content_ids(ids) == [TOOL_S, 30, TOOL_E]
def test_falls_back_to_first_tool_call_of_turn(self, parser):
ids = [THINK_S, TEXT, TOOL_S, 30, TOOL_E, TOOL_S, 31, TOOL_E]
assert parser.extract_content_ids(ids) == ids[2:]
def test_previous_turn_tool_call_ignored(self, parser):
ids = [TOOL_S, 30, TOOL_E, OBSERVATION, TEXT, ASSISTANT, THINK_S, TEXT]
assert parser.extract_content_ids(ids) == ids
def test_no_markers_returns_input_ids(self, parser):
assert parser.extract_content_ids([20, 21]) == [20, 21]
def test_thinking_disabled(self, no_thinking_parser):
ids = [THINK_S, TEXT, TOOL_S]
assert no_thinking_parser.extract_content_ids(ids) == ids
WRITE_TOOL = ChatCompletionToolsParam(
type="function",
function={
"name": "Write",
"parameters": {
"type": "object",
"properties": {
"file_path": {"type": "string"},
"line_count": {"type": "integer"},
"content": {"type": "string"},
},
},
},
)
CALL_HEAD = (
f"{THINK_END}{TOOL_CALL_START}Write\n"
"<arg_key>file_path</arg_key><arg_value>/repo/app.py</arg_value>\n"
"<arg_key>line_count</arg_key><arg_value>120</arg_value>\n"
"<arg_key>content</arg_key><arg_value>"
)
CALL_TAIL = f"</arg_value>\n{TOOL_CALL_END}"
# Escapes, non-ASCII, and tag-like text that must stay inside the value.
CONTENT_LINE = 'if a < b > c: print("x\\y") # </div> </arg_ <arg_key>\t\u00e9\n'
def _pieces(text: str, size: int) -> list[str]:
return [text[i : i + size] for i in range(0, len(text), size)]
def _stream_arguments(parser, request, chunks: list[str]) -> str:
args = []
for i, chunk in enumerate(chunks):
delta = parser.parse_delta(chunk, [], request, finished=i == len(chunks) - 1)
for tool_call in (delta and delta.tool_calls) or []:
if tool_call.function and tool_call.function.arguments:
args.append(tool_call.function.arguments)
return "".join(args)
class TestStreamedToolArguments:
@pytest.mark.parametrize(
"tools, line_count",
[([], '"120"'), ([WRITE_TOOL], "120")],
ids=["no_schema", "schema"],
)
def test_long_call_streams_non_streaming_arguments(
self, mock_request, tools, line_count
):
mock_request.tools = tools
text = CALL_HEAD + CONTENT_LINE * 400 + CALL_TAIL
streamed = _stream_arguments(
Glm47MoeParser(make_mock_tokenizer(VOCAB)), mock_request, _pieces(text, 7)
)
result = Glm47MoeParser(make_mock_tokenizer(VOCAB)).extract_tool_calls(
text, mock_request
)
assert streamed == result.tool_calls[0].function.arguments
assert f'"line_count": {line_count},' in streamed
def test_conversions_do_not_grow_with_argument_length(self, mock_request):
"""The open value streams without re-running the converter."""
mock_request.tools = [WRITE_TOOL]
def streaming_conversions(lines: int) -> int:
calls = 0
base = glm47_moe_config()
def counting_converter(raw_args: str, partial: bool) -> str:
nonlocal calls
if partial:
calls += 1
return base.arg_converter(raw_args, partial)
parser = Glm47MoeParser(
make_mock_tokenizer(VOCAB),
parser_engine_config=dataclasses.replace(
base, arg_converter=counting_converter
),
)
chunks = [
*_pieces(CALL_HEAD, 4),
*_pieces(CONTENT_LINE * lines, 4),
*_pieces(CALL_TAIL, 4),
]
_stream_arguments(parser, mock_request, chunks)
return calls
assert streaming_conversions(lines=40) == streaming_conversions(lines=400)
def test_value_extender_stops_at_split_value_end(self):
extend = _glm47_arg_value_extender("<arg_key>k</arg_key><arg_value>x")
assert [extend(d) for d in ['a"\n', "</arg_", "value>"]] == [
'a\\"\\n',
"</arg_",
None,
]
@pytest.mark.parametrize(
"raw_args",
[
"<arg_key>k</arg_key><arg_value>x</arg_value>",
"<arg_key>k</arg_key><arg_value>1</arg_value>"
"<arg_key>j</arg_key><arg_value>2</arg_value>"
"<arg_key>k</arg_key><arg_value>3",
],
ids=["closed_value", "repeated_key_not_last"],
)
def test_no_value_extender_unless_open_value_is_last(self, raw_args):
assert _glm47_arg_value_extender(raw_args) is None
-234
View File
@@ -1,234 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Tests for the engine-based Granite parser.
Granite emits tool calls as a JSON array following a marker
(``<|tool_call|>`` for 3.0, ``<tool_call>`` for 3.1) with no closing
terminal; the ``]`` ends the region and surrounding prose is content. The
engine's ``tool_call_body_array`` mode splits the array into one call per
element and ``_granite_arg_converter`` carves each element's ``arguments``.
"""
import json
import pytest
from tests.parser.engine.conftest import make_mock_tokenizer
from tests.parser.engine.trace_builder import _GRANITE_SCENARIOS, _build_granite
from vllm.parser.granite import GraniteParser, _granite_arg_converter
TOOL_TOKEN = "<|tool_call|>"
TOOL_STRING = "<tool_call>"
_GRANITE_VOCAB = {TOOL_TOKEN: 49154}
@pytest.fixture
def mock_tokenizer():
return make_mock_tokenizer(_GRANITE_VOCAB)
@pytest.fixture
def parser(mock_tokenizer):
return GraniteParser(mock_tokenizer)
def _tokenize(text: str) -> list[tuple[int, str]]:
"""Tokenize like the real stream: ``<|tool_call|>`` is one special token,
plain text becomes one token per character."""
tokens: list[tuple[int, str]] = []
i = 0
while i < len(text):
if text.startswith(TOOL_TOKEN, i):
tokens.append((_GRANITE_VOCAB[TOOL_TOKEN], TOOL_TOKEN))
i += len(TOOL_TOKEN)
else:
tokens.append((ord(text[i]), text[i]))
i += 1
return tokens
def _stream(parser, request, text: str, chunk_size: int):
tokens = _tokenize(text)
results = []
previous_text = ""
previous_token_ids: list[int] = []
for start in range(0, len(tokens), chunk_size):
batch = tokens[start : start + chunk_size]
delta_text = "".join(t for _, t in batch)
delta_token_ids = [tid for tid, _ in batch]
current_text = previous_text + delta_text
current_token_ids = previous_token_ids + delta_token_ids
delta = parser.extract_tool_calls_streaming(
previous_text=previous_text,
current_text=current_text,
delta_text=delta_text,
previous_token_ids=tuple(previous_token_ids),
current_token_ids=tuple(current_token_ids),
delta_token_ids=tuple(delta_token_ids),
request=request,
)
results.append(delta)
previous_text = current_text
previous_token_ids = current_token_ids
finish = parser.finish_streaming()
if finish is not None:
results.append(finish)
return results
def _collect_content(results) -> str:
return "".join(d.content for d in results if d and d.content)
def _collect_names(results) -> list[str]:
names: dict[int, str] = {}
for d in results:
if not (d and d.tool_calls):
continue
for tc in d.tool_calls:
if tc.function and tc.function.name:
names[tc.index] = names.get(tc.index, "") + tc.function.name
return [names[i] for i in sorted(names)]
def _collect_args(results) -> dict[int, str]:
args: dict[int, str] = {}
for d in results:
if not (d and d.tool_calls):
continue
for tc in d.tool_calls:
if tc.function and tc.function.arguments:
args[tc.index] = args.get(tc.index, "") + tc.function.arguments
return args
class TestArgConverter:
def test_complete_wrapper(self):
raw = '{"name": "get_weather", "arguments": {"city": "SF"}}'
assert _granite_arg_converter(raw, False) == '{"city": "SF"}'
def test_arguments_before_name(self):
raw = '{"arguments": {"city": "SF"}, "name": "get_weather"}'
assert _granite_arg_converter(raw, False) == '{"city": "SF"}'
def test_partial_before_arguments(self):
assert _granite_arg_converter('{"name": "get_w', True) == ""
def test_partial_inside_arguments(self):
raw = '{"name": "x", "arguments": {"a": 1'
assert _granite_arg_converter(raw, True) == '{"a": 1'
def test_prefix_stability(self):
# Each growing prefix must extend the previous converter output.
full = '{"name": "x", "arguments": {"city": "Tokyo"}}'
prev = ""
for i in range(1, len(full) + 1):
out = _granite_arg_converter(full[:i], True)
assert out.startswith(prev) or prev.startswith(out) or out == ""
if out:
prev = out
def test_missing_arguments_defaults(self):
assert _granite_arg_converter('{"name": "x"}', False) == "{}"
assert _granite_arg_converter('{"name": "x"}', True) == ""
def test_non_object_arguments_rejected(self):
with pytest.raises(ValueError, match="JSON object"):
_granite_arg_converter('{"name": "x", "arguments": [1]}', False)
class TestNonStreaming:
def test_plain_text(self, parser, mock_request):
reasoning, content, tools = parser.parse("Just a reply.", mock_request)
assert reasoning is None
assert content == "Just a reply."
assert tools is None
def test_single_tool_token_marker(self, parser, mock_request):
text = (
f'{TOOL_TOKEN} [{{"name": "get_weather", "arguments": {{"city": "SF"}}}}]'
)
_, content, tools = parser.parse(text, mock_request)
assert content is None
assert [t.name for t in tools] == ["get_weather"]
assert json.loads(tools[0].arguments) == {"city": "SF"}
def test_single_tool_string_marker(self, parser, mock_request):
# Granite 3.1 uses the plain-text ``<tool_call>`` marker.
text = f'{TOOL_STRING} [{{"name": "get_time", "arguments": {{}}}}]'
_, content, tools = parser.parse(text, mock_request)
assert [t.name for t in tools] == ["get_time"]
assert json.loads(tools[0].arguments) == {}
def test_parallel_calls_one_array(self, parser, mock_request):
text = (
f'{TOOL_TOKEN} [{{"name": "a", "arguments": {{"x": 1}}}}, '
f'{{"name": "b", "arguments": {{"y": [1, 2]}}}}]'
)
_, _, tools = parser.parse(text, mock_request)
assert [t.name for t in tools] == ["a", "b"]
assert json.loads(tools[0].arguments) == {"x": 1}
assert json.loads(tools[1].arguments) == {"y": [1, 2]}
def test_surrounding_text_is_content(self, parser, mock_request):
text = (
"Let me check.\n"
f'{TOOL_TOKEN} [{{"name": "get_weather", "arguments": {{"city": "SF"}}}}]'
)
_, content, tools = parser.parse(text, mock_request)
assert content == "Let me check."
assert [t.name for t in tools] == ["get_weather"]
def test_arguments_before_name(self, parser, mock_request):
text = (
f'{TOOL_TOKEN} [{{"arguments": {{"city": "SF"}}, "name": "get_weather"}}]'
)
_, _, tools = parser.parse(text, mock_request)
assert [t.name for t in tools] == ["get_weather"]
assert json.loads(tools[0].arguments) == {"city": "SF"}
def test_marker_without_array_is_not_a_tool_call(self, parser, mock_request):
# A non-array body after the marker (malformed) yields no tool call.
text = f'{TOOL_TOKEN} {{"name": "func", "arguments": {{}}}}'
_, _, tools = parser.parse(text, mock_request)
assert not tools
class TestStreaming:
@pytest.mark.parametrize("chunk_size", [1, 2, 3, 5, 11])
def test_parallel_calls_chunk_invariance(self, parser, mock_request, chunk_size):
text = (
f'{TOOL_TOKEN} [{{"name": "get_weather", '
f'"arguments": {{"city": "Tokyo"}}}}, '
f'{{"name": "get_time", "arguments": {{"timezone": "Asia/Tokyo"}}}}]'
)
results = _stream(parser, mock_request, text, chunk_size)
assert _collect_names(results) == ["get_weather", "get_time"]
args = _collect_args(results)
assert json.loads(args[0]) == {"city": "Tokyo"}
assert json.loads(args[1]) == {"timezone": "Asia/Tokyo"}
@pytest.mark.parametrize("chunk_size", [1, 3, 7])
def test_surrounding_text_streaming(self, parser, mock_request, chunk_size):
text = (
"Let me check.\n"
f'{TOOL_TOKEN} [{{"name": "get_weather", "arguments": {{"city": "SF"}}}}]'
)
results = _stream(parser, mock_request, text, chunk_size)
assert _collect_names(results) == ["get_weather"]
assert "Let me check." in _collect_content(results)
def test_incomplete_array_at_eos(self, parser, mock_request):
# Truncated mid-element must not raise and must not leak the marker.
text = f'{TOOL_TOKEN} [{{"name": "func", "arguments": {{"a": 1'
results = _stream(parser, mock_request, text, 3)
assert TOOL_TOKEN not in _collect_content(results)
class TestReplay:
@pytest.mark.parametrize("scenario", _GRANITE_SCENARIOS, ids=lambda s: s.id)
def test_replay_scenarios(self, scenario):
# ``_build_granite`` replays the sample through GraniteParser at
# chunk_size=1 and asserts reasoning/content/tool_calls (validate=True).
_build_granite(scenario)
File diff suppressed because it is too large Load Diff
-106
View File
@@ -1,106 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import json
import pytest
from tests.parser.engine.replay_harness import MockTokenizer, _test_request
from vllm.reasoning import ReasoningParserManager
from vllm.tool_parsers import ToolParserManager
def _tokenizer() -> MockTokenizer:
vocab = {
"<think>": 1,
"</think>": 2,
"<tool_call>": 3,
"</tool_call>": 4,
"<arg_key>": 5,
"</arg_key>": 6,
"<arg_value>": 7,
"</arg_value>": 8,
}
return MockTokenizer(vocab=vocab, tokens=[])
def test_ling3_registered():
assert ReasoningParserManager.get_reasoning_parser("ling3") is not None
assert ToolParserManager.get_tool_parser("ling3") is not None
def test_ling3_defaults_thinking_on():
parser_cls = ReasoningParserManager.get_reasoning_parser("ling3")
parser = parser_cls(_tokenizer())
assert parser.reasoning_start_str == "<think>"
assert parser.reasoning_end_str == "</think>"
reasoning, content = parser.extract_reasoning(
"<think>reason</think>answer", _test_request()
)
assert reasoning == "reason"
assert content == "answer"
def test_ling3_disable_thinking_keeps_reasoning_as_content():
parser_cls = ReasoningParserManager.get_reasoning_parser("ling3")
parser = parser_cls(
_tokenizer(),
chat_template_kwargs={"enable_thinking": False},
)
reasoning, content = parser.extract_reasoning(
"<think>reason</think>answer", _test_request()
)
assert reasoning is None
assert content == "<think>reason</think>answer"
@pytest.mark.parametrize("model_output", ["only reasoning", "<think>only reasoning"])
def test_ling3_truncated_reasoning_stays_reasoning(model_output):
parser_cls = ReasoningParserManager.get_reasoning_parser("ling3")
parser = parser_cls(
_tokenizer(),
chat_template_kwargs={"enable_thinking": True},
)
reasoning, content = parser.extract_reasoning(model_output, _test_request())
assert reasoning == "only reasoning"
assert content is None
def test_ling3_tool_call_without_newline():
parser_cls = ToolParserManager.get_tool_parser("ling3")
parser = parser_cls(_tokenizer())
result = parser.extract_tool_calls(
"<tool_call>get_weather<arg_key>city</arg_key>"
"<arg_value>Beijing</arg_value></tool_call>",
request=_test_request(),
)
assert result.tools_called
assert result.content is None
assert result.tool_calls[0].function.name == "get_weather"
assert json.loads(result.tool_calls[0].function.arguments) == {
"city": "Beijing",
}
def test_ling3_tool_call_without_args():
parser_cls = ToolParserManager.get_tool_parser("ling3")
parser = parser_cls(_tokenizer())
result = parser.extract_tool_calls(
"<tool_call>ping</tool_call>",
request=_test_request(),
)
assert result.tools_called
assert result.content is None
assert result.tool_calls[0].function.name == "ping"
assert json.loads(result.tool_calls[0].function.arguments) == {}
@@ -1,459 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Tests for the MiniCPM-V family parser."""
import pytest
from tests.parser.engine.conftest import make_mock_tokenizer
from tests.parser.engine.streaming_helpers import simulate_reasoning_streaming
from vllm.entrypoints.generate.base.protocol import (
DeltaFunctionCall,
DeltaMessage,
DeltaToolCall,
)
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionRequest,
ChatCompletionToolsParam,
)
from vllm.parser.minicpmv import (
EscapedNewlineNormalizer,
MiniCPMVOutputNormalizer,
recover_newlines,
)
from vllm.parser.parser_manager import ParserManager
from vllm.reasoning import ReasoningParserManager
from vllm.tool_parsers import ToolParserManager
_VOCAB = {
"<think>": 10,
"</think>": 11,
"<|im_end|>": 12,
"<tool_call>": 13,
"</tool_call>": 14,
}
_TOOLS = [
ChatCompletionToolsParam(
type="function",
function={
"name": "get_weather",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
},
},
)
]
@pytest.fixture
def mock_tokenizer():
return make_mock_tokenizer(_VOCAB)
@pytest.fixture
def request_obj():
return ChatCompletionRequest(
model="test-model",
messages=[{"role": "user", "content": "hi"}],
)
def _make_parser(tokenizer, *, enable_thinking=None, tools=None):
"""Build the parser the serving stack resolves for MiniCPM-V.
Everything is built through ``ParserManager`` so the tests exercise the
``DelegatingParser`` the server actually drives, not the parser engine on
its own. Tool calling goes through ``qwen3_coder``: MiniCPM-V emits that
XML syntax verbatim and has no tool parser of its own.
"""
parser_cls = ParserManager.get_parser(
tool_parser_name="qwen3_coder" if tools else None,
reasoning_parser_name="minicpmv",
enable_auto_tools=bool(tools),
)
assert parser_cls is not None
kwargs = {}
if enable_thinking is not None:
kwargs["chat_template_kwargs"] = {"enable_thinking": enable_thinking}
return parser_cls(tokenizer, tools, **kwargs)
def test_reasoning_parser_is_registered():
parser_cls = ReasoningParserManager.get_reasoning_parser("minicpmv")
assert parser_cls.__name__ == "MiniCPMVParserReasoningAdapter"
def test_thinking_defaults_to_the_chat_template(mock_tokenizer):
"""Without `enable_thinking`, the grammar must wait for reasoning.
The chat template ends the assistant turn with `<think>` unless the flag is
explicitly false, so the model reasons by default. This is the flag
`serving.py` latches into the structured-output gate, so a non-thinking
default both disables the grammar and leaks the thinking into `content`.
"""
def reasoning_pending(**kwargs):
return not _make_parser(mock_tokenizer, **kwargs).is_reasoning_end([1, 2])
assert reasoning_pending() is True # default matches the template
assert reasoning_pending(enable_thinking=True) is True
assert reasoning_pending(enable_thinking=False) is False
def test_no_minicpmv_tool_parser_is_registered():
"""MiniCPM-V deliberately registers no tool parser.
The family emits the Qwen3-Coder XML syntax verbatim, so
``--tool-call-parser qwen3_coder`` already covers it. Registering a
``minicpmv`` tool parser would duplicate that name for no parsing gain.
"""
with pytest.raises(KeyError):
ToolParserManager.get_tool_parser("minicpmv")
def test_composes_with_qwen3_coder_tool_parser(mock_tokenizer):
"""`--tool-call-parser qwen3_coder` must work through the serving Parser."""
request = ChatCompletionRequest(
model="test-model",
messages=[{"role": "user", "content": "hi"}],
tools=_TOOLS,
)
parser_cls = ParserManager.get_parser(
tool_parser_name="qwen3_coder",
reasoning_parser_name="minicpmv",
enable_auto_tools=True,
)
assert parser_cls is not None
parser = parser_cls(mock_tokenizer, request.tools)
_, _, tool_calls = parser.parse(
"<tool_call><function=get_weather>"
"<parameter=city>SF</parameter></function></tool_call>",
request,
enable_auto_tools=True,
)
assert tool_calls is not None
assert tool_calls[0].name == "get_weather"
assert "SF" in tool_calls[0].arguments
class TestNonThinking:
@pytest.fixture
def parser(self, mock_tokenizer):
return _make_parser(mock_tokenizer, enable_thinking=False)
def test_plain_content_is_unchanged(self, parser):
reasoning, content = parser.extract_reasoning("The answer is 42.", None)
assert reasoning is None
assert content == "The answer is 42."
def test_reserved_markers_are_removed(self, parser):
output = (
"<reserved_12>reasoning<reserved_13><|reserved_14|>tool call<|reserved_15|>"
)
reasoning, content = parser.extract_reasoning(output, None)
assert reasoning is None
assert content == "reasoningtool call"
def test_unrelated_reserved_text_is_unchanged(self, parser):
reasoning, content = parser.extract_reasoning(
"<reserved_11>content<|reserved_16|>",
None,
)
assert reasoning is None
assert content == "<reserved_11>content<|reserved_16|>"
def test_dangling_standard_think_end_is_removed(self, parser):
output = "first part </think>\n\nfinal part"
reasoning, content = parser.extract_reasoning(output, None)
assert reasoning is None
assert content == "first part \n\nfinal part"
def test_truncated_standard_reasoning_does_not_leak(self, parser):
reasoning, content = parser.extract_reasoning(
"prefix<think>private reasoning",
None,
)
assert reasoning is None
assert content == "prefix"
def test_streaming_split_standard_markers(self, parser):
reasoning, content = simulate_reasoning_streaming(
parser,
[
"<",
"think>",
"private reasoning",
"</",
"think>",
"final answer",
],
)
assert reasoning == ""
assert content == "final answer"
def test_streaming_split_reserved_markers(self, parser):
reasoning, content = simulate_reasoning_streaming(
parser,
[
"<reser",
"ved_12>",
"reasoning",
"<|reserved",
"_13|>",
"final answer",
],
)
assert reasoning == ""
assert content == "reasoningfinal answer"
def test_tool_calls_are_parsed(self, mock_tokenizer):
"""Thinking is off by default, and tool calls must still be parsed."""
parser = _make_parser(mock_tokenizer, enable_thinking=False, tools=_TOOLS)
request = ChatCompletionRequest(
model="test-model",
messages=[{"role": "user", "content": "hi"}],
tools=_TOOLS,
)
reasoning, content, tool_calls = parser.parse(
"<tool_call><function=get_weather>"
"<parameter=city>SF</parameter></function></tool_call>",
request,
enable_auto_tools=True,
)
assert reasoning is None
assert not content
assert tool_calls is not None
assert tool_calls[0].name == "get_weather"
assert "SF" in tool_calls[0].arguments
def test_streaming_tool_calls_are_parsed(self, mock_tokenizer):
"""Regression: tool calls must stream when thinking is explicitly off.
The reasoning parser used to report ``reasoning_ended=False`` in
non-thinking mode, which kept the delegating parser in its reasoning
phase and streamed the raw ``<tool_call>`` XML to the client as content.
"""
parser = _make_parser(mock_tokenizer, enable_thinking=False, tools=_TOOLS)
request = ChatCompletionRequest(
model="test-model",
messages=[{"role": "user", "content": "hi"}],
tools=_TOOLS,
)
delta = parser.parse_delta(
"<tool_call><function=get_weather>"
"<parameter=city>SF</parameter></function></tool_call>",
[],
request,
prompt_token_ids=[],
finished=True,
)
assert delta is not None
assert delta.tool_calls
assert delta.tool_calls[0].function is not None
assert delta.tool_calls[0].function.name == "get_weather"
assert "SF" in (delta.tool_calls[0].function.arguments or "")
assert not delta.content
class TestThinking:
def test_standard_think_tags_are_parsed(self, mock_tokenizer):
parser = _make_parser(mock_tokenizer, enable_thinking=True)
reasoning, content = parser.extract_reasoning(
"<think>private reasoning</think>final answer",
None,
)
assert reasoning == "private reasoning"
assert content == "final answer"
def test_reserved_markers_are_removed(self, mock_tokenizer):
parser = _make_parser(mock_tokenizer, enable_thinking=True)
reasoning, content = parser.extract_reasoning(
"<think><reserved_12>private reasoning</think><|reserved_13|>final answer",
None,
)
assert reasoning == "private reasoning"
assert content == "final answer"
def test_streaming_im_end_is_removed(self):
tokenizer = make_mock_tokenizer(_VOCAB, special_tokens=[])
parser = _make_parser(tokenizer, enable_thinking=True)
reasoning, content = simulate_reasoning_streaming(
parser,
["Answer: 111", "<|im_end|>"],
)
assert reasoning == "Answer: 111"
assert content == ""
class TestNewlineRecovery:
"""The tokenizer writes a newline as the two characters ``\\`` and ``n``."""
def test_recover_newlines(self):
text = (
"first\\nsecond\\rthird\\r\\nfourth "
"`code\\nvalue` "
"```python\\nvalue = '\\\\n'\\n``` "
"$x\\ny$ $$x\\ny$$ \\(x\\ny\\) \\[x\\ny\\] "
"escaped \\\\n"
)
assert recover_newlines(text) == (
"first\nsecond\nthird\nfourth "
"`code\\nvalue` "
"```python\\nvalue = '\\\\n'\\n``` "
"$x\\ny$ $$x\\ny$$ \\(x\\ny\\) \\[x\\ny\\] "
"escaped \\\\n"
)
def test_streaming_matches_complete_recovery(self):
text = (
"first\\nsecond `code\\nvalue` $x\\ny$ ```text\\nvalue\\n``` last\\r\\nline"
)
expected = recover_newlines(text)
for split in range(len(text) + 1):
normalizer = EscapedNewlineNormalizer()
actual = normalizer.feed(text[:split])
actual += normalizer.feed(text[split:], final=True)
assert actual == expected
normalizer = EscapedNewlineNormalizer()
actual = "".join(normalizer.feed(char) for char in text)
actual += normalizer.feed("", final=True)
assert actual == expected
def test_backslash_runs_stay_consistent_across_splits(self):
# A run of backslashes can be split over chunks. Its parity decides
# whether a following `n` is a newline, so streaming and non-streaming
# must agree for every split point.
for count in range(1, 7):
text = "\\" * count + "n"
expected = recover_newlines(text)
for split in range(len(text) + 1):
normalizer = EscapedNewlineNormalizer()
actual = normalizer.feed(text[:split])
actual += normalizer.feed(text[split:], final=True)
assert actual == expected, (count, split)
def test_streaming_recovers_reasoning_and_content(self):
normalizer = MiniCPMVOutputNormalizer()
first = normalizer.normalize_delta(
DeltaMessage(reasoning="reason\\"),
finished=False,
)
second = normalizer.normalize_delta(
DeltaMessage(reasoning="nnext\\", content="answer\\"),
finished=False,
)
final = normalizer.normalize_delta(
DeltaMessage(content="nend"),
finished=True,
)
assert first == DeltaMessage(reasoning="reason")
assert second == DeltaMessage(reasoning="\nnext\\", content="answer")
assert final == DeltaMessage(content="\nend")
def test_streaming_does_not_modify_tool_arguments(self):
normalizer = MiniCPMVOutputNormalizer()
arguments = '{"value":"first\\\\nsecond"}'
delta = DeltaMessage(
tool_calls=[
DeltaToolCall(
index=0,
function=DeltaFunctionCall(arguments=arguments),
)
]
)
result = normalizer.normalize_delta(delta, finished=True)
assert result is not None
assert result.tool_calls[0].function is not None
assert result.tool_calls[0].function.arguments == arguments
def test_parse_recovers_content(self, mock_tokenizer, request_obj):
parser = _make_parser(mock_tokenizer, enable_thinking=False)
reasoning, content, tool_calls = parser.parse(
"first\\nsecond",
request_obj,
)
assert reasoning is None
assert content == "first\nsecond"
assert not tool_calls
def test_parse_delta_recovers_content(self, mock_tokenizer, request_obj):
parser = _make_parser(mock_tokenizer, enable_thinking=False)
chunk = parser.parse_delta(
"first\\",
[],
request_obj,
finished=False,
)
rest = parser.parse_delta(
"nsecond",
[],
request_obj,
finished=True,
)
# The trailing half of the escape is held back until it is complete.
assert chunk is not None
assert chunk.content == "first"
assert rest is not None
assert rest.content == "\nsecond"
def test_buffered_delta_is_dropped_not_sent_empty(
self, mock_tokenizer, request_obj
):
parser = _make_parser(mock_tokenizer, enable_thinking=False)
parser.parse_delta("hello", [], request_obj, finished=False)
# The lone backtick is held back for the next chunk. Reporting it as an
# all-None DeltaMessage would reach the client as an empty SSE chunk.
assert parser.parse_delta("`", [], request_obj, finished=False) is None
def test_normalizer_drops_empty_delta(self):
normalizer = MiniCPMVOutputNormalizer()
assert (
normalizer.normalize_delta(DeltaMessage(content="`"), finished=False)
is None
)
def test_normalizer_flushes_buffered_content_when_finished(self):
normalizer = MiniCPMVOutputNormalizer()
normalizer.normalize_delta(DeltaMessage(content="`"), finished=False)
final = normalizer.normalize_delta(DeltaMessage(), finished=True)
assert final is not None
assert final.content == "`"
-385
View File
@@ -1,385 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import dataclasses
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.engine.parser_engine import ParserEngine
from vllm.parser.minimax_m2 import (
THINK_END,
TOOL_CALL_END,
TOOL_CALL_START,
MinimaxM2Parser,
_minimax_m2_arg_value_extender,
minimax_m2_config,
)
@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) == ""
WRITE_CALL_HEAD = (
'<minimax:tool_call>\n<invoke name="write_file">\n'
'<parameter name="path">/repo/app.py</parameter>\n'
'<parameter name="mode">644</parameter>\n'
'<parameter name="content">'
)
WRITE_CALL_TAIL = "</parameter>\n</invoke>\n</minimax:tool_call>"
# Escapes, non-ASCII, blank lines and tag-like text that must stay inside
# the value, including prefixes of the closing and next-parameter tags.
CONTENT_LINE = 'if a < b > c: print("x\\y") # </p> </param <parameter\té\n\n'
class TestArgValueExtender:
"""The open parameter value streams without re-running the converter."""
@pytest.fixture
def write_tool(self):
return ChatCompletionToolsParam(
function=FunctionDefinition(
name="write_file",
parameters={
"type": "object",
"properties": {
"path": {"type": "string"},
"mode": {"type": "integer"},
"content": {"type": "string"},
},
},
),
)
@pytest.mark.parametrize("use_schema", [False, True], ids=["no_schema", "schema"])
def test_long_call_streams_non_streaming_arguments(
self, mock_tokenizer, mock_request, write_tool, use_schema
):
mock_request.tools = [write_tool] if use_schema else []
text = WRITE_CALL_HEAD + CONTENT_LINE * 200 + WRITE_CALL_TAIL
results = simulate_tool_streaming(
MinimaxM2Parser(mock_tokenizer),
mock_request,
[text[i : i + 7] for i in range(0, len(text), 7)],
)
result = MinimaxM2Parser(mock_tokenizer).extract_tool_calls(text, mock_request)
arguments = collect_tool_arguments(results)
assert arguments == result.tool_calls[0].function.arguments
assert json.loads(arguments)["content"] == CONTENT_LINE * 200
assert ('"mode": 644,' if use_schema else '"mode": "644",') in arguments
def test_conversions_do_not_grow_with_argument_length(
self, mock_tokenizer, mock_request, write_tool
):
mock_request.tools = [write_tool]
base = minimax_m2_config()
converter = base.arg_converter
assert converter is not None
def streaming_conversions(lines: int) -> int:
calls = 0
def counting_converter(raw_args: str, partial: bool) -> str:
nonlocal calls
calls += partial
return converter(raw_args, partial)
parser = ParserEngine(
mock_tokenizer,
parser_engine_config=dataclasses.replace(
base, arg_converter=counting_converter
),
)
text = WRITE_CALL_HEAD + CONTENT_LINE * lines + WRITE_CALL_TAIL
simulate_tool_streaming(
parser,
mock_request,
[text[i : i + 4] for i in range(0, len(text), 4)],
)
return calls
assert streaming_conversions(lines=20) == streaming_conversions(lines=200)
def test_extender_stops_at_split_end_tag(self):
extend = _minimax_m2_arg_value_extender('<parameter name="k">')
assert extend is not None
deltas = ['a"\n', "</param", "eter>"]
assert [extend(d) for d in deltas] == ['a\\"\\n', "</param", None]
def test_extender_stops_at_split_next_parameter(self):
extend = _minimax_m2_arg_value_extender("<parameter name='k'>x")
assert extend is not None
assert [extend(d) for d in ["< parameter", " name ="]] == ["< parameter", None]
@pytest.mark.parametrize(
"raw_args",
[
'<parameter name="k">x</parameter>',
'<parameter name="k',
(
'<parameter name="k">1</parameter><parameter name="j">2</parameter>'
'<parameter name="k">3'
),
'<parameter name="a<parameter name=k>x',
'<parameter name="k>x',
],
ids=[
"closed_value",
"open_name",
"repeated_key_not_last",
"earlier_unmatched_opener",
"bare_name_opens_quote",
],
)
def test_no_extender_unless_open_value_is_stable(self, raw_args):
assert _minimax_m2_arg_value_extender(raw_args) is None
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]
-253
View File
@@ -1,253 +0,0 @@
# 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_content,
collect_function_name,
collect_tool_arguments,
simulate_tool_streaming,
)
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionToolsParam,
FunctionDefinition,
)
from vllm.parser.minimax_m3 import (
INVOKE_END,
MAX_ELEMENT_DEPTH,
NAMESPACE,
TOOL_CALL_END,
TOOL_CALL_START,
MinimaxM3Parser,
)
NS = NAMESPACE
TOOLS = [
ChatCompletionToolsParam(
function=FunctionDefinition(
name="create_order",
parameters={
"type": "object",
"properties": {
"user_id": {"type": "integer"},
"note": {"type": "string"},
"tags": {"type": "array", "items": {"type": "string"}},
"shipping": {
"type": "object",
"properties": {"zip": {"type": "integer"}},
},
"either": {
"anyOf": [
{"type": "array", "items": {"type": "integer"}},
{"type": "string"},
]
},
"free": {"type": "object"},
},
},
),
)
]
@pytest.fixture
def parser():
return MinimaxM3Parser(make_mock_tokenizer({}), tools=TOOLS)
def element(name: str, body: str) -> str:
return f"{NS}<{name}>{body}{NS}</{name}>"
def tool_block(*invokes: tuple[str, str]) -> str:
body = "\n".join(
f'{NS}<invoke name="{name}">{params}{INVOKE_END}' for name, params in invokes
)
return f"{TOOL_CALL_START}\n{body}\n{TOOL_CALL_END}"
def parse_arguments(parser, request, params: str, name: str = "create_order"):
result = parser.extract_tool_calls(tool_block((name, params)), request)
assert [tc.function.name for tc in result.tool_calls] == [name]
return json.loads(result.tool_calls[0].function.arguments)
class TestArguments:
def test_nested_elements_follow_schema(self, parser, mock_request):
params = (
element("tags", element("item", "a") + element("item", "b"))
+ element("shipping", element("zip", "018956"))
+ element("either", element("item", "1") + element("item", "2"))
+ element("free", element("t", "x") + element("t", "y"))
)
assert parse_arguments(parser, mock_request, params) == {
"tags": ["a", "b"],
"shipping": {"zip": 18956},
"either": [1, 2],
"free": {"t": ["x", "y"]},
}
def test_empty_elements_follow_schema(self, parser, mock_request):
# The chat template renders [] and {} as empty elements.
params = (
element("tags", "")
+ element("shipping", "")
+ element("note", "")
+ element("either", "")
)
assert parse_arguments(parser, mock_request, params) == {
"tags": [],
"shipping": {},
"note": "",
"either": "",
}
def test_unknown_tool_keeps_text_and_nests_objects(self, parser, mock_request):
params = element("n", "1") + element(
"list", element("item", "a") + element("item", "b")
)
assert parse_arguments(parser, mock_request, params, name="other") == {
"n": "1",
"list": {"item": ["a", "b"]},
}
def test_mixed_text_uses_reserved_field(self, parser, mock_request):
params = element("free", "hi " + element("k", "v") + " there") + element(
"shipping", "x" + element("$text", "child")
)
assert parse_arguments(parser, mock_request, params) == {
"free": {"k": "v", "$text": "hi there"},
"shipping": {"$text": "child", "$$text": "x"},
}
def test_leaf_text_is_verbatim_for_strings(self, parser, mock_request):
params = element("note", "\n a <b> & c\n") + element("user_id", " 7 ")
assert parse_arguments(parser, mock_request, params) == {
"note": "\n a <b> & c\n",
"user_id": 7,
}
def test_block_marker_named_element_is_argument(self, parser, mock_request):
params = element("free", element("tool_call", "v"))
assert parse_arguments(parser, mock_request, params) == {
"free": {"tool_call": "v"},
}
@pytest.mark.parametrize(
"tail",
[
"junk" + element("note", "dropped"),
f"{NS}<note>a{NS}</other>",
f"{NS}<note>unterminated",
],
)
def test_malformed_element_keeps_preceding_params(self, parser, mock_request, tail):
params = element("user_id", "1") + tail
assert parse_arguments(parser, mock_request, params) == {"user_id": 1}
def test_depth_limit(self, parser, mock_request):
def nested(depth: int) -> str:
body = "v"
for _ in range(depth - 1):
body = element("n", body)
return element("free", body)
accepted = parse_arguments(parser, mock_request, nested(MAX_ELEMENT_DEPTH))
assert "free" in accepted
rejected = parse_arguments(parser, mock_request, nested(MAX_ELEMENT_DEPTH + 1))
assert rejected == {}
class TestToolBlock:
def test_multiple_invokes_and_prefix_content(self, parser, mock_request):
result = parser.extract_tool_calls(
"Checking.\n"
+ tool_block(
("create_order", element("user_id", "1")),
("create_order", element("user_id", "2")),
),
mock_request,
)
assert result.content == "Checking.\n"
assert [json.loads(tc.function.arguments) for tc in result.tool_calls] == [
{"user_id": 1},
{"user_id": 2},
]
def test_text_after_tool_block_is_dropped(self, parser, mock_request):
result = parser.extract_tool_calls(
"pre" + tool_block(("create_order", "")) + "post", mock_request
)
assert result.content == "pre"
assert len(result.tool_calls) == 1
def test_invoke_outside_tool_block_is_content(self, parser, mock_request):
text = f'{NS}<invoke name="create_order">{INVOKE_END}'
result = parser.extract_tool_calls(text, mock_request)
assert not result.tools_called
assert result.content == text
class TestStreaming:
def test_arguments_arrive_with_invoke_end(self, parser, mock_request):
block = tool_block(
("create_order", element("user_id", "42") + element("tags", ""))
)
invoke_end = block.index(INVOKE_END)
results = simulate_tool_streaming(
parser,
mock_request,
["Sure. ", block[:invoke_end], block[invoke_end:]],
)
before_end, at_end = results[1][0], results[2][0]
assert before_end is not None and before_end.tool_calls
assert before_end.tool_calls[0].function.name == "create_order"
assert not before_end.tool_calls[0].function.arguments
assert collect_content(results) == "Sure. "
assert json.loads(collect_tool_arguments([(at_end, "")])) == {
"user_id": 42,
"tags": [],
}
def test_char_chunks_match_complete_parse(self, parser, mock_request):
text = "a" + tool_block(
("create_order", element("shipping", element("zip", "1"))),
("create_order", element("note", "x")),
)
results = simulate_tool_streaming(parser, mock_request, list(text))
assert collect_content(results) == "a"
assert collect_function_name(results) == "create_order"
streamed = [
tc.function.arguments
for delta, _ in results
if delta and delta.tool_calls
for tc in delta.tool_calls
if tc.function and tc.function.arguments
]
assert [json.loads(args) for args in streamed] == [
{"shipping": {"zip": 1}},
{"note": "x"},
]
def test_truncated_invoke_keeps_completed_params(self, parser, mock_request):
block = tool_block(
("create_order", element("user_id", "1") + element("note", "x"))
)
truncated = block[: block.index(f"{NS}</note>")]
results = simulate_tool_streaming(parser, mock_request, [truncated])
results.append((parser.finish_streaming(), truncated))
assert collect_function_name(results) == "create_order"
assert json.loads(collect_tool_arguments(results)) == {"user_id": 1}
def test_adjust_request_keeps_special_tokens_skipped(parser, mock_request):
mock_request.skip_special_tokens = True
assert parser.adjust_request(mock_request).skip_special_tokens is True
-303
View File
@@ -1,303 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Tests for the engine-based Nemotron V3 parser.
Validates that ``NemotronV3Parser`` correctly handles:
- ``<think>``/``</think>`` reasoning with ``<tool_call>`` XML tool calls
(same format as Qwen3)
- Nemotron-specific reasoning/content swap when ``enable_thinking=False``
or ``force_nonempty_content=True``
"""
import json
from unittest.mock import MagicMock
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 (
ChatCompletionRequest,
)
from vllm.parser.nemotron_v3 import NemotronV3Parser
_THINK_START_ID = 50
_THINK_END_ID = 51
_TOOL_CALL_ID = 60
_TOOL_CALL_END_ID = 61
_TEXT_ID = 100
_VOCAB = {
"<think>": _THINK_START_ID,
"</think>": _THINK_END_ID,
"<tool_call>": _TOOL_CALL_ID,
"</tool_call>": _TOOL_CALL_END_ID,
}
def _make_request(**chat_template_kwargs):
request = MagicMock(spec=ChatCompletionRequest)
request.tools = []
request.tool_choice = "auto"
request.include_reasoning = True
request.chat_template_kwargs = chat_template_kwargs or None
return request
@pytest.fixture
def parser():
return NemotronV3Parser(make_mock_tokenizer(_VOCAB))
class TestNemotronSwap:
def test_enable_thinking_false_swaps(self, parser):
"""When enable_thinking=False, model output without think tags
should have reasoning swapped to content."""
text = "The answer is 42."
request = _make_request(enable_thinking=False)
reasoning, content = parser.extract_reasoning(text, request)
assert content == "The answer is 42."
assert reasoning is None
def test_force_nonempty_content_swaps(self, parser):
"""force_nonempty_content=True triggers swap when content empty."""
text = "The answer is 42."
request = _make_request(force_nonempty_content=True)
reasoning, content = parser.extract_reasoning(text, request)
assert content == "The answer is 42."
assert reasoning is None
def test_no_swap_when_content_exists(self, parser):
"""With enable_thinking=False but real </think> giving content,
no swap occurs."""
text = "Some reasoning.</think>Actual content here."
request = _make_request(enable_thinking=False)
reasoning, content = parser.extract_reasoning(text, request)
assert reasoning == "Some reasoning."
assert content == "Actual content here."
def test_no_swap_when_enable_thinking_true(self, parser):
"""Normal thinking mode: no swap, even when content is empty."""
text = "Still thinking..."
request = _make_request(enable_thinking=True)
reasoning, content = parser.extract_reasoning(text, request)
assert reasoning == "Still thinking..."
assert content is None
def test_no_swap_with_none_request(self, parser):
"""Graceful handling when request is None."""
text = "Some text."
reasoning, content = parser.extract_reasoning(text, None)
assert reasoning == "Some text."
assert content is None
def test_no_swap_with_no_kwargs(self, parser):
"""No swap when chat_template_kwargs is absent."""
text = "Some text."
request = _make_request()
reasoning, content = parser.extract_reasoning(text, request)
assert reasoning == "Some text."
assert content is None
def test_swap_with_whitespace_only_content(self, parser):
"""Swap occurs when content is whitespace-only."""
text = "The answer.</think> "
request = _make_request(enable_thinking=False)
reasoning, content = parser.extract_reasoning(text, request)
assert content == "The answer."
assert reasoning == " "
class TestNonStreamingToolCalls:
def test_single_tool_call(self, parser):
text = (
"<tool_call>\n"
"<function=get_weather>\n"
"<parameter=city>Tokyo</parameter>\n"
"</function>\n"
"</tool_call>"
)
request = _make_request()
result = parser.extract_tool_calls(text, request)
assert result.tools_called is True
assert len(result.tool_calls) == 1
assert result.tool_calls[0].function.name == "get_weather"
args = json.loads(result.tool_calls[0].function.arguments)
assert args == {"city": "Tokyo"}
def test_parallel_tool_calls(self, parser):
text = (
"<tool_call>\n"
"<function=get_weather>\n"
"<parameter=city>Tokyo</parameter>\n"
"</function>\n"
"</tool_call>"
"<tool_call>\n"
"<function=get_time>\n"
"<parameter=timezone>Asia/Tokyo</parameter>\n"
"</function>\n"
"</tool_call>"
)
request = _make_request()
result = parser.extract_tool_calls(text, request)
assert result.tools_called is True
assert len(result.tool_calls) == 2
assert result.tool_calls[0].function.name == "get_weather"
assert result.tool_calls[1].function.name == "get_time"
def test_no_tool_calls(self, parser):
request = _make_request()
result = parser.extract_tool_calls("Hello, how can I help?", request)
assert result.tools_called is False
# Parser starts in REASONING state, so plain text is classified
# as reasoning (not content) when there are no tool calls.
assert result.content is None
class TestStreaming:
def test_streaming_tool_calls(self, parser):
request = _make_request()
chunks = [
"<tool_call>\n",
"<function=get_weather>\n",
"<parameter=city>Tokyo",
"</parameter>\n",
"</function>\n",
"</tool_call>",
]
results = simulate_tool_streaming(parser, request, chunks)
name = collect_function_name(results)
assert name == "get_weather"
args_text = collect_tool_arguments(results)
assert args_text
parsed = json.loads(args_text)
assert parsed == {"city": "Tokyo"}
class TestParseDeltaTokenIdFiltering:
"""parse_delta must not trigger tool call parsing when <tool_call>
appears as regular text rather than as a special token ID."""
def test_tool_call_text_in_reasoning_is_not_parsed(self, parser):
"""Literal <tool_call> in model reasoning should be content,
not a tool call."""
request = _make_request()
text = (
"The test uses <tool_call> syntax:\n"
"<tool_call>\n"
"<function=Bash>\n"
"<parameter=command>ls</parameter>\n"
"</function>\n"
"</tool_call>"
)
result = parser.parse_delta(
delta_text=text,
delta_token_ids=[_TEXT_ID] * 6,
request=request,
prompt_token_ids=[],
finished=True,
)
assert result is not None
assert result.reasoning is not None
assert "<tool_call>" in result.reasoning
assert not result.tool_calls
def test_special_token_id_still_triggers_tool_call(self, parser):
"""When the scanner matches a special token ID, the tool call
must still be parsed correctly."""
request = _make_request()
parser.parse_delta(
delta_text="Let me check.",
delta_token_ids=[_TEXT_ID, _TEXT_ID, _TEXT_ID],
request=request,
prompt_token_ids=[],
finished=False,
)
parser.parse_delta(
delta_text="<tool_call>",
delta_token_ids=[_TOOL_CALL_ID],
request=request,
finished=False,
)
parser.parse_delta(
delta_text=(
"\n<function=get_weather>\n"
"<parameter=city>Tokyo</parameter>\n"
"</function>\n"
),
delta_token_ids=[_TEXT_ID] * 5,
request=request,
finished=False,
)
parser.parse_delta(
delta_text="</tool_call>",
delta_token_ids=[_TOOL_CALL_END_ID],
request=request,
finished=True,
)
assert any(s.name == "get_weather" for s in parser._tool_slots)
def test_text_discussion_then_real_tool_call(self, parser):
"""Model discusses tool syntax in reasoning, then makes a real
tool call via special tokens."""
request = _make_request()
r1 = parser.parse_delta(
delta_text="Use <tool_call> to invoke tools.",
delta_token_ids=[_TEXT_ID] * 6,
request=request,
prompt_token_ids=[],
finished=False,
)
r2 = parser.parse_delta(
delta_text="</think>",
delta_token_ids=[_THINK_END_ID],
request=request,
finished=False,
)
r3 = parser.parse_delta(
delta_text="<tool_call>",
delta_token_ids=[_TOOL_CALL_ID],
request=request,
finished=False,
)
r4 = parser.parse_delta(
delta_text=("\n<function=test>\n<parameter=x>1</parameter>\n</function>\n"),
delta_token_ids=[_TEXT_ID] * 4,
request=request,
finished=False,
)
r5 = parser.parse_delta(
delta_text="</tool_call>",
delta_token_ids=[_TOOL_CALL_END_ID],
request=request,
finished=True,
)
results = [r1, r2, r3, r4, r5]
reasoning = "".join(r.reasoning for r in results if r and r.reasoning)
assert "<tool_call>" in reasoning
names = [
tc.function.name
for r in results
if r and r.tool_calls
for tc in r.tool_calls
if tc.function and tc.function.name
]
assert "test" in names
File diff suppressed because it is too large Load Diff
-556
View File
@@ -1,556 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""PLaMo3 grammar, truncation, and serving-adapter regressions."""
from __future__ import annotations
import json
import pytest
import regex as re
from tests.parser.engine.conftest import make_mock_tokenizer
from tests.parser.engine.replay_harness import (
DUMMY_TOOLS,
_test_request,
collect_output,
replay_streaming,
replay_with_text_holdback,
)
from vllm.parser.parser_manager import ParserManager
from vllm.parser.plamo3 import (
BEGIN_THINK,
BEGIN_TOOL_ARGUMENTS,
BEGIN_TOOL_NAME,
BEGIN_TOOL_REQUEST,
BEGIN_TOOL_REQUESTS,
END_THINK,
END_TOOL_ARGUMENTS,
END_TOOL_NAME,
END_TOOL_REQUEST,
END_TOOL_REQUESTS,
EOT,
PLAMO_MARKER_TOKENS,
Plamo3Parser,
)
def _tool_header(name="weather"):
return (
BEGIN_TOOL_REQUEST
+ BEGIN_TOOL_NAME
+ name
+ END_TOOL_NAME
+ BEGIN_TOOL_ARGUMENTS
)
def _tool_call(name, arguments):
return _tool_header(name) + arguments + END_TOOL_ARGUMENTS + END_TOOL_REQUEST
@pytest.fixture
def mock_tokenizer():
special = [*sorted(PLAMO_MARKER_TOKENS), EOT, "<|plamo:bos|>"]
atoms = [*special, "<|plamo:constrain|>", "<|plamo:msg|>"]
vocab = {token: 100 + i for i, token in enumerate(atoms)}
inverse = {i: token for token, i in vocab.items()}
pattern = re.compile("|".join(map(re.escape, atoms)) + r"|[\s\S]")
tokenizer = make_mock_tokenizer(vocab, special_tokens=special)
tokenizer.encode.side_effect = lambda text, **kwargs: [
vocab[piece] if piece in vocab else 1000 + ord(piece)
for piece in pattern.findall(text)
]
tokenizer.decode.side_effect = lambda ids, **kwargs: "".join(
inverse[i] if i in inverse else chr(i - 1000) for i in ids
)
return tokenizer
@pytest.fixture(params=["direct", "registered"])
def parser_cls(request):
if request.param == "direct":
return Plamo3Parser
return ParserManager.get_parser(
tool_parser_name="plamo3",
reasoning_parser_name="plamo3",
enable_auto_tools=True,
)
def _stream(parser, tokenizer, output, chunk_size=1, finished_on_last=False):
tokens = [
(i, tokenizer.decode([i]))
for i in tokenizer.encode(output, add_special_tokens=False)
]
deltas = replay_streaming(
parser,
tokens,
chunk_size=chunk_size,
finished_on_last=finished_on_last,
tools=DUMMY_TOOLS,
)
if not finished_on_last:
deltas.append(
parser.parse_delta("", [], _test_request(DUMMY_TOOLS), finished=True)
)
return deltas
@pytest.mark.parametrize("thinking", [False, True])
@pytest.mark.parametrize("with_markers", [False, True])
def test_reasoning_mode(
parser_cls, mock_tokenizer, mock_request, thinking, with_markers
):
output = (
BEGIN_THINK + "Reasoning." + END_THINK + "Answer."
if with_markers
else "Answer."
)
if thinking:
expected = ("Reasoning.", "Answer.") if with_markers else ("Answer.", "")
else:
expected = ("", output)
kwargs = {"chat_template_kwargs": {"enable_thinking": thinking}}
reasoning, content, calls = parser_cls(mock_tokenizer, **kwargs).parse(
output, mock_request
)
assert (reasoning or "", content or "") == expected
assert not calls
result = collect_output(
_stream(parser_cls(mock_tokenizer, **kwargs), mock_tokenizer, output)
)
assert (result.reasoning, result.content) == expected
assert not result.tool_calls
def test_duplicate_think_end_does_not_leak_into_content(
parser_cls, mock_tokenizer, mock_request
):
output = END_THINK + "A" + END_THINK + "B"
reasoning, content, calls = parser_cls(mock_tokenizer).parse(output, mock_request)
assert (reasoning, content) == (None, "AB")
assert not calls
def test_content_can_start_a_new_reasoning_block(
parser_cls, mock_tokenizer, mock_request
):
output = END_THINK + "A" + BEGIN_THINK + "R" + END_THINK
reasoning, content, calls = parser_cls(mock_tokenizer).parse(output, mock_request)
assert (reasoning, content) == ("R", "A")
assert not calls
@pytest.mark.parametrize("chunk_size", [1, 7, None])
@pytest.mark.parametrize("finished_on_last", [False, True])
@pytest.mark.parametrize(
"closing",
[
"",
END_TOOL_ARGUMENTS + END_TOOL_REQUEST,
END_TOOL_ARGUMENTS + END_TOOL_REQUEST + END_TOOL_REQUESTS + EOT,
],
)
def test_complete_arguments_survive_missing_closers(
parser_cls, mock_tokenizer, mock_request, chunk_size, finished_on_last, closing
):
output = (
BEGIN_THINK
+ "Reasoning. "
+ END_THINK
+ "Before tools. "
+ BEGIN_TOOL_REQUESTS
+ _tool_header()
+ '{"city":"東京"}'
+ closing
)
reasoning, content, calls = parser_cls(mock_tokenizer).parse(
output, mock_request, enable_auto_tools=True
)
assert (reasoning, content) == ("Reasoning. ", "Before tools. ")
assert calls and [(c.name, json.loads(c.arguments)) for c in calls] == [
("weather", {"city": "東京"})
]
result = collect_output(
_stream(
parser_cls(mock_tokenizer),
mock_tokenizer,
output,
chunk_size,
finished_on_last,
)
)
assert (result.reasoning, result.content) == (reasoning, content)
assert result.tool_calls == [{"name": "weather", "arguments": '{"city":"東京"}'}]
def test_tool_name_cutoffs_do_not_leak_markers(
parser_cls, mock_tokenizer, mock_request
):
header = BEGIN_TOOL_REQUEST + BEGIN_TOOL_NAME + "weather" + END_TOOL_NAME
header_ids = mock_tokenizer.encode(header, add_special_tokens=False)
name_start = len(
mock_tokenizer.encode(
BEGIN_TOOL_REQUEST + BEGIN_TOOL_NAME, add_special_tokens=False
)
)
for end in range(len(header_ids)):
partial = mock_tokenizer.decode(header_ids[:end])
output = END_THINK + "Before tools." + BEGIN_TOOL_REQUESTS + partial
reasoning, content, calls = parser_cls(mock_tokenizer).parse(
output, mock_request, enable_auto_tools=True
)
assert (reasoning, content) == (None, "Before tools."), end
if end <= name_start:
expected_name = ""
else:
name_text = mock_tokenizer.decode(header_ids[name_start:end])
if "<|plamo:" in name_text:
name_text = name_text[: name_text.rfind("<|plamo:")]
expected_name = name_text[: len("weather")]
assert [call.name for call in calls or []] == (
[expected_name] if expected_name else []
), end
result = collect_output(
_stream(parser_cls(mock_tokenizer), mock_tokenizer, output)
)
assert [call["name"] for call in result.tool_calls] == (
[expected_name] if expected_name else []
), end
assert all("<" not in call["name"] for call in result.tool_calls), end
assert result.content == "Before tools.", end
@pytest.mark.parametrize("chunk_size", [1, 7, None])
@pytest.mark.parametrize("finished_on_last", [False, True])
def test_all_argument_closer_cutoffs_do_not_leak(
parser_cls, mock_tokenizer, mock_request, chunk_size, finished_on_last
):
head = END_THINK + BEGIN_TOOL_REQUESTS + _tool_header()
closer_ids = mock_tokenizer.encode(END_TOOL_ARGUMENTS, add_special_tokens=False)
for end in range(len(closer_ids)):
partial = mock_tokenizer.decode(closer_ids[:end])
output = head + '{"city":"東京"}' + partial
_, _, calls = parser_cls(mock_tokenizer).parse(
output, mock_request, enable_auto_tools=True
)
assert calls and calls[0].arguments == '{"city":"東京"}', end
result = collect_output(
_stream(
parser_cls(mock_tokenizer),
mock_tokenizer,
output,
chunk_size,
finished_on_last,
)
)
assert result.tool_calls == [
{"name": "weather", "arguments": '{"city":"東京"}'}
], end
@pytest.mark.parametrize(
"arguments",
[
"{}",
'{"name":"Alice","arguments":{"x":1},"parameters":true}',
'{"text":"literal <|plamo:end_',
],
)
def test_arguments_are_not_rewritten(
parser_cls, mock_tokenizer, mock_request, arguments
):
output = END_THINK + BEGIN_TOOL_REQUESTS + _tool_header() + arguments
_, _, calls = parser_cls(mock_tokenizer).parse(
output, mock_request, enable_auto_tools=True
)
assert calls and calls[0].arguments == arguments
result = collect_output(_stream(parser_cls(mock_tokenizer), mock_tokenizer, output))
assert result.tool_calls == [{"name": "weather", "arguments": arguments}]
@pytest.mark.parametrize("truncated_second", [False, True])
def test_multiple_calls_keep_indices_ids_and_arguments_separate(
parser_cls, mock_tokenizer, mock_request, truncated_second
):
second = (
BEGIN_TOOL_REQUEST + BEGIN_TOOL_NAME + "clo"
if truncated_second
else _tool_call("clock", '{"timezone":"Asia/Tokyo"}')
)
output = (
END_THINK
+ BEGIN_TOOL_REQUESTS
+ _tool_call("weather", '{"city":"東京"}')
+ second
)
expected = [{"name": "weather", "arguments": '{"city":"東京"}'}]
if truncated_second:
expected.append({"name": "clo", "arguments": "{}"})
else:
output += END_TOOL_REQUESTS
expected.append({"name": "clock", "arguments": '{"timezone":"Asia/Tokyo"}'})
stream_expected = expected
_, content, calls = parser_cls(mock_tokenizer).parse(
output, mock_request, enable_auto_tools=True
)
assert (
calls
and [{"name": c.name, "arguments": c.arguments} for c in calls] == expected
)
deltas = _stream(parser_cls(mock_tokenizer), mock_tokenizer, output)
result = collect_output(deltas)
assert result.tool_calls == stream_expected
assert result.content == (content or "") == ""
announced = [
call for delta in deltas if delta for call in delta.tool_calls or [] if call.id
]
assert [call.index for call in announced] == list(range(len(expected)))
assert len({call.id for call in announced}) == len(expected)
def test_arguments_are_available_before_outer_closer(mock_tokenizer, mock_request):
parser = Plamo3Parser(mock_tokenizer)
output = END_THINK + BEGIN_TOOL_REQUESTS + _tool_call("weather", '{"city":"Tokyo"}')
first = parser.parse_delta(output, [], mock_request, finished=False)
assert collect_output([first]).tool_calls == [
{"name": "weather", "arguments": '{"city":"Tokyo"}'}
]
assert (
parser.parse_delta(END_TOOL_REQUESTS[:10], [], mock_request, finished=False)
is None
)
assert (
parser.parse_delta(END_TOOL_REQUESTS[10:], [], mock_request, finished=True)
is None
)
def test_missing_tool_request_start_does_not_break_streaming(
parser_cls, mock_tokenizer, mock_request
):
output = (
BEGIN_TOOL_REQUESTS
+ BEGIN_TOOL_NAME
+ "weather"
+ END_TOOL_NAME
+ BEGIN_TOOL_ARGUMENTS
+ "{}"
+ END_TOOL_ARGUMENTS
+ END_TOOL_REQUESTS
)
delta = parser_cls(mock_tokenizer).parse_delta(
output, [], mock_request, finished=False
)
assert delta is not None
assert collect_output([delta]).tool_calls == [
{"name": "weather", "arguments": "{}"}
]
def test_reusing_parser_drops_pending_names(mock_tokenizer, mock_request):
parser = Plamo3Parser(mock_tokenizer)
parser.parse(
END_THINK
+ BEGIN_TOOL_REQUESTS
+ BEGIN_TOOL_REQUEST
+ BEGIN_TOOL_NAME
+ "partial",
mock_request,
)
output = (
END_THINK
+ BEGIN_TOOL_REQUESTS
+ _tool_call("weather", "{}")
+ END_TOOL_REQUESTS
)
_, _, calls = parser.parse(output, mock_request)
assert calls and calls[0].name == "weather"
@pytest.mark.parametrize(
"text", ["", "a < b", "literal <", "literal <|plamo:unknown", " text\n"]
)
def test_plain_content_is_preserved(parser_cls, mock_tokenizer, mock_request, text):
output = END_THINK + text + EOT
_, content, calls = parser_cls(mock_tokenizer).parse(output, mock_request)
assert (content or "") == text
assert not calls
result = collect_output(_stream(parser_cls(mock_tokenizer), mock_tokenizer, output))
assert (result.content, result.tool_calls) == (text, [])
@pytest.mark.parametrize(
"text, ended",
[
("", False),
(BEGIN_THINK + "reasoning", False),
(END_THINK + "answer", True),
(END_THINK + "answer" + BEGIN_THINK + "new reasoning", False),
(END_THINK + "answer" + EOT + "assistant", False),
],
)
def test_reasoning_end_checks_the_latest_turn(mock_tokenizer, text, ended):
parser = Plamo3Parser(mock_tokenizer)
ids = mock_tokenizer.encode(text)
assert parser.is_reasoning_end(ids) == ended
if ended:
assert mock_tokenizer.decode(parser.extract_content_ids(ids)) == "answer"
@pytest.mark.parametrize("suffix", ["<|plamo:end_", "<|plamo:end_think"])
def test_partial_end_think_is_consistent_between_streaming_and_non_streaming(
parser_cls, mock_tokenizer, mock_request, suffix
):
output = "visible" + suffix
reasoning, content, calls = parser_cls(mock_tokenizer).parse(output, mock_request)
assert (reasoning, content) == ("visible", None)
assert not calls
result = collect_output(_stream(parser_cls(mock_tokenizer), mock_tokenizer, output))
assert (result.reasoning, result.content, result.tool_calls) == ("visible", "", [])
@pytest.mark.parametrize(
"suffix",
[
"<|plamo:begin_",
"<|plamo:begin_tool_name",
"<|plamo:end_tool",
],
)
def test_unfinished_marker_prefixes_are_stripped(mock_tokenizer, suffix):
parser = Plamo3Parser(mock_tokenizer)
assert parser._strip_unfinished_marker("visible" + suffix) == "visible"
@pytest.mark.parametrize("suffix", ["<|plamo:", "<|plamo:b"])
def test_non_token_prefix_is_not_stripped(mock_tokenizer, suffix):
parser = Plamo3Parser(mock_tokenizer)
text = "visible" + suffix
assert parser._strip_unfinished_marker(text) == text
def test_complete_markers_are_not_stripped(mock_tokenizer):
parser = Plamo3Parser(mock_tokenizer)
text = "visible" + BEGIN_TOOL_NAME
assert parser._strip_unfinished_marker(text) == text
def test_short_prefix_is_not_stripped(mock_tokenizer):
parser = Plamo3Parser(mock_tokenizer)
text = "visible<"
assert parser._strip_unfinished_marker(text) == text
def test_tool_block_can_end_reasoning_implicitly(
parser_cls, mock_tokenizer, mock_request
):
output = (
"reasoning"
+ BEGIN_TOOL_REQUESTS
+ _tool_call("weather", "{}")
+ END_TOOL_REQUESTS
)
reasoning, content, calls = parser_cls(mock_tokenizer).parse(
output, mock_request, enable_auto_tools=True
)
assert (reasoning, content) == ("reasoning", None)
assert calls and calls[0].name == "weather"
result = collect_output(_stream(parser_cls(mock_tokenizer), mock_tokenizer, output))
assert (result.reasoning, result.content) == ("reasoning", "")
assert result.tool_calls == [{"name": "weather", "arguments": "{}"}]
def test_every_cut_point_matches_non_streaming(
parser_cls, mock_tokenizer, mock_request
):
output = (
BEGIN_THINK
+ "Reasoning. "
+ END_THINK
+ "Before tools. "
+ BEGIN_TOOL_REQUESTS
+ _tool_call("weather", '{"city":"東京"}')
+ END_TOOL_REQUESTS
+ EOT
)
ids = mock_tokenizer.encode(output, add_special_tokens=False)
for k in range(1, len(ids) + 1):
prefix = mock_tokenizer.decode(ids[:k])
reasoning, content, calls = parser_cls(mock_tokenizer).parse(
prefix, mock_request, enable_auto_tools=True
)
result = collect_output(
_stream(parser_cls(mock_tokenizer), mock_tokenizer, prefix)
)
assert (result.reasoning, result.content) == (
(reasoning or ""),
(content or ""),
), k
expected = [{"name": c.name, "arguments": c.arguments} for c in (calls or [])]
assert result.tool_calls == expected, (k, expected, result.tool_calls)
@pytest.mark.parametrize(
"choice",
[
"auto",
"required",
"none",
{"type": "function", "function": {"name": "weather"}},
],
)
def test_tool_choice_uses_the_plamo_format(parser_cls, mock_tokenizer, choice):
request = _test_request(
[
{
"type": "function",
"function": {"name": "weather", "parameters": {"type": "object"}},
},
]
)
request = type(request).model_validate(
request.model_dump() | {"tool_choice": choice}
)
output = (
END_THINK
+ "Before tools."
+ BEGIN_TOOL_REQUESTS
+ _tool_call("weather", "{}")
+ END_TOOL_REQUESTS
)
_, content, calls = parser_cls(mock_tokenizer).parse(
output, request, enable_auto_tools=True
)
assert content == "Before tools."
assert [c.name for c in calls or []] == ([] if choice == "none" else ["weather"])
parser = parser_cls(mock_tokenizer)
delta = parser.parse_delta(output, [], request, finished=True)
result = collect_output([delta])
assert result.content == "Before tools."
assert result.tool_calls == (
[] if choice == "none" else [{"name": "weather", "arguments": "{}"}]
)
@pytest.mark.parametrize("delay", [1, 3])
def test_delayed_text_keeps_split_markers_intact(parser_cls, mock_tokenizer, delay):
output = (
BEGIN_THINK
+ "reasoning"
+ END_THINK
+ "content"
+ BEGIN_TOOL_REQUESTS
+ _tool_call("weather", "{}")
+ END_TOOL_REQUESTS
+ EOT
)
tokens = [(i, mock_tokenizer.decode([i])) for i in mock_tokenizer.encode(output)]
result = collect_output(
replay_with_text_holdback(
parser_cls(mock_tokenizer), tokens, text_delay=delay, tools=DUMMY_TOOLS
)
)
assert (result.reasoning, result.content) == ("reasoning", "content")
assert result.tool_calls == [{"name": "weather", "arguments": "{}"}]
File diff suppressed because it is too large Load Diff
-235
View File
@@ -1,235 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Tests for the engine-based seed_oss parser.
seed_oss is Qwen3 with four overridden wrapper tokens, so the shared grammar
(arg types, multiline values, parallel calls, streaming mechanics, …) is
already covered by ``test_qwen3.py``/``test_qwen3_reasoning.py``. These tests
cover only what is seed_oss-specific: that the ``seed:`` token overrides are
wired through, the reasoning→tool boundary holds with them, the malformed
header from #46314 no longer drops sibling calls, and the registered adapters
resolve. Seed-specific budget-reflect tags inside reasoning are also covered
here because the old dedicated parser tests exercised them.
"""
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_reasoning_streaming,
simulate_tool_streaming,
)
from vllm.parser.engine.registered_adapters import (
SeedOssParserReasoningAdapter,
SeedOssParserToolAdapter,
)
from vllm.parser.seed_oss import SeedOssParser
TOOL_CALL_START = "<seed:tool_call>"
TOOL_CALL_END = "</seed:tool_call>"
THINK_START = "<seed:think>"
THINK_END = "</seed:think>"
TURN_START = "<seed:bos>"
TURN_END = "<seed:eos>"
_THINK_START_ID = 50
_THINK_END_ID = 51
_TOOL_CALL_START_ID = 60
_TOOL_CALL_END_ID = 61
_SEED_BOS_ID = 70
_SEED_EOS_ID = 71
_TEXT_ID = 100
_SEED_OSS_VOCAB = {
THINK_START: _THINK_START_ID,
THINK_END: _THINK_END_ID,
TOOL_CALL_START: _TOOL_CALL_START_ID,
TOOL_CALL_END: _TOOL_CALL_END_ID,
TURN_START: _SEED_BOS_ID,
TURN_END: _SEED_EOS_ID,
}
@pytest.fixture
def mock_tokenizer():
return make_mock_tokenizer(_SEED_OSS_VOCAB)
@pytest.fixture
def tool_parser(mock_tokenizer):
return SeedOssParser(
mock_tokenizer, chat_template_kwargs={"enable_thinking": False}
)
@pytest.fixture
def parser(mock_tokenizer):
return SeedOssParser(mock_tokenizer)
def test_token_overrides_wired(parser):
assert parser.parser_engine_config.name == "seed_oss"
assert parser.reasoning_start_str == THINK_START
assert parser.reasoning_end_str == THINK_END
assert parser.parser_engine_config.turn_boundary_tokens == frozenset(
(TURN_START, TURN_END)
)
class TestSeedTurnBoundaries:
"""The Seed-OSS chat template wraps every message in ``<seed:bos>role`` /
``<seed:eos>`` and replays prior-turn ``reasoning_content`` inside
``<seed:think>...</seed:think>``, so a stale ``</seed:think>`` in history
is the normal multi-turn case. The backward walk must stop at the Seed
boundary tokens; the inherited ChatML tokens are absent from the Seed
vocab and would disable the scoping entirely.
"""
def test_replayed_reasoning_in_history_not_end(self, parser):
assert not parser.is_reasoning_end(
[
_SEED_BOS_ID,
_THINK_START_ID,
_TEXT_ID,
_THINK_END_ID,
_TEXT_ID,
_SEED_EOS_ID,
_SEED_BOS_ID,
]
)
def test_think_end_in_current_turn_is_end(self, parser):
assert parser.is_reasoning_end(
[_SEED_BOS_ID, _TEXT_ID, _SEED_EOS_ID, _SEED_BOS_ID, _THINK_END_ID]
)
def test_boundary_with_thinking_disabled_is_end(self, mock_tokenizer):
parser = SeedOssParser(
mock_tokenizer, chat_template_kwargs={"enable_thinking": False}
)
assert parser.is_reasoning_end([_SEED_BOS_ID, _TEXT_ID])
def test_single_tool_call(tool_parser, mock_request):
text = (
f"{TOOL_CALL_START}\n<function=get_weather>\n"
"<parameter=city>Tokyo</parameter>\n"
f"</function>\n{TOOL_CALL_END}"
)
result = tool_parser.extract_tool_calls(text, mock_request)
assert result.tools_called is True
assert result.tool_calls[0].function.name == "get_weather"
assert json.loads(result.tool_calls[0].function.arguments) == {"city": "Tokyo"}
def test_malformed_function_end_does_not_drop_siblings(tool_parser, mock_request):
"""Regression for #46314: a malformed ``</function>`` with no closing ``>``
on the header must not discard the other, well-formed calls."""
text = (
f"{TOOL_CALL_START}\n<function=broken</function>\n{TOOL_CALL_END}"
f"{TOOL_CALL_START}\n<function=get_weather>\n"
"<parameter=city>Tokyo</parameter>\n"
f"</function>\n{TOOL_CALL_END}"
)
result = tool_parser.extract_tool_calls(text, mock_request)
weather = next(tc for tc in result.tool_calls if tc.function.name == "get_weather")
assert json.loads(weather.function.arguments) == {"city": "Tokyo"}
def test_basic_streaming(tool_parser, mock_request):
chunks = [
f"{TOOL_CALL_START}\n",
"<function=get_weather>\n",
"<parameter=city>Tokyo",
"</parameter>\n",
"</function>\n",
f"{TOOL_CALL_END}",
]
results = simulate_tool_streaming(tool_parser, mock_request, chunks)
assert collect_function_name(results) == "get_weather"
assert json.loads(collect_tool_arguments(results)) == {"city": "Tokyo"}
def test_reasoning_then_tool_call(parser):
text = (
f"{THINK_START}I need to read the file.{THINK_END}"
f"{TOOL_CALL_START}\n<function=read>\n"
"<parameter=path>/tmp/x</parameter>\n"
f"</function>\n{TOOL_CALL_END}"
)
reasoning, _ = parser.extract_reasoning(text, None)
assert reasoning == "I need to read the file."
assert TOOL_CALL_START not in reasoning
def test_streaming_think_end_and_tool_call_same_delta(parser):
"""``</seed:think>`` and ``<seed:tool_call>`` arriving in one delta must
not leak the terminal tokens into the reasoning text."""
reasoning, content = simulate_reasoning_streaming(
parser,
[
"Let me list the directory.",
f"{THINK_END}{TOOL_CALL_START}",
"<function=read>",
],
[(1,), (_THINK_END_ID, _TOOL_CALL_START_ID), (2,)],
)
assert reasoning == "Let me list the directory."
assert THINK_END not in reasoning
assert TOOL_CALL_START not in reasoning
assert content is not None
def test_end_to_end_through_registered_adapters(mock_tokenizer, mock_request):
reasoning_parser = SeedOssParserReasoningAdapter(mock_tokenizer)
tool_parser = SeedOssParserToolAdapter(mock_tokenizer)
text = (
f"{THINK_START}Plan the call.{THINK_END}"
f"{TOOL_CALL_START}\n<function=get_weather>\n"
"<parameter=city>Tokyo</parameter>\n"
f"</function>\n{TOOL_CALL_END}"
)
reasoning, remaining = reasoning_parser.extract_reasoning(text, mock_request)
assert reasoning == "Plan the call."
tool_result = tool_parser.extract_tool_calls(remaining, mock_request)
assert tool_result.tool_calls[0].function.name == "get_weather"
assert json.loads(tool_result.tool_calls[0].function.arguments) == {"city": "Tokyo"}
def test_budget_reflect_tags_do_not_break_adapter_pipeline(
mock_tokenizer,
mock_request,
):
reasoning_parser = SeedOssParserReasoningAdapter(mock_tokenizer)
tool_parser = SeedOssParserToolAdapter(mock_tokenizer)
text = (
f"{THINK_START}"
"The user's current thinking budget is 512.</seed:cot_budget_reflect>\n"
"I need the weather.\n"
"<seed:cot_budget_reflect>I have used 131 tokens."
"</seed:cot_budget_reflect>\n"
f"{THINK_END}"
f"{TOOL_CALL_START}\n<function=get_weather>\n"
"<parameter=city>Barcelona</parameter>\n"
f"</function>\n{TOOL_CALL_END}"
)
reasoning, remaining = reasoning_parser.extract_reasoning(text, mock_request)
assert reasoning is not None
assert "current thinking budget is 512" in reasoning
assert "<seed:cot_budget_reflect>" in reasoning
assert "</seed:cot_budget_reflect>" in reasoning
tool_result = tool_parser.extract_tool_calls(remaining, mock_request)
assert tool_result.tool_calls[0].function.name == "get_weather"
assert json.loads(tool_result.tool_calls[0].function.arguments) == {
"city": "Barcelona"
}
-122
View File
@@ -1,122 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Tests for the engine-based Step-3.5 parser.
Step-3.5 reuses the Qwen3 grammar, which ``test_qwen3.py`` covers. These tests
cover what differs: thinking is always on, the newline the model emits before
``</think>`` is dropped, prior-turn reasoning does not end the current turn,
and the ``step3p5`` names resolve to the engine adapters.
"""
import json
import pytest
from tests.parser.engine.conftest import make_mock_tokenizer
from tests.parser.engine.streaming_helpers import simulate_reasoning_streaming
from vllm.parser.engine.registered_adapters import (
Step3p5ParserReasoningAdapter,
Step3p5ParserToolAdapter,
)
from vllm.parser.step3p5 import Step3p5Parser
from vllm.reasoning import ReasoningParserManager
from vllm.tool_parsers import ToolParserManager
_THINK_START_ID = 50
_THINK_END_ID = 51
_IM_START_ID = 70
_IM_END_ID = 71
_TEXT_ID = 100
_STEP3P5_VOCAB = {
"<think>": _THINK_START_ID,
"</think>": _THINK_END_ID,
"<tool_call>": 60,
"</tool_call>": 61,
"<|im_start|>": _IM_START_ID,
"<|im_end|>": _IM_END_ID,
}
@pytest.fixture
def mock_tokenizer():
return make_mock_tokenizer(_STEP3P5_VOCAB)
@pytest.fixture
def parser(mock_tokenizer):
return Step3p5Parser(mock_tokenizer)
def test_parser_names_resolve_to_engine_adapters():
tool_parser_cls = ToolParserManager.get_tool_parser("step3p5")
assert issubclass(tool_parser_cls, Step3p5ParserToolAdapter)
assert tool_parser_cls.structural_tag_model == "qwen_3_coder"
reasoning_parser_cls = ReasoningParserManager.get_reasoning_parser("step3p5")
assert reasoning_parser_cls is Step3p5ParserReasoningAdapter
def test_enable_thinking_false_is_ignored(mock_tokenizer):
# The chat template always prefills "<think>\n" and has no thinking switch.
parser = Step3p5Parser(
mock_tokenizer, chat_template_kwargs={"enable_thinking": False}
)
assert parser.extract_reasoning("plan</think>answer", None) == ("plan", "answer")
assert not parser.is_reasoning_end([_IM_START_ID, _TEXT_ID, _THINK_START_ID])
def test_newline_before_think_end_dropped(parser):
text = "Line one.\nLine two.\n</think>\nThe answer."
assert parser.extract_reasoning(text, None) == (
"Line one.\nLine two.",
"\nThe answer.",
)
def test_newline_before_think_end_dropped_streaming(parser):
reasoning, content = simulate_reasoning_streaming(
parser,
["Line one.\n", "Line two.\n", "</think>", "\nThe answer."],
[(_TEXT_ID,), (_TEXT_ID,), (_THINK_END_ID,), (_TEXT_ID,)],
)
assert reasoning == "Line one.\nLine two."
assert content == "\nThe answer."
class TestPriorTurnReasoning:
"""The template replays earlier assistant turns as
``<think>\\n...\\n</think>\\n``, so a ``</think>`` from history must not end
reasoning for the new turn (#34211)."""
_HISTORY = [
_IM_START_ID,
_TEXT_ID,
_THINK_START_ID,
_TEXT_ID,
_THINK_END_ID,
_TEXT_ID,
_IM_END_ID,
]
def test_prior_turn_think_end_not_end(self, parser):
assert not parser.is_reasoning_end([*self._HISTORY, _IM_START_ID, _TEXT_ID])
def test_think_end_in_current_turn_is_end(self, parser):
assert parser.is_reasoning_end(
[*self._HISTORY, _IM_START_ID, _THINK_START_ID, _TEXT_ID, _THINK_END_ID]
)
def test_reasoning_then_tool_call(parser, mock_request):
text = (
"Need the weather.\n</think>\n<tool_call>\n<function=get_weather>\n"
"<parameter=city>\nTokyo\n</parameter>\n</function>\n</tool_call>"
)
reasoning, content, tool_calls = parser.parse(
text, mock_request, enable_auto_tools=True
)
assert reasoning == "Need the weather."
assert content is None
assert tool_calls is not None
assert tool_calls[0].name == "get_weather"
assert json.loads(tool_calls[0].arguments) == {"city": "Tokyo"}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff