[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
@@ -1,288 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from transformers import AutoTokenizer, PreTrainedTokenizerBase
from tests.reasoning.utils import run_reasoning_extraction
from vllm.reasoning import ReasoningParser, ReasoningParserManager
parser_name = "deepseek_r1"
start_token = "<think>"
end_token = "</think>"
REASONING_MODEL_NAME = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
@pytest.fixture(scope="module")
def deepseek_r1_qwen_tokenizer() -> PreTrainedTokenizerBase:
return AutoTokenizer.from_pretrained(REASONING_MODEL_NAME)
SIMPLE_REASONING = {
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
}
COMPLETE_REASONING = {
"output": "This is a reasoning section</think>",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": True,
}
NO_CONTENT = {
"output": "This is content",
"reasoning": "This is content",
"content": None,
"is_reasoning_end": False,
}
NO_REASONING_STREAMING = {
"output": "This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": False,
}
MULTIPLE_LINES = {
"output": "This\nThat</think>This is the rest\nThat",
"reasoning": "This\nThat",
"content": "This is the rest\nThat",
"is_reasoning_end": True,
}
SHORTEST_REASONING_NO_STREAMING = {
"output": "</think>This is the rest",
"reasoning": "",
"content": "This is the rest",
"is_reasoning_end": True,
}
SHORTEST_REASONING = {
"output": "</think>This is the rest",
"reasoning": None,
"content": "This is the rest",
"is_reasoning_end": True,
}
REASONING_WITH_THINK = {
"output": "<think>This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
}
COMPLETE_REASONING_WITH_THINK = {
"output": "<think>This is a reasoning section</think>",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": True,
}
MULTIPLE_LINES_WITH_THINK = {
"output": "<think>This\nThat</think>This is the rest\nThat",
"reasoning": "This\nThat",
"content": "This is the rest\nThat",
"is_reasoning_end": True,
}
SHORTEST_REASONING_NO_STREAMING_WITH_THINK = {
"output": "</think>This is the rest",
"reasoning": "",
"content": "This is the rest",
"is_reasoning_end": True,
}
SHORTEST_REASONING_WITH_THINK = {
"output": "</think>This is the rest",
"reasoning": None,
"content": "This is the rest",
"is_reasoning_end": True,
}
THINK_NO_END = {
"output": "<think>This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": False,
}
EMPTY = {
"output": "",
"reasoning": "",
"content": None,
"is_reasoning_end": False,
}
EMPTY_STREAMING = {
"output": "",
"reasoning": None,
"content": None,
"is_reasoning_end": False,
}
NEW_LINE = {
"output": "\n<think>This is a reasoning section</think>\nThis is the rest",
"reasoning": "This is a reasoning section",
"content": "\nThis is the rest",
"is_reasoning_end": True,
}
# Streaming cannot handle new lines at the beginning of the output
# because we need to support <think>...</think> and </think>...
# We cannot know if the text before <think> is reasoning content
# or not.
NEW_LINE_STREAMING = {
"output": "\n<think>This is a reasoning section</think>\nThis is the rest",
"reasoning": "\nThis is a reasoning section",
"content": "\nThis is the rest",
"is_reasoning_end": True,
}
TEST_CASES = [
pytest.param(
False,
SIMPLE_REASONING,
id="simple_reasoning",
),
pytest.param(
True,
SIMPLE_REASONING,
id="simple_reasoning_streaming",
),
pytest.param(
False,
COMPLETE_REASONING,
id="complete_reasoning",
),
pytest.param(
True,
COMPLETE_REASONING,
id="complete_reasoning_streaming",
),
pytest.param(
False,
NO_CONTENT,
id="no_content_token",
),
pytest.param(
True,
NO_REASONING_STREAMING,
id="no_reasoning_token_streaming",
),
pytest.param(
False,
MULTIPLE_LINES,
id="multiple_lines",
),
pytest.param(
True,
MULTIPLE_LINES,
id="multiple_lines_streaming",
),
pytest.param(
True,
SHORTEST_REASONING,
id="shortest",
),
pytest.param(
False,
SHORTEST_REASONING_NO_STREAMING,
id="shortest_streaming",
),
pytest.param(
False,
REASONING_WITH_THINK,
id="reasoning_with_think",
),
pytest.param(
True,
REASONING_WITH_THINK,
id="reasoning_with_think_streaming",
),
pytest.param(
False,
COMPLETE_REASONING_WITH_THINK,
id="complete_reasoning_with_think",
),
pytest.param(
True,
COMPLETE_REASONING_WITH_THINK,
id="complete_reasoning_with_think_streaming",
),
pytest.param(
False,
MULTIPLE_LINES_WITH_THINK,
id="multiple_lines_with_think",
),
pytest.param(
True,
MULTIPLE_LINES_WITH_THINK,
id="multiple_lines_with_think_streaming",
),
pytest.param(
False,
SHORTEST_REASONING_NO_STREAMING_WITH_THINK,
id="shortest_with_think",
),
pytest.param(
True,
SHORTEST_REASONING_WITH_THINK,
id="shortest_with_think_streaming",
),
pytest.param(
False,
THINK_NO_END,
id="think_no_end",
),
pytest.param(
True,
THINK_NO_END,
id="think_no_end_streaming",
),
pytest.param(
False,
EMPTY,
id="empty",
),
pytest.param(
True,
EMPTY_STREAMING,
id="empty_streaming",
),
pytest.param(
False,
NEW_LINE,
id="new_line",
),
pytest.param(
True,
NEW_LINE_STREAMING,
id="new_line_streaming",
),
]
@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_reasoning(
streaming: bool,
param_dict: dict,
deepseek_r1_qwen_tokenizer,
):
output = deepseek_r1_qwen_tokenizer.tokenize(param_dict["output"])
# decode everything to tokens
output_tokens: list[str] = [
deepseek_r1_qwen_tokenizer.convert_tokens_to_string([token]) for token in output
]
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
deepseek_r1_qwen_tokenizer
)
reasoning, content = run_reasoning_extraction(
parser, output_tokens, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
# Test is_reasoning_end
output_ids = deepseek_r1_qwen_tokenizer.convert_tokens_to_ids(output)
is_reasoning_end = parser.is_reasoning_end(output_ids)
assert is_reasoning_end == param_dict["is_reasoning_end"]
# Test extract_content
if param_dict["content"] is not None:
content_ids = parser.extract_content_ids(output_ids)
assert content_ids == deepseek_r1_qwen_tokenizer.convert_tokens_to_ids(
deepseek_r1_qwen_tokenizer.tokenize(param_dict["content"])
)
else:
content_ids = parser.extract_content_ids(output)
assert content_ids == []
@@ -1,100 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from transformers import AutoTokenizer, PreTrainedTokenizerBase
from vllm.entrypoints.generate.base.protocol import DeltaMessage
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
from vllm.reasoning import ReasoningParserManager
from vllm.reasoning.deepseek_r1_reasoning_parser import DeepSeekR1ReasoningParser
from vllm.reasoning.deepseek_v3_reasoning_parser import DeepSeekV3ReasoningParser
from vllm.reasoning.identity_reasoning_parser import IdentityReasoningParser
pytestmark = pytest.mark.skip_global_cleanup
REASONING_MODEL_NAME = "deepseek-ai/DeepSeek-V3.1"
@pytest.fixture(scope="module")
def tokenizer() -> PreTrainedTokenizerBase:
return AutoTokenizer.from_pretrained(REASONING_MODEL_NAME)
@pytest.mark.parametrize(
"thinking,expected_parser_type",
[
(True, DeepSeekR1ReasoningParser),
(False, IdentityReasoningParser),
],
)
def test_parser_selection(tokenizer, thinking, expected_parser_type):
parser = DeepSeekV3ReasoningParser(
tokenizer, chat_template_kwargs={"thinking": thinking}
)
assert isinstance(parser._parser, expected_parser_type)
def test_deepseek_v4_reasoning_parser_alias():
from vllm.reasoning.deepseek_v4_engine_reasoning_parser import (
DeepSeekV4ParserReasoningAdapter,
)
parser_cls = ReasoningParserManager.get_reasoning_parser("deepseek_v4")
assert parser_cls is DeepSeekV4ParserReasoningAdapter
def test_identity_reasoning_parser_basic(tokenizer):
parser = IdentityReasoningParser(tokenizer)
# Test is_reasoning_end always returns True
input_text = "This is some output"
input_tokens = tokenizer.tokenize(input_text)
input_ids = tokenizer.convert_tokens_to_ids(input_tokens)
assert parser.is_reasoning_end(input_ids) is True
assert parser.is_reasoning_end_streaming(input_ids, input_ids) is True
# Test extract_content_ids returns all input_ids
assert parser.extract_content_ids(input_ids) == input_ids
# Test extract_reasoning returns (None, model_output)
request = ChatCompletionRequest(model="test-model", messages=[], temperature=1.0)
reasoning, content = parser.extract_reasoning(input_text, request)
assert reasoning is None
assert content == input_text
# Test extract_reasoning_streaming returns DeltaMessage or None
result = parser.extract_reasoning_streaming(
previous_text="",
current_text="Hello world",
delta_text="Hello world",
previous_token_ids=[],
current_token_ids=input_ids,
delta_token_ids=input_ids,
)
assert isinstance(result, DeltaMessage)
assert result.content == "Hello world"
# If delta_text is empty, should return None
result_none = parser.extract_reasoning_streaming(
previous_text="Hello world",
current_text="Hello world",
delta_text="",
previous_token_ids=input_ids,
current_token_ids=input_ids,
delta_token_ids=[],
)
assert result_none is None
@pytest.mark.parametrize("thinking,expected", [(True, 3), (False, 0)])
def test_count_reasoning_tokens_delegates(tokenizer, thinking, expected):
parser = DeepSeekV3ReasoningParser(
tokenizer, chat_template_kwargs={"thinking": thinking}
)
think = tokenizer.convert_tokens_to_ids(["<think>", "</think>"])
token_ids = [think[0], 11, 12, 13, think[1], 14]
assert parser.count_reasoning_tokens(token_ids) == expected
@@ -1,124 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from transformers import AutoTokenizer, PreTrainedTokenizerBase
from tests.reasoning.utils import run_reasoning_extraction
from vllm.reasoning import ReasoningParser, ReasoningParserManager
parser_name = "ernie45"
REASONING_MODEL_NAME = "baidu/ERNIE-4.5-21B-A3B-Thinking"
@pytest.fixture(scope="module")
def ernie45_tokenizer() -> PreTrainedTokenizerBase:
return AutoTokenizer.from_pretrained(REASONING_MODEL_NAME)
# 带 </think>,非stream
WITH_THINK = {
"output": "abc</think>def",
"reasoning": "abc",
"content": "def",
}
# 带 </think>,stream
WITH_THINK_STREAM = {
"output": "abc</think>def",
"reasoning": "abc",
"content": "def",
}
# without </think>, all is reasoning
WITHOUT_THINK = {
"output": "abc",
"reasoning": "abc",
"content": None,
}
# without </think>, all is reasoning
WITHOUT_THINK_STREAM = {
"output": "abc",
"reasoning": "abc",
"content": None,
}
COMPLETE_REASONING = {
"output": "abc</think>",
"reasoning": "abc",
"content": None,
}
MULTILINE_REASONING = {
"output": "abc\nABC</think>def\nDEF",
"reasoning": "abc\nABC",
"content": "def\nDEF",
}
TEST_CASES = [
pytest.param(
False,
WITH_THINK,
id="with_think",
),
pytest.param(
True,
WITH_THINK_STREAM,
id="with_think_stream",
),
pytest.param(
False,
WITHOUT_THINK,
id="without_think",
),
pytest.param(
True,
WITHOUT_THINK_STREAM,
id="without_think_stream",
),
pytest.param(
False,
COMPLETE_REASONING,
id="complete_reasoning",
),
pytest.param(
True,
COMPLETE_REASONING,
id="complete_reasoning_stream",
),
pytest.param(
False,
MULTILINE_REASONING,
id="multiline_reasoning",
),
pytest.param(
True,
MULTILINE_REASONING,
id="multiline_reasoning_stream",
),
]
@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_reasoning(
streaming: bool,
param_dict: dict,
ernie45_tokenizer,
):
output = ernie45_tokenizer.tokenize(param_dict["output"])
output_tokens: list[str] = []
for token in output:
one_token = ernie45_tokenizer.convert_tokens_to_string([token])
if one_token:
output_tokens.append(one_token)
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
ernie45_tokenizer
)
reasoning, content = run_reasoning_extraction(
parser, output_tokens, streaming=streaming
)
print()
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
@@ -1,288 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from tests.reasoning.utils import run_reasoning_extraction
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionRequest,
)
from vllm.reasoning import ReasoningParser, ReasoningParserManager
# Using mistral tokenizer as a generic mock since the actual model is not on HF
from vllm.tokenizers.registry import get_tokenizer
parser_name = "gemma4"
@pytest.fixture(scope="module")
def generic_tokenizer():
return get_tokenizer("google/gemma-4-E2B-it")
INVALID_SIMPLE_NONSTREAMING = {
"output": "This is a reasoning section<channel|>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
}
INVALID_SIMPLE_STREAMING = {
"output": "This is a reasoning section<channel|>This is the rest",
"reasoning": None,
"content": "This is a reasoning sectionThis is the rest",
"is_reasoning_end": True,
}
INVALID_COMPLETE_NONSTREAMING = {
"output": "This is a reasoning section<channel|>",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": True,
}
INVALID_COMPLETE_STREAMING = {
"output": "This is a reasoning section<channel|>",
"reasoning": None,
"content": "This is a reasoning section",
"is_reasoning_end": True,
}
NO_CONTENT = {
"output": "<|channel>This is reasoning",
"reasoning": "This is reasoning",
"content": None,
"is_reasoning_end": False,
}
NO_REASONING = {
"output": "This is content",
"reasoning": None,
"content": "This is content",
"is_reasoning_end": True,
}
REASONING_WITH_CHANNEL = {
"output": "<|channel>This is a reasoning section<channel|>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
}
COMPLETE_REASONING_WITH_CHANNEL = {
"output": "<|channel>This is a reasoning section<channel|>",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": True,
}
MULTIPLE_LINES_WITH_CHANNEL = {
"output": "<|channel>This\nThat<channel|>This is the rest\nThat",
"reasoning": "This\nThat",
"content": "This is the rest\nThat",
"is_reasoning_end": True,
}
CHANNEL_NO_END = {
"output": "<|channel>This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": False,
}
EMPTY = {
"output": "",
"reasoning": None,
"content": None,
"is_reasoning_end": True,
}
NEW_LINE_NONSTREAMING = {
"output": (
"Before\n<|channel>This is a reasoning section<channel|>\nThis is the rest"
),
"reasoning": "This is a reasoning section",
"content": "Before\n\nThis is the rest",
"is_reasoning_end": True,
}
NEW_LINE_STREAMING = {
"output": (
"Before\n<|channel>This is a reasoning section<channel|>\nThis is the rest"
),
"reasoning": "This is a reasoning section",
"content": "Before\n\nThis is the rest",
"is_reasoning_end": True,
}
THOUGHT_PREFIX = {
"output": "<|channel>thought\nActual reasoning here<channel|>Final answer",
"reasoning": "Actual reasoning here",
"content": "Final answer",
"is_reasoning_end": True,
}
THOUGHT_PREFIX_ONLY = {
"output": "<|channel>thought\n<channel|>",
"reasoning": None,
"content": None,
"is_reasoning_end": True,
}
THOUGHT_PREFIX_MULTILINE = {
"output": "<|channel>thought\nLine1\nLine2<channel|>Answer",
"reasoning": "Line1\nLine2",
"content": "Answer",
"is_reasoning_end": True,
}
# "thousand" starts like "thought" but diverges — exercises Case 2→3 in streaming.
THOUGHT_PREFIX_DIVERGE = {
"output": "<|channel>thousand reasons<channel|>Done",
"reasoning": "thousand reasons",
"content": "Done",
"is_reasoning_end": True,
}
# The model isn't reasoning if we're generating tool calls.
TOOL_CALL_STARTED = {
"output": "<|tool_call>",
"reasoning": None,
"content": "<|tool_call>",
"is_reasoning_end": True,
}
TEST_CASES = [
pytest.param(False, INVALID_SIMPLE_NONSTREAMING, id="invalid_simple"),
pytest.param(True, INVALID_SIMPLE_STREAMING, id="invalid_simple_streaming"),
pytest.param(False, INVALID_COMPLETE_NONSTREAMING, id="invalid_complete"),
pytest.param(True, INVALID_COMPLETE_STREAMING, id="invalid_complete_streaming"),
pytest.param(False, NO_CONTENT, id="no_content"),
pytest.param(False, NO_REASONING, id="no_reasoning"),
pytest.param(False, REASONING_WITH_CHANNEL, id="reasoning"),
pytest.param(True, REASONING_WITH_CHANNEL, id="reasoning_streaming"),
pytest.param(False, COMPLETE_REASONING_WITH_CHANNEL, id="complete_reasoning"),
pytest.param(
True, COMPLETE_REASONING_WITH_CHANNEL, id="complete_reasoning_streaming"
),
pytest.param(False, MULTIPLE_LINES_WITH_CHANNEL, id="multiple_lines"),
pytest.param(True, MULTIPLE_LINES_WITH_CHANNEL, id="multiple_lines_streaming"),
pytest.param(False, CHANNEL_NO_END, id="no_end"),
pytest.param(True, CHANNEL_NO_END, id="no_end_streaming"),
pytest.param(False, EMPTY, id="empty"),
pytest.param(False, NEW_LINE_NONSTREAMING, id="new_line"),
pytest.param(True, NEW_LINE_STREAMING, id="new_line_streaming"),
pytest.param(False, THOUGHT_PREFIX, id="thought_prefix"),
pytest.param(True, THOUGHT_PREFIX, id="thought_prefix_streaming"),
pytest.param(False, THOUGHT_PREFIX_ONLY, id="thought_prefix_only"),
pytest.param(True, THOUGHT_PREFIX_ONLY, id="thought_prefix_only_streaming"),
pytest.param(False, THOUGHT_PREFIX_MULTILINE, id="thought_prefix_multiline"),
pytest.param(
True, THOUGHT_PREFIX_MULTILINE, id="thought_prefix_multiline_streaming"
),
pytest.param(False, THOUGHT_PREFIX_DIVERGE, id="thought_prefix_diverge"),
pytest.param(True, THOUGHT_PREFIX_DIVERGE, id="thought_prefix_diverge_streaming"),
pytest.param(False, TOOL_CALL_STARTED, id="tool_call_started"),
pytest.param(True, TOOL_CALL_STARTED, id="tool_call_started_streaming"),
]
def gemma4_encode_output(generic_tokenizer, output: str) -> list[int]:
# Resolve token IDs dynamically from the real tokenizer
vocab = generic_tokenizer.get_vocab()
start_token_id = vocab["<|channel>"]
end_token_id = vocab["<channel|>"]
index_start = output.find("<|channel>")
len_start = len("<|channel>")
index_end = output.find("<channel|>")
len_end = len("<channel|>")
output_tokens = []
def _encode(text: str) -> list[int]:
if not text:
return []
# Handle both raw transformers and vLLM wrappers
enc = getattr(generic_tokenizer, "tokenizer", generic_tokenizer)
try:
return enc.encode(text, add_special_tokens=False)
except TypeError:
return enc.encode(text)
if index_start != -1:
output_before = output[:index_start]
output_tokens += _encode(output_before)
output_tokens += [start_token_id]
if index_end != -1:
output_middle = output[index_start + len_start : index_end]
output_after = output[index_end + len_end :]
output_tokens += _encode(output_middle)
output_tokens += [end_token_id]
output_tokens += _encode(output_after)
else:
output_middle = output[index_start + len_start :]
output_tokens += _encode(output_middle)
elif index_end != -1:
output_before = output[:index_end]
output_after = output[index_end + len_end :]
output_tokens += _encode(output_before)
output_tokens += [end_token_id]
output_tokens += _encode(output_after)
else:
output_tokens += _encode(output)
return output_tokens
@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_gemma4_reasoning(
streaming: bool,
param_dict: dict,
generic_tokenizer,
):
output = param_dict["output"]
output_tokens = gemma4_encode_output(generic_tokenizer, output)
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
generic_tokenizer
)
# We use the generic run_reasoning_extraction from utils
# Use decode per token to get standard spaces instead of
# SentencePiece space characters
output_token_strings = [generic_tokenizer.decode([t]) for t in output_tokens]
reasoning, content = run_reasoning_extraction(
parser, output_token_strings, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
# Test is_reasoning_end
is_reasoning_end = parser.is_reasoning_end(output_tokens)
assert is_reasoning_end == param_dict["is_reasoning_end"]
def test_gemma4_adjust_request(generic_tokenizer):
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
generic_tokenizer
)
request = ChatCompletionRequest(messages=[], model="test-model")
assert request.skip_special_tokens is True
result = parser.adjust_request(request)
assert result.skip_special_tokens is False
assert result is request
@pytest.mark.parametrize(
("chat_template_kwargs", "expected_is_reasoning_end"),
[
pytest.param({}, True, id="omitted_enable_thinking"),
pytest.param({"enable_thinking": False}, True, id="thinking_disabled"),
pytest.param({"enable_thinking": True}, False, id="thinking_enabled"),
],
)
def test_gemma4_new_turn_reasoning_end_matches_enable_thinking(
generic_tokenizer,
chat_template_kwargs,
expected_is_reasoning_end,
):
output = (
"<|channel>thought\n1st thought<channel|>1st content<turn|>\n"
"<|turn>user\nThanks<|turn>model\n"
)
output_tokens = gemma4_encode_output(generic_tokenizer, output)
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
generic_tokenizer,
chat_template_kwargs=chat_template_kwargs,
)
is_reasoning_end = parser.is_reasoning_end(output_tokens)
assert is_reasoning_end is expected_is_reasoning_end
@@ -1,318 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import copy
import pytest
from transformers import AutoTokenizer, PreTrainedTokenizerBase
from tests.reasoning.utils import run_reasoning_extraction
from vllm.parser.engine.adapters import ParserEngineReasoningAdapter
from vllm.parser.glm47_moe import Glm47MoeParser
from vllm.reasoning import ReasoningParser, ReasoningParserManager
parser_name = "glm45"
start_token = "<think>"
end_token = "</think>"
REASONING_MODEL_NAME = "zai-org/GLM-4.7"
@pytest.fixture(scope="module")
def glm45_tokenizer() -> PreTrainedTokenizerBase:
return AutoTokenizer.from_pretrained(REASONING_MODEL_NAME)
WITH_THINK = {
"output": "<think>This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
}
WITH_THINK_STREAM = {
"output": "<think>This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
}
WITHOUT_THINK = {
"output": "This is the rest",
"reasoning": "This is the rest",
"content": None,
"is_reasoning_end": False,
}
WITHOUT_THINK_STREAM = {
"output": "This is the rest",
"reasoning": "This is the rest",
"content": None,
"is_reasoning_end": False,
}
WITHOUT_OPEN_THINK = {
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
}
WITHOUT_OPEN_THINK_STREAM = {
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
}
COMPLETE_REASONING = {
"output": "<think>This is a reasoning section</think>",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": True,
}
MULTILINE_REASONING = {
"output": "<think>This is a reasoning\nsection</think>This is the rest\nThat",
"reasoning": "This is a reasoning\nsection",
"content": "This is the rest\nThat",
"is_reasoning_end": True,
}
ONLY_OPEN_TAG = {
"output": "<think>This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": False,
}
ONLY_OPEN_TAG_STREAM = {
"output": "<think>This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": False,
}
TEST_CASES = [
pytest.param(
False,
WITH_THINK,
id="with_think",
),
pytest.param(
True,
WITH_THINK_STREAM,
id="with_think_stream",
),
pytest.param(
False,
WITHOUT_THINK,
id="without_think",
),
pytest.param(
True,
WITHOUT_THINK_STREAM,
id="without_think_stream",
),
pytest.param(
False,
WITHOUT_OPEN_THINK,
id="without_open_think",
),
pytest.param(
True,
WITHOUT_OPEN_THINK_STREAM,
id="without_open_think_stream",
),
pytest.param(
False,
COMPLETE_REASONING,
id="complete_reasoning",
),
pytest.param(
True,
COMPLETE_REASONING,
id="complete_reasoning_stream",
),
pytest.param(
False,
MULTILINE_REASONING,
id="multiline_reasoning",
),
pytest.param(
True,
MULTILINE_REASONING,
id="multiline_reasoning_stream",
),
pytest.param(
False,
ONLY_OPEN_TAG,
id="only_open_tag",
),
pytest.param(
True,
ONLY_OPEN_TAG_STREAM,
id="only_open_tag_stream",
),
]
STILL_REASONING_PROMPT = """[gMASK]<sop><|system|>
You are a helpful assistant.<|user|>
What is the capital of France?<|assistant|>
<think>The user is asking for the capital of"""
DONE_REASONING_PROMPT = """[gMASK]<sop><|system|>
You are a helpful assistant.<|user|>
What is the capital of France?<|assistant|>
<think>The user is asking for the capital of France.</think>
The capital of France is Paris."""
MULTI_TURN_STILL_REASONING_PROMPT = """[gMASK]<sop><|system|>
You are a helpful assistant.<|user|>
What is the capital of France?<|assistant|>
<think></think>
The capital of France is Paris.<|user|>
What about Chile?<|assistant|>
<think>The user is asking for the capital of"""
MULTI_TURN_DONE_REASONING_PROMPT = """[gMASK]<sop><|system|>
You are a helpful assistant.<|user|>
What is the capital of France?<|assistant|>
<think></think>
The capital of France is Paris.<|user|>
What about Chile?<|assistant|>
<think>The user is asking for the capital of Chile.</think>
The capital of Chile is Santiago."""
REASONING_END_TEST_CASES = [
pytest.param(STILL_REASONING_PROMPT, False, id="still_reasoning"),
pytest.param(DONE_REASONING_PROMPT, True, id="done_reasoning"),
pytest.param(
MULTI_TURN_STILL_REASONING_PROMPT, False, id="multi_turn_still_reasoning"
),
pytest.param(
MULTI_TURN_DONE_REASONING_PROMPT, True, id="multi_turn_done_reasoning"
),
]
@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_reasoning(
streaming: bool,
param_dict: dict,
glm45_tokenizer,
):
output = glm45_tokenizer.tokenize(param_dict["output"])
output_tokens: list[str] = [
glm45_tokenizer.convert_tokens_to_string([token]) for token in output
]
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
glm45_tokenizer
)
reasoning, content = run_reasoning_extraction(
parser, output_tokens, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
output_ids = glm45_tokenizer.convert_tokens_to_ids(output)
is_reasoning_end = parser.is_reasoning_end(output_ids)
assert is_reasoning_end == param_dict["is_reasoning_end"]
@pytest.mark.parametrize("prompt, is_reasoning_end", REASONING_END_TEST_CASES)
def test_is_reasoning_end_full_prompt(
prompt: str, is_reasoning_end: bool, glm45_tokenizer
):
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
glm45_tokenizer
)
tokens = glm45_tokenizer.tokenize(prompt)
token_ids = glm45_tokenizer.convert_tokens_to_ids(tokens)
check_is_reasoning_end = parser.is_reasoning_end(token_ids)
assert check_is_reasoning_end == is_reasoning_end
GLM53_TEMPLATE = (
"[gMASK]<sop>\n"
"{%- set effective_reasoning_effort = reasoning_effort if reasoning_effort is"
" defined and reasoning_effort in ['low', 'high'] else 'max' -%}\n"
"<|system|>Reasoning Effort: {{ effective_reasoning_effort | capitalize }}\n"
"{% for tc in m.tool_calls %}\n"
"{{- '<tool_call>' + tc.name -}}\n"
"{% set _args = tc.arguments %}"
"{% for k, v in _args.items() %}"
"<arg_key>{{ k }}</arg_key><arg_value>{{ v }}</arg_value>"
"{% endfor %}</tool_call>\n"
"{% endfor %}\n"
"<|assistant|>{{- '<think>' -}}"
)
GLM53_LEAK = {
"output": "Simple question.</think>2 + 2 = **4**",
"reasoning": "Simple question.",
"content": "2 + 2 = **4**",
"is_reasoning_end": True,
}
@pytest.fixture()
def glm53_style_tokenizer(glm45_tokenizer):
tokenizer = copy.copy(glm45_tokenizer)
tokenizer.chat_template = GLM53_TEMPLATE
return tokenizer
def _glm_engine(parser: ReasoningParser) -> Glm47MoeParser:
assert isinstance(parser, ParserEngineReasoningAdapter)
engine = parser._parser_engine
assert isinstance(engine, Glm47MoeParser)
return engine
@pytest.mark.parametrize(
"disable_kwargs", [{"enable_thinking": False}, {"thinking": False}]
)
def test_glm53_template_forces_reasoning(disable_kwargs: dict, glm53_style_tokenizer):
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(glm53_style_tokenizer, chat_template_kwargs=disable_kwargs)
assert _glm_engine(parser).thinking_enabled
output = glm53_style_tokenizer.tokenize(GLM53_LEAK["output"])
output_tokens: list[str] = [
glm53_style_tokenizer.convert_tokens_to_string([token]) for token in output
]
reasoning, content = run_reasoning_extraction(parser, output_tokens)
assert reasoning == GLM53_LEAK["reasoning"]
assert content == GLM53_LEAK["content"]
output_ids = glm53_style_tokenizer.convert_tokens_to_ids(output)
assert parser.is_reasoning_end(output_ids) == GLM53_LEAK["is_reasoning_end"]
def test_glm53_template_forces_reasoning_streaming(glm53_style_tokenizer):
parser = ReasoningParserManager.get_reasoning_parser(parser_name)(
glm53_style_tokenizer, chat_template_kwargs={"enable_thinking": False}
)
output = glm53_style_tokenizer.tokenize(GLM53_LEAK["output"])
output_tokens: list[str] = [
glm53_style_tokenizer.convert_tokens_to_string([token]) for token in output
]
reasoning, content = run_reasoning_extraction(parser, output_tokens, streaming=True)
assert reasoning == GLM53_LEAK["reasoning"]
assert content == GLM53_LEAK["content"]
def test_glm47_template_honors_thinking_disable(glm45_tokenizer):
parser = ReasoningParserManager.get_reasoning_parser(parser_name)(
glm45_tokenizer, chat_template_kwargs={"enable_thinking": False}
)
assert not _glm_engine(parser).thinking_enabled
output = glm45_tokenizer.tokenize(GLM53_LEAK["output"])
output_tokens: list[str] = [
glm45_tokenizer.convert_tokens_to_string([token]) for token in output
]
reasoning, content = run_reasoning_extraction(parser, output_tokens)
assert reasoning is None
assert content == GLM53_LEAK["output"]
@@ -1,344 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from transformers import AutoTokenizer
from tests.reasoning.utils import DeltaMessage, run_reasoning_extraction
from vllm.reasoning import ReasoningParser, ReasoningParserManager
parser_name = "granite"
START_REASONING = "Here is my thought process:"
START_RESPONSE = "Here is my response:"
SIMPLE_REASONING = {
"output": f"{START_REASONING}This is a reasoning section{START_RESPONSE}This is the rest", # noqa: E501
"reasoning": "This is a reasoning section",
"content": "This is the rest",
}
COMPLETE_REASONING = {
"output": f"{START_REASONING}This is a reasoning section{START_RESPONSE}",
"reasoning": "This is a reasoning section",
"content": None,
}
NO_REASONING = {
"output": "This is content",
"reasoning": None,
"content": "This is content",
}
MULTIPLE_LINES = {
"output": f"{START_REASONING}This\nThat{START_RESPONSE}This is the rest\nThat",
"reasoning": "This\nThat",
"content": "This is the rest\nThat",
}
REASONING_WITH_THINK = {
"output": f"{START_REASONING}This is a reasoning section{START_RESPONSE}This is the rest", # noqa: E501
"reasoning": "This is a reasoning section",
"content": "This is the rest",
}
COMPLETE_REASONING_WITH_THINK = {
"output": f"{START_REASONING}This is a reasoning section{START_RESPONSE}",
"reasoning": "This is a reasoning section",
"content": None,
}
MULTIPLE_LINES_WITH_THINK = {
"output": f"{START_REASONING}This\nThat{START_RESPONSE}This is the rest\nThat",
"reasoning": "This\nThat",
"content": "This is the rest\nThat",
}
TEST_CASES = [
pytest.param(
False,
SIMPLE_REASONING,
id="simple_reasoning",
),
pytest.param(
False,
COMPLETE_REASONING,
id="complete_reasoning",
),
pytest.param(
False,
NO_REASONING,
id="no_reasoning",
),
pytest.param(
False,
MULTIPLE_LINES,
id="multiple_lines",
),
pytest.param(
False,
REASONING_WITH_THINK,
id="reasoning_with_think",
),
pytest.param(
False,
COMPLETE_REASONING_WITH_THINK,
id="complete_reasoning_with_think",
),
pytest.param(
False,
MULTIPLE_LINES_WITH_THINK,
id="multiple_lines_with_think",
),
pytest.param(
True,
SIMPLE_REASONING,
id="simple_reasoning_streaming",
),
pytest.param(
True,
COMPLETE_REASONING,
id="complete_reasoning_streaming",
),
pytest.param(
True,
NO_REASONING,
id="no_reasoning_streaming",
),
pytest.param(
True,
MULTIPLE_LINES,
id="multiple_lines_streaming",
),
pytest.param(
True,
REASONING_WITH_THINK,
id="reasoning_with_think_streaming",
),
pytest.param(
True,
COMPLETE_REASONING_WITH_THINK,
id="complete_reasoning_with_think_streaming",
),
pytest.param(
True,
MULTIPLE_LINES_WITH_THINK,
id="multiple_lines_with_think_streaming",
),
]
# Global tokenizer initialization to avoid repeated loading
tokenizer = AutoTokenizer.from_pretrained("facebook/opt-125m")
@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_reasoning(
streaming: bool,
param_dict: dict,
):
output = tokenizer.tokenize(param_dict["output"])
# decode everything to tokens
output_tokens: list[str] = [
tokenizer.convert_tokens_to_string([token]) for token in output
]
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
tokenizer
)
reasoning, content = run_reasoning_extraction(
parser, output_tokens, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
# Additional tests for verifying the correctness of granite streaming; this
# is complicated because granite uses multiple tokens to indicate when thinking
# is starting / when it's starting its response, so skipping special tokens
# is awkward.
### Handling the start of reasoning
STREAMING_1 = {
"previous_text": None,
"current_text": "Here",
"delta_text": "Here",
"reasoning": None,
"content": None,
}
# When we fail, we should give what was previously being silenced first
STREAMING_2 = {
"previous_text": "Here is my thought",
"current_text": "Here is my thought failure",
"delta_text": " failure",
"reasoning": None,
"content": "Here is my thought failure",
}
# But then after the first one, we should only add the delta text to content
STREAMING_3 = {
"previous_text": "Here wrong",
"current_text": " words",
"delta_text": " Here wrong words",
"reasoning": None,
"content": " words",
}
# But then after the first one, we should only add the delta text to content
STREAMING_4 = {
"previous_text": "Here is my thought",
"current_text": "Here is my thought process:",
"delta_text": " process:",
"reasoning": None,
"content": None,
}
# Reasoning started successfully; parse reasoning content
STREAMING_5 = {
"previous_text": "Here is my thought process:",
"current_text": "Here is my thought process: foo",
"delta_text": " foo",
"reasoning": " foo",
"content": None,
}
# Response special sequence has started, but not finished.
STREAMING_6 = {
"previous_text": "Here is my thought process: foo",
"current_text": "Here is my thought process: foo Here is",
"delta_text": " Here is",
"reasoning": " ",
"content": None,
}
# Response special sequence started, but was broken; the reasoning
# content should be the content that was previously unused.
STREAMING_7 = {
"previous_text": "Here is my thought process: foo Here is",
"current_text": "Here is my thought process: foo Here is Here",
"delta_text": " Here",
"reasoning": "Here is ",
"content": None,
}
# Response special sequence is ongoing
STREAMING_8 = {
"previous_text": "Here is my thought process: foo Here is my response:",
"current_text": "Here is my thought process: foo Here is my response: bar",
"delta_text": " bar",
"reasoning": None,
"content": " bar",
}
# The delta text has everything; we should be able to correctly parse both
STREAMING_9 = {
"previous_text": None,
"current_text": "Here is my thought process: foo Here is my response: bar",
"delta_text": "Here is my thought process: foo Here is my response: bar",
"reasoning": " foo ",
"content": " bar",
}
## The Response is ongoing, and the delta mixes reasoning content / content
STREAMING_10 = {
"previous_text": "Here is my thought process: foo",
"current_text": "Here is my thought process: foo bar Here is my response: baz",
"delta_text": " bar Here is my response: baz",
"reasoning": " bar ",
"content": " baz",
}
# The delta text starts a new substring that might be a response special seq
STREAMING_11 = {
"previous_text": "Here is my thought process: This is a reasoning section ",
"current_text": "Here is my thought process: This is a reasoning section Here",
"delta_text": "Here",
"reasoning": None,
"content": None,
}
# The delta text is finishing the response special seq
STREAMING_12 = {
"previous_text": "Here is my thought process: foo Here is my response",
"current_text": "Here is my thought process: foo Here is my response:",
"delta_text": ":",
"reasoning": None,
"content": None,
}
STREAMING_13 = {
"previous_text": "Here is my thought process: foo Here",
"current_text": "Here is my thought process: foo Here was",
"delta_text": " was",
"reasoning": "Here was",
"content": None,
}
STREAMING_SUBCASES = [
pytest.param(
STREAMING_1,
id="Starting reasoning special sequence",
),
pytest.param(
STREAMING_2,
id="Unexpected start reasoning sequence",
),
pytest.param(
STREAMING_3,
id="Continuing unexpected start reasoning sequence",
),
pytest.param(
STREAMING_4,
id="Only start reasoning sequence and nothing else",
),
pytest.param(
STREAMING_5,
id="Reasoning content has started",
),
pytest.param(
STREAMING_6,
id="Response special sequence has started",
),
pytest.param(
STREAMING_7,
id="Response special sequence reset",
),
pytest.param(
STREAMING_8,
id="Response text has started",
),
pytest.param(
STREAMING_9,
id="Delta contains everything",
),
pytest.param(
STREAMING_10,
id="Delta contains some reasoning and response",
),
pytest.param(
STREAMING_11,
id="Delta starts response sequence",
),
pytest.param(
STREAMING_12,
id="Delta finishes response sequence",
),
pytest.param(
STREAMING_13,
id="Delta breaks potential responise sequence",
),
]
@pytest.mark.parametrize("param_dict", STREAMING_SUBCASES)
def test_streaming_subcases(param_dict):
# Get all of the token IDs
previous_token_ids = (
tokenizer.encode(param_dict["previous_text"])
if param_dict["previous_text"] is not None
else []
)
current_token_ids = tokenizer.encode(param_dict["current_text"])
delta_token_ids = tokenizer.encode(param_dict["delta_text"])
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
tokenizer
)
response = parser.extract_reasoning_streaming(
previous_text=param_dict["previous_text"],
current_text=param_dict["current_text"],
delta_text=param_dict["delta_text"],
previous_token_ids=previous_token_ids,
current_token_ids=current_token_ids,
delta_token_ids=delta_token_ids,
)
# Streaming currently expects at least one of reasoning content / content,
# so the response should return None in that case.
if param_dict["reasoning"] is None and param_dict["content"] is None:
assert response is None
else:
assert isinstance(response, DeltaMessage)
assert param_dict["reasoning"] == response.reasoning
assert param_dict["content"] == response.content
@@ -1,644 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from typing import TypedDict, cast
import pytest
import regex as re
from transformers import PreTrainedTokenizerBase
from tests.reasoning.utils import run_reasoning_extraction
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionRequest,
)
from vllm.parser.abstract_parser import DelegatingParser
from vllm.parser.engine.registered_adapters import (
GraniteThinkingParserReasoningAdapter,
)
from vllm.reasoning import ReasoningParser, ReasoningParserManager
parser_name = "granite_thinking_parser"
class ReasoningCase(TypedDict):
output: str
reasoning: str | None
content: str | None
class GraniteThinkingTokenizer:
def __init__(self):
self._vocab = {
"<think>": 1,
"</think>": 2,
}
self._inv_vocab = {v: k for k, v in self._vocab.items()}
self._pattern = re.compile(r"(<think>|</think>)")
def get_vocab(self) -> dict[str, int]:
return self._vocab
def tokenize(self, text: str) -> list[str]:
tokens: list[str] = []
for part in self._pattern.split(text):
if part:
tokens.append(part)
return tokens
def convert_tokens_to_string(self, tokens: list[str]) -> str:
return "".join(tokens)
def decode(self, token_ids: list[int]) -> str:
return "".join(self._inv_vocab.get(tid, f"<unk:{tid}>") for tid in token_ids)
@pytest.fixture
def tokenizer() -> PreTrainedTokenizerBase:
return cast(PreTrainedTokenizerBase, GraniteThinkingTokenizer())
# ── Basic reasoning extraction (non-streaming + streaming) ───────────
@pytest.mark.parametrize(
"streaming,param_dict",
[
pytest.param(
False,
{
"output": "<think>reasoning</think>\nHello",
"reasoning": "reasoning",
"content": "Hello",
},
id="leading_newline_stripped",
),
pytest.param(
True,
{
"output": "<think>reasoning</think>\nHello",
"reasoning": "reasoning",
"content": "Hello",
},
id="leading_newline_stripped_streaming",
),
pytest.param(
False,
{
"output": "<think>r</think>c",
"reasoning": "r",
"content": "c",
},
id="simple_reasoning",
),
pytest.param(
True,
{
"output": "<think>r</think>c",
"reasoning": "r",
"content": "c",
},
id="simple_reasoning_streaming",
),
pytest.param(
False,
{
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
},
id="without_start_token",
),
pytest.param(
True,
{
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
},
id="without_start_token_streaming",
),
pytest.param(
False,
{
"output": "<think>This is a reasoning section</think>This is the rest", # noqa: E501
"reasoning": "This is a reasoning section",
"content": "This is the rest",
},
id="with_start_token",
),
pytest.param(
True,
{
"output": "<think>This is a reasoning section</think>This is the rest", # noqa: E501
"reasoning": "This is a reasoning section",
"content": "This is the rest",
},
id="with_start_token_streaming",
),
pytest.param(
False,
{
"output": "<think>reasoning</think>\n\n\nHello",
"reasoning": "reasoning",
"content": "Hello",
},
id="multiple_leading_newlines_stripped",
),
pytest.param(
True,
{
"output": "<think>reasoning</think>\n\n\nHello",
"reasoning": "reasoning",
"content": "Hello",
},
id="multiple_leading_newlines_stripped_streaming",
),
pytest.param(
False,
{
"output": "<think>line1\nline2</think>\nresult1\nresult2",
"reasoning": "line1\nline2",
"content": "result1\nresult2",
},
id="multiline_reasoning_and_content",
),
pytest.param(
True,
{
"output": "<think>line1\nline2</think>\nresult1\nresult2",
"reasoning": "line1\nline2",
"content": "result1\nresult2",
},
id="multiline_reasoning_and_content_streaming",
),
],
)
def test_granite_thinking_reasoning(
tokenizer: PreTrainedTokenizerBase,
streaming: bool,
param_dict: ReasoningCase,
):
output = tokenizer.tokenize(param_dict["output"])
model_output = [tokenizer.convert_tokens_to_string([token]) for token in output]
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
tokenizer
)
reasoning, content = run_reasoning_extraction(
parser, model_output, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
# ── No content after end token ───────────────────────────────────────
def test_granite_thinking_no_content_after_end_token(
tokenizer: PreTrainedTokenizerBase,
):
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
reasoning, content = run_reasoning_extraction(
parser,
["<think>", "reasoning", "</think>"],
streaming=False,
)
assert reasoning == "reasoning"
assert content is None
# ── Whitespace-only content after end token ──────────────────────────
@pytest.mark.parametrize("streaming", [False, True])
def test_granite_thinking_whitespace_only_content(
tokenizer: PreTrainedTokenizerBase,
streaming: bool,
):
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
reasoning, content = run_reasoning_extraction(
parser,
["<think>", "reasoning", "</think>", "\n\n"],
streaming=streaming,
)
assert reasoning == "reasoning"
assert content is None
# ── Unterminated think block ─────────────────────────────────────────
def test_granite_thinking_unterminated_think_block(
tokenizer: PreTrainedTokenizerBase,
):
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"enable_thinking": True},
)
reasoning, content = run_reasoning_extraction(
parser,
["<think>", "reasoning only"],
request=request,
streaming=False,
)
assert reasoning == "reasoning only"
assert content is None
# ── enable_thinking=False ────────────────────────────────────────────
def test_granite_thinking_disabled_moves_into_content(
tokenizer: PreTrainedTokenizerBase,
):
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"enable_thinking": False},
)
reasoning, content = run_reasoning_extraction(
parser,
["This is plain content"],
request=request,
streaming=False,
)
assert reasoning is None
assert content == "This is plain content"
# ── enable_thinking=False + leading newline (§6.2 ordering) ─────────
def test_granite_thinking_disabled_with_leading_newline(
tokenizer: PreTrainedTokenizerBase,
):
# With enable_thinking=False, model output goes through the swap
# path: content is initially None (all text classified as
# reasoning), so lstrip doesn't fire pre-swap. After the swap,
# any leading \n in the original output is preserved. In practice,
# enable_thinking=False output has no template-injected \n, so
# this is a correctness check, not a realistic scenario.
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"enable_thinking": False},
)
reasoning, content = run_reasoning_extraction(
parser,
["\nThis is plain content"],
request=request,
streaming=False,
)
assert reasoning is None
assert content == "\nThis is plain content"
# ── force_nonempty_content=True ──────────────────────────────────────
def test_granite_thinking_force_nonempty_content_moves_into_content(
tokenizer: PreTrainedTokenizerBase,
):
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"force_nonempty_content": True},
)
reasoning, content = run_reasoning_extraction(
parser,
["<think>", "This is plain content"],
request=request,
streaming=False,
)
assert reasoning is None
assert content == "This is plain content"
def test_granite_thinking_force_nonempty_no_swap_when_newlines_only(
tokenizer: PreTrainedTokenizerBase,
):
# When </think> IS present and content is newlines-only, lstrip
# removes them but the swap should NOT fire — content was present,
# just whitespace. Matches the HF plugin behavior.
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"force_nonempty_content": True},
)
reasoning, content = run_reasoning_extraction(
parser,
["<think>", "reasoning", "</think>", "\n\n"],
request=request,
streaming=False,
)
assert reasoning == "reasoning"
assert content is None
def test_granite_thinking_force_nonempty_swaps_when_content_absent(
tokenizer: PreTrainedTokenizerBase,
):
# When </think> IS present but content is truly absent (zero
# characters after </think>, e.g. max_tokens cut), swap fires.
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"force_nonempty_content": True},
)
reasoning, content = run_reasoning_extraction(
parser,
["<think>", "reasoning", "</think>"],
request=request,
streaming=False,
)
assert reasoning is None
assert content == "reasoning"
def test_granite_thinking_force_nonempty_keeps_real_content(
tokenizer: PreTrainedTokenizerBase,
):
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"force_nonempty_content": True},
)
reasoning, content = run_reasoning_extraction(
parser,
["<think>", "reasoning here", "</think>", "real answer"],
request=request,
streaming=False,
)
assert reasoning == "reasoning here"
assert content == "real answer"
# ── Truncated reasoning with thinking on ─────────────────────────────
def test_granite_thinking_keeps_truncated_reasoning(
tokenizer: PreTrainedTokenizerBase,
):
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"enable_thinking": True},
)
reasoning, content = run_reasoning_extraction(
parser,
["This is truncated reasoning"],
request=request,
streaming=False,
)
assert reasoning == "This is truncated reasoning"
assert content is None
# ── DelegatingParser / parse_delta streaming tests ───────────────────
_SPECIAL_TOKEN_IDS = {"<think>": 1, "</think>": 2}
def _token_id(token: str) -> int:
return _SPECIAL_TOKEN_IDS.get(token, 0)
def _make_reasoning_parser(tokenizer):
class _GraniteThinkingDelegating(DelegatingParser):
reasoning_parser_cls = GraniteThinkingParserReasoningAdapter
tool_parser_cls = None
return _GraniteThinkingDelegating(tokenizer)
def _run_parse_delta(parser, tokenizer, text, request):
tokens = tokenizer.tokenize(text)
reasoning_parts: list[str] = []
content_parts: list[str] = []
for i, token in enumerate(tokens):
delta = parser.parse_delta(
delta_text=token,
delta_token_ids=[_token_id(token)],
request=request,
prompt_token_ids=[] if i == 0 else None,
finished=(i == len(tokens) - 1),
)
if delta is None:
continue
if delta.reasoning:
reasoning_parts.append(delta.reasoning)
if delta.content:
content_parts.append(delta.content)
return "".join(reasoning_parts), "".join(content_parts)
def test_granite_thinking_streaming_enable_thinking_false(
tokenizer: PreTrainedTokenizerBase,
):
# With enable_thinking=False, the parser (constructed without
# kwargs) starts in REASONING state. All text streams as reasoning
# AND is duplicated into content via the streaming fallback —
# matching NemotronV3 behavior.
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"enable_thinking": False},
)
parser = _make_reasoning_parser(tokenizer)
reasoning, content = _run_parse_delta(
parser, tokenizer, "This is plain content", request
)
assert reasoning == "This is plain content"
assert content == "This is plain content"
def test_granite_thinking_streaming_strips_leading_newline(
tokenizer: PreTrainedTokenizerBase,
):
request = ChatCompletionRequest(
model="test-model",
messages=[],
)
parser = _make_reasoning_parser(tokenizer)
reasoning, content = _run_parse_delta(
parser, tokenizer, "<think>reason</think>\nHello", request
)
assert reasoning == "reason"
assert content == "Hello"
def test_granite_thinking_streaming_promotes_reasoning_to_content(
tokenizer: PreTrainedTokenizerBase,
):
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"force_nonempty_content": True},
)
parser = _make_reasoning_parser(tokenizer)
reasoning, content = _run_parse_delta(parser, tokenizer, "<think>4", request)
assert reasoning == "4"
assert content == "4"
def test_granite_thinking_streaming_no_promotion_with_real_content(
tokenizer: PreTrainedTokenizerBase,
):
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"force_nonempty_content": True},
)
parser = _make_reasoning_parser(tokenizer)
reasoning, content = _run_parse_delta(
parser,
tokenizer,
"<think>reason</think>real answer",
request,
)
assert reasoning == "reason"
assert content == "real answer"
def test_granite_thinking_streaming_no_promotion_without_opt_in(
tokenizer: PreTrainedTokenizerBase,
):
request = ChatCompletionRequest(model="test-model", messages=[])
parser = _make_reasoning_parser(tokenizer)
reasoning, content = _run_parse_delta(parser, tokenizer, "<think>4", request)
assert reasoning == "4"
assert content == ""
# ── Empty think block ────────────────────────────────────────────────
@pytest.mark.parametrize("streaming", [False, True])
def test_granite_thinking_empty_think_block(
tokenizer: PreTrainedTokenizerBase,
streaming: bool,
):
# <think></think>\nHello — empty reasoning, content after newline.
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
reasoning, content = run_reasoning_extraction(
parser,
["<think>", "</think>", "\n", "Hello"],
streaming=streaming,
)
assert reasoning is None or reasoning == ""
assert content == "Hello"
# ── Chunking-independence streaming test ─────────────────────────────
@pytest.mark.parametrize(
"text,expected_reasoning,expected_content",
[
("<think>r</think>\nHello", "r", "Hello"),
("<think></think>\nHello", "", "Hello"),
],
)
def test_granite_thinking_streaming_chunking_independent(
tokenizer: PreTrainedTokenizerBase,
text: str,
expected_reasoning: str,
expected_content: str,
):
# Verify the same input produces identical results regardless
# of how it's chunked into streaming deltas. Uses the
# DelegatingParser path (_run_parse_delta) which matches the
# real serving flow.
tokens = tokenizer.tokenize(text)
request = ChatCompletionRequest(model="test-model", messages=[])
chunk_patterns = [
tokens,
# Split at the </think> boundary
[
tokenizer.convert_tokens_to_string(tokens[: tokens.index("</think>") + 1]),
tokenizer.convert_tokens_to_string(tokens[tokens.index("</think>") + 1 :]),
],
]
for chunks in chunk_patterns:
parser = _make_reasoning_parser(tokenizer)
reasoning_parts: list[str] = []
content_parts: list[str] = []
all_tokens = []
for chunk in chunks:
chunk_tokens = tokenizer.tokenize(chunk)
all_tokens.extend(chunk_tokens)
for i, token in enumerate(all_tokens):
delta = parser.parse_delta(
delta_text=token,
delta_token_ids=[_token_id(token)],
request=request,
prompt_token_ids=[] if i == 0 else None,
finished=(i == len(all_tokens) - 1),
)
if delta is None:
continue
if delta.reasoning:
reasoning_parts.append(delta.reasoning)
if delta.content:
content_parts.append(delta.content)
reasoning = "".join(reasoning_parts)
content = "".join(content_parts)
assert reasoning == expected_reasoning or (
not expected_reasoning and not reasoning
), f"reasoning mismatch with chunks={chunks}"
assert content == expected_content, f"content mismatch with chunks={chunks}"
@@ -1,190 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from transformers import AutoTokenizer, PreTrainedTokenizerBase
from tests.reasoning.utils import run_reasoning_extraction
from vllm.reasoning import ReasoningParser, ReasoningParserManager
from vllm.reasoning.deepseek_r1_reasoning_parser import DeepSeekR1ReasoningParser
from vllm.reasoning.deepseek_v3_reasoning_parser import (
DeepSeekV3ReasoningWithThinkingParser as Holo2ReasoningParser,
)
from vllm.reasoning.identity_reasoning_parser import IdentityReasoningParser
REASONING_MODEL_NAME = "HCompany/Holo2-4B"
@pytest.fixture(scope="module")
def tokenizer() -> PreTrainedTokenizerBase:
return AutoTokenizer.from_pretrained(REASONING_MODEL_NAME)
@pytest.mark.parametrize(
"thinking,expected_parser_type",
[
(True, DeepSeekR1ReasoningParser),
(False, IdentityReasoningParser),
],
)
def test_parser_selection(tokenizer, thinking, expected_parser_type):
parser = Holo2ReasoningParser(
tokenizer,
chat_template_kwargs={
"thinking": thinking,
},
)
assert isinstance(parser._parser, expected_parser_type)
def test_holo2_default_parser_is_deepseekr1(tokenizer):
parser = Holo2ReasoningParser(tokenizer)
assert isinstance(parser._parser, DeepSeekR1ReasoningParser)
def test_holo2_supports_structured_output(tokenizer):
# Structured output manager uses the reasoning parser to check if the
# reasoning content is ended before applying the grammar. The main function
# used is is_reasoning_end. This test checks if the parser is able to
# correctly identify the end of the reasoning content.
# important to not pass chat_template_kwargs here as it is done in the
# StructuredOutputManager
parser = Holo2ReasoningParser(tokenizer)
end_token_id = tokenizer.encode("</think>", add_special_tokens=False)[0]
assert parser.is_reasoning_end([1, 2, 4, end_token_id])
assert not parser.is_reasoning_end([1, 2, 4])
assert parser.is_reasoning_end([1, 2, 4, end_token_id, 5])
# thinking is True, non-streaming
WITH_THINK = {
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
}
# thinking is True, streaming
WITH_THINK_STREAM = {
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
}
# thinking is False, non-streaming
THINKING_DISABLED = {
"output": "This is the rest",
"reasoning": None,
"content": "This is the rest",
}
# thinking is False, streaming
THINKING_DISABLED_STREAM = {
"output": "This is the rest",
"reasoning": None,
"content": "This is the rest",
}
# thinking is False but the model output </think>, non-streaming
THINKING_DISABLED_WITH_CLOSE_TAG = {
"output": "</think>This is the rest",
"reasoning": None,
"content": "</think>This is the rest",
}
# thinking is False but the model output </think>, streaming
THINKING_DISABLED_WITH_CLOSE_TAG_STREAM = {
"output": "some text</think>This is the rest",
"reasoning": None,
"content": "some text</think>This is the rest",
}
COMPLETE_REASONING = {
"output": "This is a reasoning section</think>",
"reasoning": "This is a reasoning section",
"content": None,
}
TEST_CASES = [
pytest.param(
False,
WITH_THINK,
None,
id="with_think",
),
pytest.param(
True,
WITH_THINK_STREAM,
None,
id="with_think_stream",
),
pytest.param(
False,
WITH_THINK,
{"thinking": True},
id="with_think_enabled",
),
pytest.param(
True,
WITH_THINK_STREAM,
{"thinking": True},
id="with_think_stream_enabled",
),
pytest.param(
False,
THINKING_DISABLED,
{"thinking": False},
id="thinking_disabled",
),
pytest.param(
True,
THINKING_DISABLED_STREAM,
{"thinking": False},
id="thinking_disabled_stream",
),
pytest.param(
False,
THINKING_DISABLED_WITH_CLOSE_TAG,
{"thinking": False},
id="thinking_disabled_with_close_tag",
),
pytest.param(
True,
THINKING_DISABLED_WITH_CLOSE_TAG_STREAM,
{"thinking": False},
id="thinking_disabled_with_close_tag_stream",
),
pytest.param(
False,
COMPLETE_REASONING,
None,
id="complete_reasoning",
),
pytest.param(
True,
COMPLETE_REASONING,
None,
id="complete_reasoning_stream",
),
]
@pytest.mark.parametrize("streaming, param_dict, chat_template_kwargs", TEST_CASES)
def test_reasoning(
streaming: bool,
param_dict: dict,
chat_template_kwargs: dict | None,
tokenizer,
):
output = tokenizer.tokenize(param_dict["output"])
output_tokens: list[str] = [
tokenizer.convert_tokens_to_string([token]) for token in output
]
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser("holo2")(
tokenizer,
chat_template_kwargs=chat_template_kwargs,
)
reasoning, content = run_reasoning_extraction(
parser, output_tokens, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
@@ -1,168 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from transformers import AutoTokenizer
from tests.reasoning.utils import run_reasoning_extraction
from vllm.reasoning import ReasoningParser, ReasoningParserManager
parser_name = "hunyuan_a13b"
START_REASONING = "<think>\n"
START_RESPONSE = "\n</think>\n<answer>\n"
END_RESPONSE = "\n</answer>"
NO_REASONING_QUICK_THOUGHT = {
"output": f"{START_REASONING}{START_RESPONSE}This is the rest{END_RESPONSE}", # noqa: E501
"reasoning": None,
"content": "This is the rest",
}
SIMPLE_REASONING = {
"output": f"{START_REASONING}This is a reasoning section{START_RESPONSE}This is the rest{END_RESPONSE}", # noqa: E501
"reasoning": "This is a reasoning section",
"content": "This is the rest",
}
COMPLETE_REASONING = {
"output": f"{START_REASONING}This is a reasoning section{START_RESPONSE}",
"reasoning": "This is a reasoning section",
"content": None,
}
COMPLETE_REASONING_WITH_SYMBOL = {
"output": f"{START_REASONING}This is a reasoning section!{START_RESPONSE}",
"reasoning": "This is a reasoning section!",
"content": None,
}
NO_REASONING = {
"output": "This is content",
"reasoning": None,
"content": "This is content",
}
MULTIPLE_LINES = {
"output": f"{START_REASONING}This\nThat{START_RESPONSE}This is the rest\nThat",
"reasoning": "This\nThat",
"content": "This is the rest\nThat",
}
REASONING_WITH_THINK = {
"output": f"{START_REASONING}This is a reasoning section{START_RESPONSE}This is the rest", # noqa: E501
"reasoning": "This is a reasoning section",
"content": "This is the rest",
}
COMPLETE_REASONING_WITH_THINK = {
"output": f"{START_REASONING}This is a reasoning section{START_RESPONSE}",
"reasoning": "This is a reasoning section",
"content": None,
}
MULTIPLE_LINES_WITH_THINK = {
"output": f"{START_REASONING}This\nThat{START_RESPONSE}This is the rest\nThat",
"reasoning": "This\nThat",
"content": "This is the rest\nThat",
}
TEST_CASES = [
pytest.param(
False,
SIMPLE_REASONING,
id="simple_reasoning",
),
pytest.param(
False,
COMPLETE_REASONING,
id="complete_reasoning",
),
pytest.param(
False,
COMPLETE_REASONING_WITH_SYMBOL,
id="complete_reasoning_with_symbol",
),
pytest.param(
False,
NO_REASONING,
id="no_reasoning",
),
pytest.param(False, NO_REASONING_QUICK_THOUGHT, id="no_reasoning_quick"),
pytest.param(
False,
MULTIPLE_LINES,
id="multiple_lines",
),
pytest.param(
False,
REASONING_WITH_THINK,
id="reasoning_with_think",
),
pytest.param(
False,
COMPLETE_REASONING_WITH_THINK,
id="complete_reasoning_with_think",
),
pytest.param(
False,
MULTIPLE_LINES_WITH_THINK,
id="multiple_lines_with_think",
),
pytest.param(
True,
SIMPLE_REASONING,
id="simple_reasoning_streaming",
),
pytest.param(
True,
COMPLETE_REASONING,
id="complete_reasoning_streaming",
),
pytest.param(
True,
NO_REASONING,
id="no_reasoning_streaming",
),
pytest.param(True, NO_REASONING_QUICK_THOUGHT, id="no_reasoning_quick_stream"),
pytest.param(
True,
MULTIPLE_LINES,
id="multiple_lines_streaming",
),
pytest.param(
True,
REASONING_WITH_THINK,
id="reasoning_with_think_streaming",
),
pytest.param(
True,
COMPLETE_REASONING_WITH_THINK,
id="complete_reasoning_with_think_streaming",
),
pytest.param(
True,
MULTIPLE_LINES_WITH_THINK,
id="multiple_lines_with_think_streaming",
),
]
# Global tokenizer initialization to avoid repeated loading
tokenizer = AutoTokenizer.from_pretrained(
"tencent/Hunyuan-A13B-Instruct", trust_remote_code=True
)
@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_reasoning(
streaming: bool,
param_dict: dict,
):
output = tokenizer.tokenize(param_dict["output"])
# decode everything to tokens
output_tokens: list[str] = [
tokenizer.convert_tokens_to_string([token]) for token in output
]
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
tokenizer
)
reasoning, content = run_reasoning_extraction(
parser, output_tokens, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
@@ -1,274 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from tests.reasoning.utils import run_reasoning_extraction
from vllm.reasoning import ReasoningParser, ReasoningParserManager
from vllm.reasoning.hy_v3_reasoning_parser import HYV3ReasoningParser
from vllm.tokenizers import get_tokenizer
parser_name = "hy_v3"
MODEL = "tencent/Hy3-preview"
@pytest.fixture(scope="module")
def hy_v3_tokenizer():
return get_tokenizer(tokenizer_name=MODEL)
WITH_THINK = {
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
"reasoning_effort": "high",
}
WITH_THINK_STREAM = {
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
"reasoning_effort": "high",
}
WITHOUT_THINK = {
"output": "This is the rest",
"reasoning": None,
"content": "This is the rest",
"is_reasoning_end": True,
"reasoning_effort": "no_think",
}
WITHOUT_THINK_STREAM = {
"output": "This is the rest",
"reasoning": None,
"content": "This is the rest",
"is_reasoning_end": True,
"reasoning_effort": "no_think",
}
WITH_REASONING_EFFORT_NONE = {
"output": "This is the rest",
"reasoning": None,
"content": "This is the rest",
"is_reasoning_end": True,
}
WITH_REASONING_EFFORT_NONE_STREAM = {
"output": "This is the rest",
"reasoning": None,
"content": "This is the rest",
"is_reasoning_end": True,
}
COMPLETE_REASONING = {
"output": "This is a reasoning section</think>",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": True,
"reasoning_effort": "high",
}
MULTILINE_REASONING = {
"output": "This is a reasoning\nsection</think>This is the rest\nThat",
"reasoning": "This is a reasoning\nsection",
"content": "This is the rest\nThat",
"is_reasoning_end": True,
"reasoning_effort": "high",
}
ONLY_OPEN_TAG = {
"output": "This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": False,
"reasoning_effort": "high",
}
ONLY_OPEN_TAG_STREAM = {
"output": "This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": False,
"reasoning_effort": "high",
}
TEST_CASES = [
pytest.param(
False,
WITH_THINK,
id="with_think",
),
pytest.param(
True,
WITH_THINK_STREAM,
id="with_think_stream",
),
pytest.param(
False,
WITHOUT_THINK,
id="without_think",
),
pytest.param(
True,
WITHOUT_THINK_STREAM,
id="without_think_stream",
),
pytest.param(
False,
WITH_REASONING_EFFORT_NONE,
id="with_reasoning_effort_none",
),
pytest.param(
True,
WITH_REASONING_EFFORT_NONE_STREAM,
id="with_reasoning_effort_none_stream",
),
pytest.param(
False,
COMPLETE_REASONING,
id="complete_reasoning",
),
pytest.param(
True,
COMPLETE_REASONING,
id="complete_reasoning_stream",
),
pytest.param(
False,
MULTILINE_REASONING,
id="multiline_reasoning",
),
pytest.param(
True,
MULTILINE_REASONING,
id="multiline_reasoning_stream",
),
pytest.param(
False,
ONLY_OPEN_TAG,
id="only_open_tag",
),
pytest.param(
True,
ONLY_OPEN_TAG_STREAM,
id="only_open_tag_stream",
),
]
STILL_REASONING_PROMPT = """<|hy_begin▁of▁sentence|>
You are a helpful assistant.
<|reasoning_mode|>reasoning_effort:high<|hy_User|>
What is the capital of France?<|hy_Assistant|>
<think>The user is asking for the capital of"""
DONE_REASONING_PROMPT = """<|hy_begin▁of▁sentence|>
You are a helpful assistant.
<|reasoning_mode|>reasoning_effort:high<|hy_User|>
What is the capital of France?<|hy_Assistant|>
<think>The user is asking for the capital of France.</think>
The capital of France is Paris."""
MULTI_TURN_STILL_REASONING_PROMPT = """<|hy_begin▁of▁sentence|>
You are a helpful assistant.
<|reasoning_mode|>reasoning_effort:high<|hy_User|>
What is the capital of France?<|hy_Assistant|
><think></think>The capital of France is Paris.<eos:6124c78e>
<|hy_User|>What about Chile?<|hy_Assistant|>
<think>The user is asking for the capital of"""
MULTI_TURN_DONE_REASONING_PROMPT = """<|hy_begin▁of▁sentence|>
You are a helpful assistant.
<|reasoning_mode|>reasoning_effort:high<|hy_User|>
What is the capital of France?<|hy_Assistant|
><think></think>The capital of France is Paris.<eos:6124c78e>
<|hy_User|>What about Chile?<|hy_Assistant|>
<think>The user is asking for the capital of Chile.</think>
The capital of Chile is Santiago."""
REASONING_END_TEST_CASES = [
pytest.param(STILL_REASONING_PROMPT, False, id="still_reasoning"),
pytest.param(DONE_REASONING_PROMPT, True, id="done_reasoning"),
pytest.param(
MULTI_TURN_STILL_REASONING_PROMPT, False, id="multi_turn_still_reasoning"
),
pytest.param(
MULTI_TURN_DONE_REASONING_PROMPT, True, id="multi_turn_done_reasoning"
),
]
@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_reasoning(
streaming: bool,
param_dict: dict,
hy_v3_tokenizer,
):
output = hy_v3_tokenizer.tokenize(param_dict["output"])
output_tokens: list[str] = [
hy_v3_tokenizer.convert_tokens_to_string([token]) for token in output
]
parser_kwargs = {}
if "reasoning_effort" in param_dict:
parser_kwargs["chat_template_kwargs"] = {
"reasoning_effort": param_dict["reasoning_effort"]
}
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
hy_v3_tokenizer,
**parser_kwargs,
)
reasoning, content = run_reasoning_extraction(
parser, output_tokens, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
output_ids = hy_v3_tokenizer.convert_tokens_to_ids(output)
is_reasoning_end = parser.is_reasoning_end(output_ids)
assert is_reasoning_end == param_dict["is_reasoning_end"]
@pytest.mark.parametrize("prompt, is_reasoning_end", REASONING_END_TEST_CASES)
def test_is_reasoning_end_full_prompt(
prompt: str, is_reasoning_end: bool, hy_v3_tokenizer
):
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
hy_v3_tokenizer,
chat_template_kwargs={"reasoning_effort": "high"},
)
tokens = hy_v3_tokenizer.tokenize(prompt)
token_ids = hy_v3_tokenizer.convert_tokens_to_ids(tokens)
check_is_reasoning_end = parser.is_reasoning_end(token_ids)
assert check_is_reasoning_end == is_reasoning_end
def test_constructor_does_not_mutate_shared_chat_template_kwargs(hy_v3_tokenizer):
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
chat_template_kwargs = {"reasoning_effort": "low"}
first_parser: ReasoningParser = parser_cls(
hy_v3_tokenizer,
chat_template_kwargs=chat_template_kwargs,
)
second_parser: ReasoningParser = parser_cls(
hy_v3_tokenizer,
chat_template_kwargs=chat_template_kwargs,
)
assert chat_template_kwargs == {"reasoning_effort": "low"}
assert isinstance(first_parser, HYV3ReasoningParser)
assert isinstance(second_parser, HYV3ReasoningParser)
assert first_parser._identity_parser is None
assert second_parser._identity_parser is None
def test_constructor_falls_back_to_outer_reasoning_effort(hy_v3_tokenizer):
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
hy_v3_tokenizer,
reasoning_effort="low",
)
assert isinstance(parser, HYV3ReasoningParser)
assert parser._identity_parser is None
@@ -1,220 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import string
from collections.abc import Sequence
from typing import cast
import pytest
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
from vllm.parser.parser_manager import ParserManager
from vllm.reasoning import ReasoningParserManager
from vllm.reasoning.k2_horizon_reasoning_parser import K2HorizonReasoningParser
from vllm.tokenizers import TokenizerLike
pytestmark = pytest.mark.skip_global_cleanup
EFFORT_TOKENS = {
"high": ("<ifm|think>", "</ifm|think>"),
"medium": ("<ifm|think_fast>", "</ifm|think_fast>"),
"low": ("<ifm|think_faster>", "</ifm|think_faster>"),
}
TOOL_CALLS_START = "<ifm|tool_calls>"
class K2Tokenizer:
def __init__(self) -> None:
special_tokens = [
token for token_pair in EFFORT_TOKENS.values() for token in token_pair
] + [TOOL_CALLS_START]
tokens = special_tokens + list(string.printable)
self._vocab = {token: index for index, token in enumerate(tokens, start=1)}
def get_vocab(self) -> dict[str, int]:
return dict(self._vocab)
def encode(self, text: str) -> list[int]:
tokens: list[int] = []
position = 0
specials = sorted(EFFORT_TOKENS["high"] + (TOOL_CALLS_START,), key=len)
while position < len(text):
for special in specials:
if text.startswith(special, position):
tokens.append(self._vocab[special])
position += len(special)
break
else:
tokens.append(self._vocab[text[position]])
position += 1
return tokens
def decode(
self,
ids: Sequence[int] | int,
skip_special_tokens: bool = False,
) -> str:
del skip_special_tokens
if isinstance(ids, int):
ids = [ids]
inverse_vocab = {token_id: token for token, token_id in self._vocab.items()}
return "".join(inverse_vocab[token_id] for token_id in ids)
def _k2_tokenizer() -> TokenizerLike:
return cast(TokenizerLike, K2Tokenizer())
def _request(**kwargs) -> ChatCompletionRequest:
return ChatCompletionRequest(
model="k2-horizon",
messages=[{"role": "user", "content": "test"}],
**kwargs,
)
@pytest.mark.parametrize("effort", ["high", "medium", "low"])
def test_reasoning_effort_selects_tokens(effort: str):
parser = K2HorizonReasoningParser(
_k2_tokenizer(),
chat_template_kwargs={"reasoning_effort": effort},
)
assert (parser.start_token, parser.end_token) == EFFORT_TOKENS[effort]
def test_reasoning_effort_precedence_and_default():
tokenizer = _k2_tokenizer()
request = _request(
reasoning_effort="medium",
chat_template_kwargs={"reasoning_effort": "low"},
)
resolved_kwargs = (
request.build_chat_params(None, "auto")
.with_defaults({"reasoning_effort": "high"})
.chat_template_kwargs
)
parser = K2HorizonReasoningParser(
tokenizer,
chat_template_kwargs=resolved_kwargs,
)
default_parser = K2HorizonReasoningParser(tokenizer)
assert parser.start_token == EFFORT_TOKENS["medium"][0]
assert default_parser.start_token == EFFORT_TOKENS["high"][0]
@pytest.mark.parametrize("effort", ["none", "unknown", None])
def test_invalid_reasoning_effort_raises(effort: str | None):
with pytest.raises(ValueError, match="Unsupported reasoning_effort"):
K2HorizonReasoningParser(
_k2_tokenizer(),
chat_template_kwargs={"reasoning_effort": effort},
)
def test_non_streaming_explicit_and_implicit_boundaries():
parser = K2HorizonReasoningParser(_k2_tokenizer())
request = _request()
assert parser.extract_reasoning("<ifm|think>plan</ifm|think>answer", request) == (
"plan",
"answer",
)
assert parser.extract_reasoning(
f"plan{TOOL_CALLS_START}<ifm|tool_call>ping</ifm|tool_call>", request
) == (
"plan",
f"{TOOL_CALLS_START}<ifm|tool_call>ping</ifm|tool_call>",
)
@pytest.mark.parametrize("reasoning", ["plan", " ", "\n", " \n"])
def test_character_split_reasoning_stream(reasoning: str):
parser = K2HorizonReasoningParser(_k2_tokenizer())
reasoning_parts: list[str] = []
content_parts: list[str] = []
for char in f"{reasoning}</ifm|think>answer":
delta = parser.extract_reasoning_streaming(
previous_text="",
current_text="",
delta_text=char,
previous_token_ids=[],
current_token_ids=[],
delta_token_ids=[],
)
if delta is not None:
if delta.reasoning is not None:
reasoning_parts.append(delta.reasoning)
if delta.content is not None:
content_parts.append(delta.content)
assert "".join(reasoning_parts) == reasoning
assert "".join(content_parts) == "answer"
def test_latest_boundary_controls_reasoning_state():
tokenizer = _k2_tokenizer()
parser = K2HorizonReasoningParser(tokenizer)
vocab = tokenizer.get_vocab()
assert parser.is_reasoning_end([vocab["<ifm|think>"], vocab[TOOL_CALLS_START]])
assert not parser.is_reasoning_end([vocab[TOOL_CALLS_START], vocab["<ifm|think>"]])
def test_composed_streaming_reasoning_to_tool_handoff():
tokenizer = _k2_tokenizer()
tools = [
{
"type": "function",
"function": {
"name": "ping",
"parameters": {"type": "object", "properties": {}},
},
}
]
request = _request(tools=tools, tool_choice="auto")
parser_cls = ParserManager.get_parser(
tool_parser_name="k2_horizon",
reasoning_parser_name="k2_horizon",
enable_auto_tools=True,
)
assert parser_cls is not None
parser = parser_cls(
tokenizer,
request.tools,
chat_template_kwargs={"reasoning_effort": "high"},
)
output = "plan<ifm|tool_calls><ifm|tool_call>ping</ifm|tool_call></ifm|tool_calls>"
reasoning_parts: list[str] = []
tool_names: list[str] = []
for index, char in enumerate(output):
delta = parser.parse_delta(
delta_text=char,
delta_token_ids=tokenizer.encode(char),
request=request,
prompt_token_ids=[] if index == 0 else None,
finished=index == len(output) - 1,
)
if delta is not None:
if delta.reasoning is not None:
reasoning_parts.append(delta.reasoning)
tool_names.extend(
tool_call.function.name
for tool_call in delta.tool_calls
if tool_call.function is not None
and tool_call.function.name is not None
)
assert "".join(reasoning_parts) == "plan"
assert tool_names == ["ping"]
def test_reasoning_parser_registered():
assert (
ReasoningParserManager.get_reasoning_parser("k2_horizon")
is K2HorizonReasoningParser
)
@@ -1,150 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from vllm.entrypoints.generate.base.protocol import DeltaMessage
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
from vllm.parser.kimi_k2 import KimiK2Parser
from vllm.reasoning.kimi_k2_reasoning_parser import KimiK2ReasoningParser
from vllm.tokenizers import get_tokenizer
REASONING_MODEL_NAME = "moonshotai/Kimi-K2.5"
@pytest.fixture(scope="module")
def kimi_k2_tokenizer():
return get_tokenizer(tokenizer_name=REASONING_MODEL_NAME, trust_remote_code=True)
def test_extract_reasoning_with_think_tags(kimi_k2_tokenizer):
parser = KimiK2ReasoningParser(kimi_k2_tokenizer)
request = ChatCompletionRequest(model="test-model", messages=[], temperature=1.0)
reasoning, content = parser.extract_reasoning(
"<think>step by step reasoning</think>final answer", request
)
assert reasoning == "step by step reasoning"
assert content == "final answer"
def test_extract_reasoning_empty_thinking(kimi_k2_tokenizer):
parser = KimiK2ReasoningParser(kimi_k2_tokenizer)
request = ChatCompletionRequest(model="test-model", messages=[], temperature=1.0)
reasoning, content = parser.extract_reasoning(
"<think></think>final answer", request
)
assert reasoning is None
assert content == "final answer"
def test_extract_reasoning_implicit_start(kimi_k2_tokenizer):
"""When there's no <think> tag, everything is treated as reasoning."""
parser = KimiK2ReasoningParser(kimi_k2_tokenizer)
request = ChatCompletionRequest(model="test-model", messages=[], temperature=1.0)
reasoning, content = parser.extract_reasoning(
"implicit reasoning with no tags", request
)
assert reasoning == "implicit reasoning with no tags"
assert content is None
def test_extract_reasoning_tool_section_ends_reasoning(kimi_k2_tokenizer):
"""<|tool_calls_section_begin|> implicitly ends reasoning."""
parser = KimiK2ReasoningParser(kimi_k2_tokenizer)
request = ChatCompletionRequest(model="test-model", messages=[], temperature=1.0)
text = "some reasoning<|tool_calls_section_begin|>tool call data"
reasoning, content = parser.extract_reasoning(text, request)
assert reasoning == "some reasoning"
assert content == "<|tool_calls_section_begin|>tool call data"
def test_streaming_reasoning_then_content(kimi_k2_tokenizer):
"""Token-by-token streaming: reasoning tokens then content after </think>."""
parser = KimiK2ReasoningParser(kimi_k2_tokenizer)
parser_engine = parser._parser_engine
assert isinstance(parser_engine, KimiK2Parser)
think_id = parser_engine._start_token_id
end_think_id = parser_engine._end_token_id
assert think_id is not None
assert end_think_id is not None
# Use a real token ID from the tokenizer for regular content
regular_id = kimi_k2_tokenizer.encode("hello", add_special_tokens=False)[0]
# First token: <think> — single special token should be skipped
result = parser.extract_reasoning_streaming(
previous_text="",
current_text="<think>",
delta_text="<think>",
previous_token_ids=[],
current_token_ids=[think_id],
delta_token_ids=[think_id],
)
assert result is None
# Reasoning token
result = parser.extract_reasoning_streaming(
previous_text="<think>",
current_text="<think>step one",
delta_text="step one",
previous_token_ids=[think_id],
current_token_ids=[think_id, regular_id],
delta_token_ids=[regular_id],
)
assert isinstance(result, DeltaMessage)
assert result.reasoning == "step one"
assert result.content is None
# End token </think> as single token — should be skipped
result = parser.extract_reasoning_streaming(
previous_text="<think>step one",
current_text="<think>step one</think>",
delta_text="</think>",
previous_token_ids=[think_id, regular_id],
current_token_ids=[think_id, regular_id, end_think_id],
delta_token_ids=[end_think_id],
)
assert result is None
# Content after </think>
content_id = kimi_k2_tokenizer.encode("world", add_special_tokens=False)[0]
result = parser.extract_reasoning_streaming(
previous_text="<think>step one</think>",
current_text="<think>step one</think>answer",
delta_text="answer",
previous_token_ids=[think_id, regular_id, end_think_id],
current_token_ids=[think_id, regular_id, end_think_id, content_id],
delta_token_ids=[content_id],
)
assert isinstance(result, DeltaMessage)
assert result.content == "answer"
def test_streaming_tool_section_ends_reasoning(kimi_k2_tokenizer):
"""<|tool_calls_section_begin|> in delta ends reasoning during streaming."""
parser = KimiK2ReasoningParser(kimi_k2_tokenizer)
parser_engine = parser._parser_engine
assert isinstance(parser_engine, KimiK2Parser)
think_id = parser_engine._start_token_id
tool_begin_id = parser_engine._tool_section_start_token_id
assert think_id is not None
assert tool_begin_id is not None
regular_id = kimi_k2_tokenizer.encode("hello", add_special_tokens=False)[0]
# Tool section token arrives — should transition from reasoning to content
result = parser.extract_reasoning_streaming(
previous_text="<think>thinking",
current_text="<think>thinking<|tool_calls_section_begin|>",
delta_text="<|tool_calls_section_begin|>",
previous_token_ids=[think_id, regular_id],
current_token_ids=[think_id, regular_id, tool_begin_id],
delta_token_ids=[tool_begin_id],
)
assert isinstance(result, DeltaMessage)
assert result.content == "<|tool_calls_section_begin|>"
@@ -1,403 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from typing import cast
import pytest
from vllm.entrypoints.generate.base.protocol import DeltaMessage
from vllm.entrypoints.openai.chat_completion.protocol import (
ChatCompletionRequest,
)
from vllm.parser.kimi_k3 import KimiK3Parser
from vllm.parser.parser_manager import ParserManager
from vllm.reasoning.kimi_k3_reasoning_parser import KimiK3ReasoningParser
from vllm.tokenizers import TokenizerLike
pytestmark = pytest.mark.skip_global_cleanup
OPEN = "<|open|>"
CLOSE = "<|close|>"
SEP = "<|sep|>"
THINK_OPEN = f"{OPEN}think{SEP}"
THINK_CLOSE = f"{CLOSE}think{SEP}"
RESPONSE_OPEN = f"{OPEN}response{SEP}"
OPEN_IDS = [1, 2, 3]
CLOSE_IDS = [4, 2, 3]
RESPONSE_OPEN_IDS = [ord(ch) for ch in RESPONSE_OPEN]
class DummyTokenizer:
def __len__(self) -> int:
return 1
def get_vocab(self) -> dict[str, int]:
return {}
def encode(self, text: str, *args, **kwargs) -> list[int]:
if text == THINK_OPEN:
return [1, 2, 3]
if text == THINK_CLOSE:
return [4, 2, 3]
return [ord(ch) for ch in text]
def _dummy_tokenizer() -> TokenizerLike:
return cast(TokenizerLike, DummyTokenizer())
class ReasoningOnlyParser(KimiK3Parser):
reasoning_parser_cls = KimiK3ReasoningParser
def test_parser_manager_selects_kimi_k3_parser_for_reasoning_only():
parser_cls = ParserManager.get_parser(reasoning_parser_name="kimi_k3")
assert parser_cls is not None
assert issubclass(parser_cls, KimiK3Parser)
assert parser_cls.reasoning_parser_cls is KimiK3ReasoningParser
assert parser_cls.tool_parser_cls is None
def test_parser_selection_thinking_disabled():
parser = KimiK3ReasoningParser(
DummyTokenizer(), chat_template_kwargs={"thinking": False}
)
assert parser._thinking_enabled is False
def test_extract_reasoning_with_xtml_tags():
parser = KimiK3ReasoningParser(DummyTokenizer())
request = ChatCompletionRequest(model="test-model", messages=[])
reasoning, content = parser.extract_reasoning_content(
f"{THINK_OPEN}step{THINK_CLOSE}{RESPONSE_OPEN}answer",
request,
)
assert reasoning == "step"
assert content == "answer"
def test_extract_reasoning_with_generation_prefix_consumed():
parser = KimiK3ReasoningParser(DummyTokenizer())
request = ChatCompletionRequest(model="test-model", messages=[])
reasoning, content = parser.extract_reasoning_content(
f"step{THINK_CLOSE}{RESPONSE_OPEN}answer",
request,
)
assert reasoning == "step"
assert content == "answer"
def test_delegating_parser_strips_response_wrapper_without_tool_parser():
parser = ReasoningOnlyParser(_dummy_tokenizer())
request = ChatCompletionRequest(model="test-model", messages=[])
reasoning, content, tool_calls = parser.parse(
f"{THINK_OPEN}step{THINK_CLOSE}{RESPONSE_OPEN}answer",
request,
)
assert reasoning == "step"
assert content == "answer"
assert tool_calls == []
def test_is_reasoning_end_uses_full_input_ids():
parser = KimiK3ReasoningParser(DummyTokenizer())
assert not parser.is_reasoning_end([4, 2])
assert parser.is_reasoning_end([4, 2, 3])
def test_is_reasoning_end_ignores_stale_close_from_prior_turn():
# DummyTokenizer: THINK_OPEN -> [1, 2, 3], THINK_CLOSE -> [4, 2, 3].
# Multi-turn / agent continuation: a prior turn's think channel (its close
# marker) is kept in the prompt, then the current turn opens a new think
# block that has not closed yet. Reasoning must read as NOT ended, otherwise
# the structured-output gate constrains the current turn's reasoning.
parser = KimiK3ReasoningParser(DummyTokenizer())
stale_close = [4, 2, 3]
new_open = [1, 2, 3]
# prior close, then current-turn open still unclosed -> not ended
assert not parser.is_reasoning_end([*stale_close, *new_open])
# ...then the current turn emits its own close -> ended
assert parser.is_reasoning_end([*stale_close, *new_open, *stale_close])
# open with no close yet -> not ended
assert not parser.is_reasoning_end([*new_open])
@pytest.mark.parametrize(
("token_ids", "expected"),
[
pytest.param(
[*OPEN_IDS, 9, 10, *CLOSE_IDS, *RESPONSE_OPEN_IDS, 11],
2,
id="open_and_close_markers",
),
pytest.param(
[9, 10, *CLOSE_IDS, *RESPONSE_OPEN_IDS, 11],
2,
id="open_marker_consumed_as_generation_prefix",
),
pytest.param([*RESPONSE_OPEN_IDS, 11, 12], 0, id="response_only"),
pytest.param([*OPEN_IDS, 9, 10], 2, id="unterminated_after_open"),
pytest.param([9, 10, 11], 3, id="unterminated_without_markers"),
pytest.param([], 0, id="empty"),
pytest.param([9, 4, 2, 10], 4, id="partial_close_marker_is_reasoning"),
pytest.param(
[*OPEN_IDS, 9, *RESPONSE_OPEN_IDS, 11],
2 + len(RESPONSE_OPEN_IDS),
id="response_open_inside_open_think_is_reasoning",
),
],
)
def test_count_reasoning_tokens_matches_think_channel(token_ids, expected):
"""reasoning_tokens must cover exactly what extract_reasoning labels as
reasoning: marker tokens are excluded and a consumed generation prefix
means the output starts inside the think channel."""
parser = KimiK3ReasoningParser(DummyTokenizer())
assert parser.count_reasoning_tokens(token_ids) == expected
def test_count_reasoning_tokens_is_zero_when_thinking_disabled():
parser = KimiK3ReasoningParser(
DummyTokenizer(), chat_template_kwargs={"thinking": False}
)
assert parser.count_reasoning_tokens([*OPEN_IDS, 9, 10, *CLOSE_IDS]) == 0
def test_count_reasoning_tokens_through_delegating_parser():
parser = ReasoningOnlyParser(_dummy_tokenizer())
assert parser.count_reasoning_tokens([*OPEN_IDS, 9, *CLOSE_IDS, 11]) == 1
def test_streaming_split_open_marker_is_held_back():
parser = KimiK3ReasoningParser(DummyTokenizer())
first = parser.extract_reasoning_content_streaming(
previous_text="",
current_text=OPEN,
delta_text=OPEN,
previous_token_ids=[],
current_token_ids=[1],
delta_token_ids=[1],
)
second = parser.extract_reasoning_content_streaming(
previous_text=OPEN,
current_text=f"{OPEN}think",
delta_text="think",
previous_token_ids=[1],
current_token_ids=[1, 2],
delta_token_ids=[2],
)
third = parser.extract_reasoning_content_streaming(
previous_text=f"{OPEN}think",
current_text=THINK_OPEN + "step",
delta_text=f"{SEP}step",
previous_token_ids=[1, 2],
current_token_ids=[1, 2, 3, 9],
delta_token_ids=[3, 9],
)
assert first is None
assert second is None
assert isinstance(third, DeltaMessage)
assert third.reasoning == "step"
def test_streaming_split_close_marker_hands_content_downstream():
parser = KimiK3ReasoningParser(DummyTokenizer())
previous_text = f"{THINK_OPEN}step"
partial_close = parser.extract_reasoning_content_streaming(
previous_text=previous_text,
current_text=previous_text + CLOSE,
delta_text=CLOSE,
previous_token_ids=[1, 2, 3, 9],
current_token_ids=[1, 2, 3, 9, 4],
delta_token_ids=[4],
)
closed = parser.extract_reasoning_content_streaming(
previous_text=previous_text + CLOSE,
current_text=previous_text + f"{THINK_CLOSE}{RESPONSE_OPEN}answer",
delta_text=f"think{SEP}{RESPONSE_OPEN}answer",
previous_token_ids=[1, 2, 3, 9, 4],
current_token_ids=[1, 2, 3, 9, 4, 2, 3, 10],
delta_token_ids=[2, 3, 10],
)
assert partial_close is None
assert isinstance(closed, DeltaMessage)
assert closed.reasoning is None
assert closed.content == f"{RESPONSE_OPEN}answer"
assert parser.extract_content_ids([2, 3, 10]) == [10]
def test_thinking_disabled_streams_content():
parser = KimiK3ReasoningParser(
DummyTokenizer(), chat_template_kwargs={"enable_thinking": False}
)
delta = parser.extract_reasoning_content_streaming(
previous_text="",
current_text=f"{RESPONSE_OPEN}answer",
delta_text=f"{RESPONSE_OPEN}answer",
previous_token_ids=[],
current_token_ids=[1],
delta_token_ids=[1],
)
assert isinstance(delta, DeltaMessage)
assert delta.content == f"{RESPONSE_OPEN}answer"
assert delta.reasoning is None
def test_delegating_parser_thinking_false_streams_response_content():
parser = ReasoningOnlyParser(
_dummy_tokenizer(),
chat_template_kwargs={"thinking": False},
)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"thinking": False},
)
first = parser.parse_delta(
delta_text="OK",
delta_token_ids=[10],
request=request,
prompt_token_ids=[1],
finished=False,
)
partial_close = parser.parse_delta(
delta_text=CLOSE,
delta_token_ids=[2],
request=request,
prompt_token_ids=[1],
finished=False,
)
closed = parser.parse_delta(
delta_text=f"response{SEP}",
delta_token_ids=[3, 4],
request=request,
prompt_token_ids=[1],
finished=False,
)
assert first is not None
assert first.content == "OK"
assert first.reasoning is None
assert partial_close is None
assert closed is None
def test_adjust_request_keeps_xtml_markers_contiguous():
parser = KimiK3ReasoningParser(DummyTokenizer())
request = ChatCompletionRequest(model="test-model", messages=[])
adjusted = parser.adjust_request(request)
assert adjusted.skip_special_tokens is False
if hasattr(adjusted, "spaces_between_special_tokens"):
assert adjusted.spaces_between_special_tokens is False
def _reference_is_reasoning_end(input_ids: list[int]) -> bool:
"""Full-sequence reference for the streaming check to be measured against.
Two independent last-occurrence scans, i.e. the straightforward reading of
"reasoning ended iff the newest think marker is a close marker".
"""
def last(needle: list[int]) -> int:
for i in range(len(input_ids) - len(needle), -1, -1):
if input_ids[i : i + len(needle)] == needle:
return i
return -1
last_close, last_open = last(CLOSE_IDS), last(OPEN_IDS)
if last_open == -1:
return last_close != -1
return last_close > last_open
def test_is_reasoning_end_streaming_only_scans_the_step_window():
"""The decode-step check must not re-derive the answer from the whole
sequence: an already-closed think block earlier in the sequence is the
engine's business (it latches `reasoning_ended`), not this call's."""
parser = KimiK3ReasoningParser(DummyTokenizer())
prompt = [*CLOSE_IDS, *OPEN_IDS, 9, 9, 9]
assert not parser.is_reasoning_end_streaming([*prompt, 7], [7])
assert parser.is_reasoning_end_streaming([*prompt, *CLOSE_IDS], CLOSE_IDS)
def test_is_reasoning_end_streaming_detects_marker_across_step_boundary():
"""A 3-token marker can be split over decode steps; the check carries the
preceding len(marker)-1 tokens so the final token still completes it."""
parser = KimiK3ReasoningParser(DummyTokenizer())
history = [*OPEN_IDS, 5, *CLOSE_IDS[:2]]
assert not parser.is_reasoning_end_streaming(history, [CLOSE_IDS[1]])
assert parser.is_reasoning_end_streaming([*history, CLOSE_IDS[2]], [CLOSE_IDS[2]])
def test_is_reasoning_end_streaming_reopened_block_is_not_ended():
"""Kimi K3 may close and immediately reopen the think channel inside one
window (speculative decoding lands several tokens per step). The newest
marker wins, matching is_reasoning_end."""
parser = KimiK3ReasoningParser(DummyTokenizer())
window = [*CLOSE_IDS, *OPEN_IDS]
assert not parser.is_reasoning_end_streaming([*OPEN_IDS, 5, *window], window)
def test_is_reasoning_end_streaming_accepts_an_iterator_delta():
"""`should_advance` may hand over an islice rather than a list."""
parser = KimiK3ReasoningParser(DummyTokenizer())
full = [*OPEN_IDS, 5, *CLOSE_IDS]
assert parser.is_reasoning_end_streaming(full, iter(CLOSE_IDS))
def test_is_reasoning_end_streaming_thinking_disabled():
parser = KimiK3ReasoningParser(
DummyTokenizer(), chat_template_kwargs={"thinking": False}
)
assert parser.is_reasoning_end_streaming([1], [1])
@pytest.mark.parametrize("seed", range(24))
def test_reasoning_end_matches_reference_over_marker_dense_sequences(seed):
"""Both the full-sequence check and the per-step check must agree with the
two-scan reference, on sequences where the markers (which share the suffix
[2, 3]) collide and overlap constantly."""
import random
rnd = random.Random(seed)
parser = KimiK3ReasoningParser(DummyTokenizer())
for _ in range(200):
head = [rnd.choice([1, 2, 3, 4]) for _ in range(rnd.randrange(0, 14))]
delta = [rnd.choice([1, 2, 3, 4]) for _ in range(rnd.randrange(1, 5))]
full = [*head, *delta]
assert parser.is_reasoning_end(full) == _reference_is_reasoning_end(full)
# The engine only calls the streaming check while reasoning is still
# open, so that is the only case it has to agree on.
if not _reference_is_reasoning_end(head):
assert parser.is_reasoning_end_streaming(
full, delta
) == _reference_is_reasoning_end(full), (head, delta)
@@ -1,195 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from transformers import AutoTokenizer, PreTrainedTokenizerBase
from tests.reasoning.utils import run_reasoning_extraction
from vllm.reasoning import ReasoningParser, ReasoningParserManager
parser_name = "minimax_m2_append_think"
end_token = "</think>"
# MiniMax M2 model path
REASONING_MODEL_NAME = "MiniMaxAI/MiniMax-M2"
@pytest.fixture(scope="module")
def minimax_m2_tokenizer() -> PreTrainedTokenizerBase:
return AutoTokenizer.from_pretrained(REASONING_MODEL_NAME)
# =============================================================================
# MiniMaxM2AppendThinkReasoningParser behavior:
# - Prepends <think> to the beginning of the output
# - Does NOT separate reasoning and content
# - Returns everything as content (with <think> prepended)
# - reasoning is always None
#
# This parser is used when you want to keep the raw output with <think> added
# =============================================================================
# Case: simple output with end token
SIMPLE_OUTPUT = {
"output": "This is reasoning</think>This is response",
"reasoning": None,
"content": "<think>This is reasoning</think>This is response",
"is_reasoning_end": True,
}
# Case: output without end token (reasoning in progress)
NO_END_TOKEN = {
"output": "This is reasoning in progress",
"reasoning": None,
"content": "<think>This is reasoning in progress",
"is_reasoning_end": False,
}
# Case: only end token
ONLY_END_TOKEN = {
"output": "</think>This is response",
"reasoning": None,
"content": "<think></think>This is response",
"is_reasoning_end": True,
}
# Case: multiple lines
MULTIPLE_LINES = {
"output": "Line 1\nLine 2</think>Response 1\nResponse 2",
"reasoning": None,
"content": "<think>Line 1\nLine 2</think>Response 1\nResponse 2",
"is_reasoning_end": True,
}
# Case: empty output (non-streaming prepends <think>)
EMPTY = {
"output": "",
"reasoning": None,
"content": "<think>",
"is_reasoning_end": False,
}
# Case: empty output streaming (no tokens = no output)
EMPTY_STREAMING = {
"output": "",
"reasoning": None,
"content": None,
"is_reasoning_end": False,
}
# Case: special characters
SPECIAL_CHARS = {
"output": "Let me think... 1+1=2</think>Yes!",
"reasoning": None,
"content": "<think>Let me think... 1+1=2</think>Yes!",
"is_reasoning_end": True,
}
# Case: code in output
CODE_OUTPUT = {
"output": "```python\nprint('hi')\n```</think>Here's the code.",
"reasoning": None,
"content": "<think>```python\nprint('hi')\n```</think>Here's the code.",
"is_reasoning_end": True,
}
TEST_CASES = [
pytest.param(
False,
SIMPLE_OUTPUT,
id="simple_output",
),
pytest.param(
True,
SIMPLE_OUTPUT,
id="simple_output_streaming",
),
pytest.param(
False,
NO_END_TOKEN,
id="no_end_token",
),
pytest.param(
True,
NO_END_TOKEN,
id="no_end_token_streaming",
),
pytest.param(
False,
ONLY_END_TOKEN,
id="only_end_token",
),
pytest.param(
True,
ONLY_END_TOKEN,
id="only_end_token_streaming",
),
pytest.param(
False,
MULTIPLE_LINES,
id="multiple_lines",
),
pytest.param(
True,
MULTIPLE_LINES,
id="multiple_lines_streaming",
),
pytest.param(
False,
EMPTY,
id="empty",
),
pytest.param(
True,
EMPTY_STREAMING,
id="empty_streaming",
),
pytest.param(
False,
SPECIAL_CHARS,
id="special_chars",
),
pytest.param(
True,
SPECIAL_CHARS,
id="special_chars_streaming",
),
pytest.param(
False,
CODE_OUTPUT,
id="code_output",
),
pytest.param(
True,
CODE_OUTPUT,
id="code_output_streaming",
),
]
@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_reasoning(
streaming: bool,
param_dict: dict,
minimax_m2_tokenizer,
):
output = minimax_m2_tokenizer.tokenize(param_dict["output"])
# decode everything to tokens
output_tokens: list[str] = [
minimax_m2_tokenizer.convert_tokens_to_string([token]) for token in output
]
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
minimax_m2_tokenizer
)
reasoning, content = run_reasoning_extraction(
parser, output_tokens, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
# Test is_reasoning_end
output_ids = minimax_m2_tokenizer.convert_tokens_to_ids(output)
is_reasoning_end = parser.is_reasoning_end(output_ids)
assert is_reasoning_end == param_dict["is_reasoning_end"]
@@ -1,204 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from transformers import AutoTokenizer, PreTrainedTokenizerBase
from tests.reasoning.utils import run_reasoning_extraction
from vllm.reasoning import ReasoningParser, ReasoningParserManager
parser_name = "minimax_m2"
end_token = "</think>"
# MiniMax M2 model path
REASONING_MODEL_NAME = "MiniMaxAI/MiniMax-M2"
@pytest.fixture(scope="module")
def minimax_m2_tokenizer() -> PreTrainedTokenizerBase:
return AutoTokenizer.from_pretrained(REASONING_MODEL_NAME)
# =============================================================================
# MiniMax M2 specific behavior:
# - Model does NOT generate <think> start token
# - Model only generates </think> end token
# - All content before </think> is reasoning
# - All content after </think> is the actual response (content)
# =============================================================================
# Case: reasoning + end token + content (typical case)
SIMPLE_REASONING = {
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
}
# Case: reasoning + end token only (no content after)
COMPLETE_REASONING = {
"output": "This is a reasoning section</think>",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": True,
}
# Case: no end token yet (streaming in progress, all is reasoning)
NO_END_TOKEN = {
"output": "This is reasoning in progress",
"reasoning": "This is reasoning in progress",
"content": None,
"is_reasoning_end": False,
}
# Case: multiple lines of reasoning
MULTIPLE_LINES = {
"output": "First line\nSecond line</think>Response first line\nResponse second",
"reasoning": "First line\nSecond line",
"content": "Response first line\nResponse second",
"is_reasoning_end": True,
}
# Case: only end token streaming (reasoning is None because it's just the token)
SHORTEST_REASONING_STREAMING = {
"output": "</think>This is the response",
"reasoning": None,
"content": "This is the response",
"is_reasoning_end": True,
}
# Case: empty streaming
EMPTY_STREAMING = {
"output": "",
"reasoning": None,
"content": None,
"is_reasoning_end": False,
}
# Case: long reasoning with special characters
SPECIAL_CHARS = {
"output": "Let me think... 1+1=2, right?</think>Yes, 1+1=2.",
"reasoning": "Let me think... 1+1=2, right?",
"content": "Yes, 1+1=2.",
"is_reasoning_end": True,
}
# Case: reasoning with code blocks
CODE_IN_REASONING = {
"output": "```python\nprint('hello')\n```</think>Here is the code.",
"reasoning": "```python\nprint('hello')\n```",
"content": "Here is the code.",
"is_reasoning_end": True,
}
TEST_CASES = [
# Core cases: no start token (MiniMax M2 actual behavior)
pytest.param(
False,
SIMPLE_REASONING,
id="simple_reasoning",
),
pytest.param(
True,
SIMPLE_REASONING,
id="simple_reasoning_streaming",
),
pytest.param(
False,
COMPLETE_REASONING,
id="complete_reasoning",
),
pytest.param(
True,
COMPLETE_REASONING,
id="complete_reasoning_streaming",
),
pytest.param(
False,
NO_END_TOKEN,
id="no_end_token",
),
pytest.param(
True,
NO_END_TOKEN,
id="no_end_token_streaming",
),
pytest.param(
False,
MULTIPLE_LINES,
id="multiple_lines",
),
pytest.param(
True,
MULTIPLE_LINES,
id="multiple_lines_streaming",
),
pytest.param(
True,
SHORTEST_REASONING_STREAMING,
id="shortest_reasoning_streaming",
),
pytest.param(
True,
EMPTY_STREAMING,
id="empty_streaming",
),
pytest.param(
False,
SPECIAL_CHARS,
id="special_chars",
),
pytest.param(
True,
SPECIAL_CHARS,
id="special_chars_streaming",
),
pytest.param(
False,
CODE_IN_REASONING,
id="code_in_reasoning",
),
pytest.param(
True,
CODE_IN_REASONING,
id="code_in_reasoning_streaming",
),
]
@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_reasoning(
streaming: bool,
param_dict: dict,
minimax_m2_tokenizer,
):
output = minimax_m2_tokenizer.tokenize(param_dict["output"])
# decode everything to tokens
output_tokens: list[str] = [
minimax_m2_tokenizer.convert_tokens_to_string([token]) for token in output
]
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
minimax_m2_tokenizer
)
reasoning, content = run_reasoning_extraction(
parser, output_tokens, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
# Test is_reasoning_end
output_ids = minimax_m2_tokenizer.convert_tokens_to_ids(output)
is_reasoning_end = parser.is_reasoning_end(output_ids)
assert is_reasoning_end == param_dict["is_reasoning_end"]
# Test extract_content
if param_dict["content"] is not None:
content_ids = parser.extract_content_ids(output_ids)
assert content_ids == minimax_m2_tokenizer.convert_tokens_to_ids(
minimax_m2_tokenizer.tokenize(param_dict["content"])
)
else:
content_ids = parser.extract_content_ids(output)
assert content_ids == []
@@ -1,461 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import string
from collections.abc import Sequence
import pytest
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
from vllm.reasoning import ReasoningParserManager
from vllm.reasoning.minimax_m3_reasoning_parser import MiniMaxM3ReasoningParser
pytestmark = pytest.mark.skip_global_cleanup
class MiniMaxM3Tokenizer:
"""Small tokenizer with MiniMax M3 reasoning tags as special tokens."""
special_tokens = ("<mm:think>", "</mm:think>")
def __init__(self):
self._token_to_id: dict[str, int] = {}
self._id_to_token: dict[int, str] = {}
for token in self.special_tokens:
self._add_token(token)
for char in string.printable:
self._add_token(char)
def _add_token(self, token: str) -> int:
token_id = self._token_to_id.get(token)
if token_id is None:
token_id = len(self._token_to_id) + 1
self._token_to_id[token] = token_id
self._id_to_token[token_id] = token
return token_id
def get_vocab(self) -> dict[str, int]:
return dict(self._token_to_id)
def encode(
self,
text: str,
truncation: bool | None = None,
max_length: int | None = None,
add_special_tokens: bool = True,
) -> list[int]:
return [self._add_token(token) for token in self.tokenize(text)]
def decode(
self, ids: Sequence[int] | int, skip_special_tokens: bool = False
) -> str:
if isinstance(ids, int):
ids = [ids]
return "".join(self._id_to_token[token_id] for token_id in ids)
def tokenize(self, text: str) -> list[str]:
tokens: list[str] = []
pos = 0
while pos < len(text):
for special_token in self.special_tokens:
if text.startswith(special_token, pos):
tokens.append(special_token)
pos += len(special_token)
break
else:
tokens.append(text[pos])
pos += 1
return tokens
def convert_ids_to_tokens(
self,
ids: Sequence[int],
skip_special_tokens: bool = False,
) -> list[str]:
return [self._id_to_token[token_id] for token_id in ids]
def convert_tokens_to_ids(self, tokens: str | list[str]) -> int | list[int]:
if isinstance(tokens, str):
return self._add_token(tokens)
return [self._add_token(token) for token in tokens]
def convert_tokens_to_string(self, tokens: list[str]) -> str:
return "".join(tokens)
class SplitMiniMaxM3Tokenizer(MiniMaxM3Tokenizer):
"""Tokenizer that exposes marker vocab entries but encodes them as text."""
def tokenize(self, text: str) -> list[str]:
return list(text)
class RuntimeSplitMiniMaxM3Tokenizer(MiniMaxM3Tokenizer):
"""Tokenizer whose runtime output splits markers despite atomic encodes."""
def encode_runtime(self, text: str) -> list[int]:
return [self._add_token(token) for token in list(text)]
def make_parser(
chat_template_kwargs: dict[str, str] | None = None,
) -> tuple[MiniMaxM3ReasoningParser, MiniMaxM3Tokenizer]:
tokenizer = MiniMaxM3Tokenizer()
return (
MiniMaxM3ReasoningParser(tokenizer, chat_template_kwargs=chat_template_kwargs),
tokenizer,
)
def run_streaming(
parser: MiniMaxM3ReasoningParser,
tokenizer: MiniMaxM3Tokenizer,
chunks: list[str],
) -> tuple[str | None, str | None, list[bool]]:
previous_text = ""
previous_token_ids: list[int] = []
reasoning_parts: list[str] = []
content_parts: list[str] = []
reasoning_end_states: list[bool] = []
for chunk in chunks:
encode_runtime = getattr(tokenizer, "encode_runtime", tokenizer.encode)
delta_token_ids = encode_runtime(chunk)
current_text = previous_text + chunk
current_token_ids = previous_token_ids + delta_token_ids
delta = parser.extract_reasoning_streaming(
previous_text=previous_text,
current_text=current_text,
delta_text=chunk,
previous_token_ids=previous_token_ids,
current_token_ids=current_token_ids,
delta_token_ids=delta_token_ids,
)
reasoning_end_states.append(
parser.is_reasoning_end_streaming(current_token_ids, delta_token_ids)
)
if delta is not None:
if delta.reasoning is not None:
reasoning_parts.append(delta.reasoning)
if delta.content is not None:
content_parts.append(delta.content)
previous_text = current_text
previous_token_ids = current_token_ids
return (
"".join(reasoning_parts) or None,
"".join(content_parts) or None,
reasoning_end_states,
)
def test_parser_registration():
parser_cls = ReasoningParserManager.get_reasoning_parser("minimax_m3")
assert parser_cls is MiniMaxM3ReasoningParser
def test_nonstreaming_extracts_explicit_reasoning_block():
parser, _ = make_parser()
request = ChatCompletionRequest(messages=[], model="test-model")
reasoning, content = parser.extract_reasoning(
"<mm:think>plan</mm:think>answer", request
)
assert reasoning == "plan"
assert content == "answer"
def test_nonstreaming_without_start_tag_is_content():
parser, _ = make_parser()
request = ChatCompletionRequest(messages=[], model="test-model")
reasoning, content = parser.extract_reasoning("plain answer", request)
assert reasoning is None
assert content == "plain answer"
def test_nonstreaming_drops_leading_end_tag():
parser, _ = make_parser()
request = ChatCompletionRequest(messages=[], model="test-model")
reasoning, content = parser.extract_reasoning("</mm:think>answer", request)
assert reasoning is None
assert content == "answer"
def test_nonstreaming_non_leading_end_tag_is_content():
parser, _ = make_parser()
request = ChatCompletionRequest(messages=[], model="test-model")
reasoning, content = parser.extract_reasoning("XXX</mm:think>YYY", request)
assert reasoning is None
assert content == "XXX</mm:think>YYY"
def test_nonstreaming_enabled_mode_starts_in_reasoning():
parser, _ = make_parser(chat_template_kwargs={"thinking_mode": "enabled"})
request = ChatCompletionRequest(messages=[], model="test-model")
reasoning, content = parser.extract_reasoning("plan</mm:think>answer", request)
assert reasoning == "plan"
assert content == "answer"
def test_nonstreaming_open_reasoning_block():
parser, _ = make_parser()
request = ChatCompletionRequest(messages=[], model="test-model")
reasoning, content = parser.extract_reasoning("<mm:think>still thinking", request)
assert reasoning == "still thinking"
assert content is None
def test_streaming_reasoning_tags_are_not_returned():
parser, tokenizer = make_parser()
reasoning, content, end_states = run_streaming(
parser,
tokenizer,
["<mm:think>", "plan", "</mm:think>", "answer"],
)
assert reasoning == "plan"
assert content == "answer"
assert end_states == [False, False, True, True]
def test_streaming_boundary_can_emit_reasoning_and_content():
parser, tokenizer = make_parser()
reasoning, content, end_states = run_streaming(
parser,
tokenizer,
["<mm:think>plan</mm:think>answer"],
)
assert reasoning == "plan"
assert content == "answer"
assert end_states == [True]
def test_streaming_drops_leading_end_tag():
parser, tokenizer = make_parser()
reasoning, content, end_states = run_streaming(
parser,
tokenizer,
["</mm:think>", "answer"],
)
assert reasoning is None
assert content == "answer"
assert end_states == [True, True]
def test_streaming_non_leading_end_tag_is_content():
parser, tokenizer = make_parser()
reasoning, content, end_states = run_streaming(
parser,
tokenizer,
["XXX</mm:think>YYY"],
)
assert reasoning is None
assert content == "XXX</mm:think>YYY"
assert end_states == [True]
def test_streaming_enabled_mode_starts_in_reasoning():
parser, tokenizer = make_parser(chat_template_kwargs={"thinking_mode": "enabled"})
reasoning, content, end_states = run_streaming(
parser,
tokenizer,
["plan", "</mm:think>", "answer"],
)
assert reasoning == "plan"
assert content == "answer"
assert end_states == [False, True, True]
def test_streaming_plain_content_ends_reasoning_phase():
parser, tokenizer = make_parser()
reasoning, content, end_states = run_streaming(
parser,
tokenizer,
["plain ", "answer"],
)
assert reasoning is None
assert content == "plain answer"
assert end_states == [True, True]
def test_streaming_split_marker_tokens_are_not_returned():
tokenizer = RuntimeSplitMiniMaxM3Tokenizer()
parser = MiniMaxM3ReasoningParser(tokenizer)
reasoning, content, end_states = run_streaming(
parser,
tokenizer,
["<mm:think>", "Reasoning", " content", "</mm:think>", "content"],
)
assert reasoning == "Reasoning content"
assert content == "content"
assert end_states == [False, False, False, True, True]
def test_streaming_split_marker_text_drives_end_state():
tokenizer = RuntimeSplitMiniMaxM3Tokenizer()
parser = MiniMaxM3ReasoningParser(tokenizer)
previous_text = ""
previous_token_ids: list[int] = []
for chunk in ["<mm:think>", "Reasoning", " content", "</mm:think>"]:
delta_token_ids = tokenizer.encode_runtime(chunk)
current_text = previous_text + chunk
current_token_ids = previous_token_ids + delta_token_ids
parser.extract_reasoning_streaming(
previous_text=previous_text,
current_text=current_text,
delta_text=chunk,
previous_token_ids=previous_token_ids,
current_token_ids=current_token_ids,
delta_token_ids=delta_token_ids,
)
previous_text = current_text
previous_token_ids = current_token_ids
assert parser.is_reasoning_end_streaming(previous_token_ids, []) is True
def test_streaming_split_end_marker_content_ids_are_stripped():
tokenizer = RuntimeSplitMiniMaxM3Tokenizer()
parser = MiniMaxM3ReasoningParser(tokenizer)
previous_text = "<mm:think>Reasoning"
previous_token_ids = tokenizer.encode_runtime(previous_text)
delta_text = "</mm:think>content"
delta_token_ids = tokenizer.encode_runtime(delta_text)
current_token_ids = previous_token_ids + delta_token_ids
parser.extract_reasoning_streaming(
previous_text=previous_text,
current_text=previous_text + delta_text,
delta_text=delta_text,
previous_token_ids=previous_token_ids,
current_token_ids=current_token_ids,
delta_token_ids=delta_token_ids,
)
assert parser.is_reasoning_end_streaming(current_token_ids, delta_token_ids)
assert tokenizer.decode(parser.extract_content_ids(delta_token_ids)) == "content"
def test_streaming_split_marker_tokens_enabled_mode():
tokenizer = RuntimeSplitMiniMaxM3Tokenizer()
parser = MiniMaxM3ReasoningParser(
tokenizer, chat_template_kwargs={"thinking_mode": "enabled"}
)
reasoning, content, end_states = run_streaming(
parser,
tokenizer,
["Reasoning", " content", "</mm:think>", "content"],
)
assert reasoning == "Reasoning content"
assert content == "content"
assert end_states == [False, False, True, True]
def test_streaming_split_marker_text_across_deltas():
tokenizer = RuntimeSplitMiniMaxM3Tokenizer()
parser = MiniMaxM3ReasoningParser(tokenizer)
reasoning, content, end_states = run_streaming(
parser,
tokenizer,
["<mm:", "think>", "Reasoning", " content", "</mm:", "think>", "content"],
)
assert reasoning == "Reasoning content"
assert content == "content"
assert end_states == [False, False, False, False, False, True, True]
def test_streaming_split_leading_end_marker_text_across_deltas():
tokenizer = RuntimeSplitMiniMaxM3Tokenizer()
parser = MiniMaxM3ReasoningParser(tokenizer)
reasoning, content, end_states = run_streaming(
parser,
tokenizer,
["</mm:", "think>", "content"],
)
assert reasoning is None
assert content == "content"
assert end_states == [False, True, True]
def test_token_id_helpers_with_split_marker_tokens():
tokenizer = SplitMiniMaxM3Tokenizer()
parser = MiniMaxM3ReasoningParser(tokenizer)
output_ids = tokenizer.encode(
"<mm:think>abc</mm:think>def", add_special_tokens=False
)
open_reasoning_ids = tokenizer.encode("<mm:think>abc", add_special_tokens=False)
content_ids = tokenizer.encode("plain", add_special_tokens=False)
assert parser.is_reasoning_end(output_ids)
assert not parser.is_reasoning_end(open_reasoning_ids)
assert not parser.is_reasoning_end(content_ids)
assert tokenizer.decode(parser.extract_content_ids(output_ids)) == "def"
assert parser.extract_content_ids(open_reasoning_ids) == []
assert parser.extract_content_ids(content_ids) == content_ids
assert parser.count_reasoning_tokens(output_ids) == len(tokenizer.encode("abc"))
def test_token_id_helpers():
parser, tokenizer = make_parser()
output_ids = tokenizer.encode(
"<mm:think>abc</mm:think>def", add_special_tokens=False
)
open_reasoning_ids = tokenizer.encode("<mm:think>abc", add_special_tokens=False)
content_ids = tokenizer.encode("plain", add_special_tokens=False)
assert parser.is_reasoning_end(output_ids)
assert not parser.is_reasoning_end(open_reasoning_ids)
assert not parser.is_reasoning_end(content_ids)
assert tokenizer.decode(parser.extract_content_ids(output_ids)) == "def"
assert parser.extract_content_ids(open_reasoning_ids) == []
assert parser.extract_content_ids(content_ids) == content_ids
assert parser.count_reasoning_tokens(output_ids) == len(tokenizer.encode("abc"))
def test_token_id_helpers_enabled_mode():
parser, tokenizer = make_parser(chat_template_kwargs={"thinking_mode": "enabled"})
output_ids = tokenizer.encode("abc</mm:think>def", add_special_tokens=False)
open_reasoning_ids = tokenizer.encode("abc", add_special_tokens=False)
assert parser.is_reasoning_end(output_ids)
assert not parser.is_reasoning_end(open_reasoning_ids)
assert tokenizer.decode(parser.extract_content_ids(output_ids)) == "def"
assert parser.extract_content_ids(open_reasoning_ids) == []
assert parser.count_reasoning_tokens(output_ids) == len(tokenizer.encode("abc"))
assert parser.count_reasoning_tokens(open_reasoning_ids) == len(
tokenizer.encode("abc")
)
@@ -1,293 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from typing import TypedDict, cast
import pytest
import regex as re
from transformers import PreTrainedTokenizerBase
from tests.reasoning.utils import run_reasoning_extraction
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
from vllm.parser.abstract_parser import DelegatingParser
from vllm.parser.engine.registered_adapters import NemotronV3ParserReasoningAdapter
from vllm.reasoning import ReasoningParser, ReasoningParserManager
parser_name = "nemotron_v3"
class ReasoningCase(TypedDict):
output: str
reasoning: str | None
content: str | None
class FakeNemotronTokenizer:
def __init__(self):
self._vocab = {
"<think>": 1,
"</think>": 2,
}
self._inv_vocab = {v: k for k, v in self._vocab.items()}
self._pattern = re.compile(r"(<think>|</think>)")
def get_vocab(self) -> dict[str, int]:
return self._vocab
def tokenize(self, text: str) -> list[str]:
tokens: list[str] = []
for part in self._pattern.split(text):
if part:
tokens.append(part)
return tokens
def convert_tokens_to_string(self, tokens: list[str]) -> str:
return "".join(tokens)
def decode(self, token_ids: list[int]) -> str:
return "".join(self._inv_vocab.get(tid, f"<unk:{tid}>") for tid in token_ids)
@pytest.fixture
def tokenizer() -> PreTrainedTokenizerBase:
return cast(PreTrainedTokenizerBase, FakeNemotronTokenizer())
@pytest.mark.parametrize(
"streaming,param_dict",
[
pytest.param(
False,
{
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
},
id="without_start_token",
),
pytest.param(
True,
{
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
},
id="without_start_token_streaming",
),
pytest.param(
False,
{
"output": "<think>This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
},
id="with_start_token",
),
pytest.param(
True,
{
"output": "<think>This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
},
id="with_start_token_streaming",
),
],
)
def test_nemotron_v3_reasoning(
tokenizer: PreTrainedTokenizerBase,
streaming: bool,
param_dict: ReasoningCase,
):
output = tokenizer.tokenize(param_dict["output"])
model_output = [tokenizer.convert_tokens_to_string([token]) for token in output]
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
tokenizer
)
reasoning, content = run_reasoning_extraction(
parser, model_output, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
def test_nemotron_v3_without_thinking_moves_into_content(
tokenizer: PreTrainedTokenizerBase,
):
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"enable_thinking": False},
)
reasoning, content = run_reasoning_extraction(
parser,
["This is plain content"],
request=request,
streaming=False,
)
# No real content followed the reasoning, so the trace is moved into
# content (reasoning left empty) — matching main's behavior.
assert reasoning is None
assert content == "This is plain content"
def test_nemotron_v3_force_nonempty_content_moves_into_content(
tokenizer: PreTrainedTokenizerBase,
):
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"force_nonempty_content": True},
)
reasoning, content = run_reasoning_extraction(
parser,
["<think>This is plain content"],
request=request,
streaming=False,
)
assert reasoning is None
assert content == "This is plain content"
def test_nemotron_v3_force_nonempty_keeps_real_content(
tokenizer: PreTrainedTokenizerBase,
):
# When real content follows the closing tag nothing is promoted: the
# content after </think> is returned as-is and reasoning stays separate.
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"force_nonempty_content": True},
)
reasoning, content = run_reasoning_extraction(
parser,
["<think>reasoning here</think>real answer"],
request=request,
streaming=False,
)
assert reasoning == "reasoning here"
assert content == "real answer"
def test_nemotron_v3_with_thinking_keeps_truncated_reasoning(
tokenizer: PreTrainedTokenizerBase,
):
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser = parser_cls(tokenizer)
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"enable_thinking": True},
)
reasoning, content = run_reasoning_extraction(
parser,
["This is truncated reasoning"],
request=request,
streaming=False,
)
assert reasoning == "This is truncated reasoning"
assert content is None
_SPECIAL_TOKEN_IDS = {"<think>": 1, "</think>": 2}
def _token_id(token: str) -> int:
# Only the think markers need stable ids; everything else is non-special.
return _SPECIAL_TOKEN_IDS.get(token, 0)
def _make_reasoning_parser(tokenizer):
class _NemotronParser(DelegatingParser):
reasoning_parser_cls = NemotronV3ParserReasoningAdapter
tool_parser_cls = None
return _NemotronParser(tokenizer)
def _run_parse_delta(parser, tokenizer, text, request):
tokens = tokenizer.tokenize(text)
reasoning_parts: list[str] = []
content_parts: list[str] = []
for i, token in enumerate(tokens):
delta = parser.parse_delta(
delta_text=token,
delta_token_ids=[_token_id(token)],
request=request,
prompt_token_ids=[] if i == 0 else None,
finished=(i == len(tokens) - 1),
)
if delta is None:
continue
if delta.reasoning:
reasoning_parts.append(delta.reasoning)
if delta.content:
content_parts.append(delta.content)
return "".join(reasoning_parts), "".join(content_parts)
def test_nemotron_v3_streaming_promotes_reasoning_to_content(
tokenizer: PreTrainedTokenizerBase,
):
# Model never closes <think>: reasoning streams normally AND is duplicated
# into content on the terminal delta.
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"force_nonempty_content": True},
)
parser = _make_reasoning_parser(tokenizer)
reasoning, content = _run_parse_delta(parser, tokenizer, "<think>4", request)
assert reasoning == "4"
assert content == "4"
def test_nemotron_v3_streaming_no_promotion_with_real_content(
tokenizer: PreTrainedTokenizerBase,
):
request = ChatCompletionRequest(
model="test-model",
messages=[],
chat_template_kwargs={"force_nonempty_content": True},
)
parser = _make_reasoning_parser(tokenizer)
reasoning, content = _run_parse_delta(
parser, tokenizer, "<think>reason</think>real answer", request
)
# Real content followed </think>, so nothing is duplicated.
assert reasoning == "reason"
assert content == "real answer"
def test_nemotron_v3_streaming_no_promotion_without_opt_in(
tokenizer: PreTrainedTokenizerBase,
):
# Without enable_thinking=False / force_nonempty_content the fallback must
# stay disabled: the response stays reasoning-only, content empty.
request = ChatCompletionRequest(model="test-model", messages=[])
parser = _make_reasoning_parser(tokenizer)
reasoning, content = _run_parse_delta(parser, tokenizer, "<think>4", request)
assert reasoning == "4"
assert content == ""
@@ -1,163 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from transformers import AutoTokenizer
from tests.reasoning.utils import run_reasoning_extraction
from vllm.reasoning import ReasoningParser, ReasoningParserManager
parser_name = "olmo3"
START_REASONING = "<think>"
END_REASONING = "</think>"
NO_REASONING = {
"output": f"{START_REASONING}{END_REASONING}No thoughts, head empty!",
"reasoning": None,
"content": "No thoughts, head empty!",
}
NO_REASONING_WITH_NEWLINE = {
"output": f"{START_REASONING}\n{END_REASONING}\n\nNo thoughts, head empty!",
"reasoning": "\n",
"content": "\n\nNo thoughts, head empty!",
}
SIMPLE_REASONING = {
"output": f"{START_REASONING}This is a reasoning section{END_REASONING}This is the rest", # noqa: E501
"reasoning": "This is a reasoning section",
"content": "This is the rest",
}
SIMPLE_REASONING_WITH_NEWLINE = {
"output": f"{START_REASONING} Look!\n\nI'm thinking...{END_REASONING}\nThis is the rest", # noqa: E501
"reasoning": " Look!\n\nI'm thinking...",
"content": "\nThis is the rest",
}
SIMPLE_REASONING_WITH_MULTIPLE_NEWLINES = {
"output": f"{START_REASONING}\nLook!\nI'm thinking...\n\n{END_REASONING}\n\n\nThis is the rest", # noqa: E501
"reasoning": "\nLook!\nI'm thinking...\n\n",
"content": "\n\n\nThis is the rest",
}
SIMPLE_REASONING_WITH_TRAILING_SPACE = {
"output": f"{START_REASONING}\nLook!\nI'm thinking... {END_REASONING}\nThis is the rest", # noqa: E501
"reasoning": "\nLook!\nI'm thinking... ",
"content": "\nThis is the rest",
}
NO_REASONING_ONLY_END_THINK = {
"output": f"{END_REASONING}\n\nNo thoughts, head empty!",
"reasoning": None,
"content": "\n\nNo thoughts, head empty!",
}
REASONING_ONLY_END_THINK = {
"output": f"The user is asking me not to think.{END_REASONING}No thoughts!",
"reasoning": "The user is asking me not to think.",
"content": "No thoughts!",
}
TEST_CASES = [
pytest.param(
False, # not streaming
NO_REASONING,
id="no_reasoning",
),
pytest.param(
False, # not streaming
NO_REASONING_WITH_NEWLINE,
id="no_reasoning_with_newline",
),
pytest.param(
False, # not streaming
SIMPLE_REASONING,
id="simple_reasoning",
),
pytest.param(
False, # not streaming
SIMPLE_REASONING_WITH_NEWLINE,
id="simple_reasoning_with_newline",
),
pytest.param(
True, # enable streaming
SIMPLE_REASONING_WITH_MULTIPLE_NEWLINES,
id="simple_reasoning_with_multiple_newlines",
),
pytest.param(
False, # not streaming
NO_REASONING_ONLY_END_THINK,
id="no_reasoning_only_end_think",
),
pytest.param(
False, # not streaming
REASONING_ONLY_END_THINK,
id="yes_reasoning_only_end_think",
),
pytest.param(
True, # enable streaming
NO_REASONING,
id="no_reasoning_streaming",
),
pytest.param(
True, # enable streaming
NO_REASONING_WITH_NEWLINE,
id="no_reasoning_with_newline_streaming",
),
pytest.param(
True, # enable streaming
SIMPLE_REASONING,
id="simple_reasoning_streaming",
),
pytest.param(
True, # enable streaming
SIMPLE_REASONING_WITH_NEWLINE,
id="simple_reasoning_with_newline_streaming",
),
pytest.param(
True, # enable streaming
SIMPLE_REASONING_WITH_MULTIPLE_NEWLINES,
id="simple_reasoning_with_multiple_newlines_streaming",
),
pytest.param(
True, # enable streaming
SIMPLE_REASONING_WITH_TRAILING_SPACE,
id="simple_reasoning_with_trailing_space_streaming",
),
pytest.param(
True, # enable streaming
NO_REASONING_ONLY_END_THINK,
id="no_reasoning_only_end_think_streaming",
),
pytest.param(
True, # enable streaming
REASONING_ONLY_END_THINK,
id="yes_reasoning_only_end_think_streaming",
),
]
# Global tokenizer initialization to avoid repeated loading
tokenizer = AutoTokenizer.from_pretrained("allenai/Olmo-3-7B-Think")
@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_reasoning(
streaming: bool,
param_dict: dict[str, str],
):
output = tokenizer.tokenize(param_dict["output"])
# decode everything to tokens
model_output: list[str] = [
tokenizer.convert_tokens_to_string([token]) for token in output
]
parser_cls = ReasoningParserManager.get_reasoning_parser(parser_name)
parser: ReasoningParser = parser_cls(tokenizer)
reasoning, content = run_reasoning_extraction(
reasoning_parser=parser, model_output=model_output, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
@@ -1,375 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from transformers import AutoTokenizer, PreTrainedTokenizerBase
from tests.reasoning.utils import (
StreamingReasoningReconstructor,
run_reasoning_extraction,
run_reasoning_extraction_streaming,
)
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
from vllm.reasoning import ReasoningParser, ReasoningParserManager
parser_name = "qwen3"
start_token = "<think>"
end_token = "</think>"
REASONING_MODEL_NAMES = [
"Qwen/Qwen3-0.6B",
"Qwen/Qwen3.5-397B-A17B",
"Qwen/Qwen3-4B-Thinking-2507",
]
@pytest.fixture(scope="module", params=REASONING_MODEL_NAMES)
def qwen3_tokenizer(request) -> PreTrainedTokenizerBase:
return AutoTokenizer.from_pretrained(request.param)
# --- <think> in prompt, only </think> in output (typical) ---
WITHOUT_START_TOKEN = {
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
}
WITHOUT_START_TOKEN_STREAM = {
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
}
WITHOUT_START_TOKEN_COMPLETE_REASONING = {
"output": "This is a reasoning section</think>",
"reasoning": "This is a reasoning section",
"content": None,
}
# --- <think> present in output (old template / edge case) ---
WITH_THINK = {
"output": "<think>This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
}
WITH_THINK_STREAM = {
"output": "<think>This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
}
# --- No think tokens at all (thinking enabled, truncated) ---
# With thinking enabled (default), no think tokens means the output was
# truncated before </think> could be generated. All output is reasoning.
WITHOUT_THINK = {
"output": "This is the rest",
"reasoning": "This is the rest",
"content": None,
}
# In streaming, the parser cannot distinguish "thinking disabled" from
# "reasoning in progress" when no think tokens have appeared yet.
# It assumes reasoning. The serving layer handles the "thinking disabled"
# case by checking prompt_is_reasoning_end_arr before calling the parser.
WITHOUT_THINK_STREAM = {
"output": "This is the rest",
"reasoning": "This is the rest",
"content": None,
}
# --- <tool_call> without </think> (implicit reasoning end) ---
TOOL_CALL_BODY = (
"<tool_call>\n<function=bash>\n<parameter=command>"
"\ncat /etc/hosts\n</parameter>\n</function>\n</tool_call>"
)
TOOL_CALL_NO_THINK_END = {
"output": "I need to read the file.\n\n" + TOOL_CALL_BODY,
"reasoning": "I need to read the file.\n\n",
"content": TOOL_CALL_BODY,
}
TOOL_CALL_WITH_THINK_NO_END = {
"output": "<think>I need to read the file.\n\n" + TOOL_CALL_BODY,
"reasoning": "I need to read the file.\n\n",
"content": TOOL_CALL_BODY,
}
# --- Edge cases ---
COMPLETE_REASONING = {
"output": "<think>This is a reasoning section</think>",
"reasoning": "This is a reasoning section",
"content": None,
}
MULTILINE_REASONING = {
"output": "<think>This is a reasoning\nsection</think>This is the rest\nThat",
"reasoning": "This is a reasoning\nsection",
"content": "This is the rest\nThat",
}
# Truncated output: <think> present but no </think> (thinking enabled).
# Everything is reasoning because the output was cut off mid-thought.
ONLY_OPEN_TAG = {
"output": "<think>This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
}
ONLY_OPEN_TAG_STREAM = {
"output": "<think>This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
}
# Truncated output without <think> prefix (Qwen3.5 style where <think>
# is in the prompt). No </think> means truncation — all is reasoning.
TRUNCATED_NO_START_TOKEN = {
"output": "This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
}
TRUNCATED_NO_START_TOKEN_STREAM = {
"output": "This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
}
TEST_CASES = [
pytest.param(
False,
WITHOUT_START_TOKEN,
id="without_start_token",
),
pytest.param(
True,
WITHOUT_START_TOKEN_STREAM,
id="without_start_token_stream",
),
pytest.param(
False,
WITHOUT_START_TOKEN_COMPLETE_REASONING,
id="without_start_token_complete_reasoning",
),
pytest.param(
True,
WITHOUT_START_TOKEN_COMPLETE_REASONING,
id="without_start_token_complete_reasoning_stream",
),
pytest.param(
False,
WITH_THINK,
id="with_think",
),
pytest.param(
True,
WITH_THINK_STREAM,
id="with_think_stream",
),
pytest.param(
False,
WITHOUT_THINK,
id="without_think",
),
pytest.param(
True,
WITHOUT_THINK_STREAM,
id="without_think_stream",
),
pytest.param(
False,
COMPLETE_REASONING,
id="complete_reasoning",
),
pytest.param(
True,
COMPLETE_REASONING,
id="complete_reasoning_stream",
),
pytest.param(
False,
MULTILINE_REASONING,
id="multiline_reasoning",
),
pytest.param(
True,
MULTILINE_REASONING,
id="multiline_reasoning_stream",
),
pytest.param(
False,
ONLY_OPEN_TAG,
id="only_open_tag",
),
pytest.param(
True,
ONLY_OPEN_TAG_STREAM,
id="only_open_tag_stream",
),
pytest.param(
False,
TRUNCATED_NO_START_TOKEN,
id="truncated_no_start_token",
),
pytest.param(
True,
TRUNCATED_NO_START_TOKEN_STREAM,
id="truncated_no_start_token_stream",
),
pytest.param(
False,
TOOL_CALL_NO_THINK_END,
id="tool_call_no_think_end",
),
pytest.param(
True,
TOOL_CALL_NO_THINK_END,
id="tool_call_no_think_end_stream",
),
pytest.param(
False,
TOOL_CALL_WITH_THINK_NO_END,
id="tool_call_with_think_no_end",
),
pytest.param(
True,
TOOL_CALL_WITH_THINK_NO_END,
id="tool_call_with_think_no_end_stream",
),
]
@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_reasoning(
streaming: bool,
param_dict: dict,
qwen3_tokenizer,
):
output = qwen3_tokenizer.tokenize(param_dict["output"])
output_tokens: list[str] = [
qwen3_tokenizer.convert_tokens_to_string([token]) for token in output
]
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
qwen3_tokenizer
)
reasoning, content = run_reasoning_extraction(
parser, output_tokens, streaming=streaming
)
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
# Multi-token delta tests: simulate real-world streaming where a single
# delta can contain multiple tokens (e.g., speculative decoding).
MULTI_TOKEN_DELTA_CASES = [
pytest.param(
# <think> grouped with following text in one delta
["<think>This is a reasoning section", "</think>", "This is the rest"],
"This is a reasoning section",
"This is the rest",
id="start_token_grouped_with_text",
),
pytest.param(
# </think> grouped with following content in one delta
["reasoning section", "</think>This is the rest"],
"reasoning section",
"This is the rest",
id="end_token_grouped_with_content",
),
pytest.param(
# <think> and </think> in the same delta, no content after
["<think>reasoning</think>"],
"reasoning",
None,
id="start_and_end_in_one_delta_no_content",
),
pytest.param(
# No start token, end grouped with content (Qwen3.5 style)
["reasoning section", "</think>content"],
"reasoning section",
"content",
id="no_start_end_grouped_with_content",
),
pytest.param(
# <tool_call> arrives in a separate delta after reasoning text
["I need to read the file.\n\n", "<tool_call>\n<function=bash>"],
"I need to read the file.\n\n",
"<tool_call>\n<function=bash>",
id="tool_call_implicit_reasoning_end",
),
]
@pytest.mark.parametrize(
"deltas, expected_reasoning, expected_content", MULTI_TOKEN_DELTA_CASES
)
def test_reasoning_streaming_multi_token_deltas(
deltas: list[str],
expected_reasoning: str | None,
expected_content: str | None,
qwen3_tokenizer,
):
"""Test that multi-token deltas don't leak <think> into reasoning."""
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
qwen3_tokenizer
)
reconstructor: StreamingReasoningReconstructor = run_reasoning_extraction_streaming(
parser, deltas
)
assert reconstructor.reasoning == expected_reasoning
assert (reconstructor.other_content or None) == expected_content
# --- Tests for enable_thinking=False (thinking explicitly disabled) ---
THINKING_DISABLED_CASES = [
pytest.param(
"This is plain content",
None,
"This is plain content",
id="thinking_disabled_plain_content",
),
pytest.param(
"Some output without think tokens",
None,
"Some output without think tokens",
id="thinking_disabled_no_think_tokens",
),
pytest.param(
"I need to read the file.\n\n" + TOOL_CALL_BODY,
None,
"I need to read the file.\n\n" + TOOL_CALL_BODY,
id="thinking_disabled_with_tool_call",
),
]
@pytest.mark.parametrize(
"output, expected_reasoning, expected_content", THINKING_DISABLED_CASES
)
def test_reasoning_thinking_disabled(
output: str,
expected_reasoning: str | None,
expected_content: str | None,
qwen3_tokenizer,
):
"""When enable_thinking=False, output without </think> is all content."""
parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)(
qwen3_tokenizer,
chat_template_kwargs={"enable_thinking": False},
)
reasoning, content = parser.extract_reasoning(
model_output=output,
request=ChatCompletionRequest(messages=[], model="test-model"),
)
assert reasoning == expected_reasoning
assert content == expected_content
@@ -1,27 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from vllm.reasoning import ReasoningParserManager
from vllm.tokenizers import get_tokenizer
pytestmark = pytest.mark.skip_global_cleanup
REASONING_MODEL_NAME = "stepfun-ai/step3"
@pytest.fixture(scope="module")
def step3_parser():
tokenizer = get_tokenizer(tokenizer_name=REASONING_MODEL_NAME)
return ReasoningParserManager.get_reasoning_parser("step3")(tokenizer)
def test_count_reasoning_tokens_stops_at_think_end(step3_parser):
end = step3_parser.think_end_token_id
assert step3_parser.count_reasoning_tokens([11, 12, end, 13]) == 2
def test_count_reasoning_tokens_without_think_end(step3_parser):
"""Step3 treats output as reasoning until </think> appears."""
assert step3_parser.count_reasoning_tokens([11, 12, 13]) == 3
@@ -1,338 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
from tests.reasoning.utils import run_reasoning_extraction
from vllm.parser.engine.registered_adapters import Step3p5ParserReasoningAdapter
from vllm.reasoning import ReasoningParserManager
from vllm.tokenizers import get_tokenizer
parser_name = "step3p5"
start_token = "<think>"
end_token = "</think>"
REASONING_MODEL_NAME = "stepfun-ai/Step-3.5-Flash"
@pytest.fixture(scope="module")
def step3p5_tokenizer():
return get_tokenizer(tokenizer_name=REASONING_MODEL_NAME)
SIMPLE_REASONING = {
"output": "This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
}
COMPLETE_REASONING = {
"output": "This is a reasoning section</think>",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": True,
}
NO_CONTENT = {
"output": "This is content",
"reasoning": "This is content",
"content": None,
"is_reasoning_end": False,
}
NO_REASONING_STREAMING = {
"output": "This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": False,
}
MULTIPLE_LINES = {
"output": "This\nThat</think>This is the rest\nThat",
"reasoning": "This\nThat",
"content": "This is the rest\nThat",
"is_reasoning_end": True,
}
SHORTEST_REASONING_NO_STREAMING = {
"output": "</think>This is the rest",
"reasoning": None,
"content": "This is the rest",
"is_reasoning_end": True,
}
SHORTEST_REASONING = {
"output": "</think>This is the rest",
"reasoning": None,
"content": "This is the rest",
"is_reasoning_end": True,
}
REASONING_WITH_THINK = {
"output": "<think>This is a reasoning section</think>This is the rest",
"reasoning": "This is a reasoning section",
"content": "This is the rest",
"is_reasoning_end": True,
}
COMPLETE_REASONING_WITH_THINK = {
"output": "<think>This is a reasoning section</think>",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": True,
}
MULTIPLE_LINES_WITH_THINK = {
"output": "<think>This\nThat</think>This is the rest\nThat",
"reasoning": "This\nThat",
"content": "This is the rest\nThat",
"is_reasoning_end": True,
}
SHORTEST_REASONING_NO_STREAMING_WITH_THINK = {
"output": "</think>This is the rest",
"reasoning": None,
"content": "This is the rest",
"is_reasoning_end": True,
}
SHORTEST_REASONING_WITH_THINK = {
"output": "</think>This is the rest",
"reasoning": None,
"content": "This is the rest",
"is_reasoning_end": True,
}
THINK_NO_END = {
"output": "<think>This is a reasoning section",
"reasoning": "This is a reasoning section",
"content": None,
"is_reasoning_end": False,
}
EMPTY = {
"output": "",
"reasoning": None,
"content": None,
"is_reasoning_end": False,
}
EMPTY_STREAMING = {
"output": "",
"reasoning": None,
"content": None,
"is_reasoning_end": False,
}
NEW_LINE = {
"output": "\n<think>This is a reasoning section</think>\nThis is the rest",
"reasoning": "\nThis is a reasoning section",
"content": "\nThis is the rest",
"is_reasoning_end": True,
}
NEW_LINE_STREAMING = {
"output": "\n<think>This is a reasoning section\n</think>\nThis is the rest",
"reasoning": "\nThis is a reasoning section",
"content": "\nThis is the rest",
"is_reasoning_end": True,
}
NEW_LINE_STREAMING_COMPLEX_CONTENT = {
"output": "\n This is a \n reasoning section\n\n\n</think>\n\nThis is the rest",
"reasoning": "\n This is a \n reasoning section",
"content": "\n\nThis is the rest",
"is_reasoning_end": True,
}
MULTI_TURN_PROMPT_CONTENT = {
"output": "<think> This is last turn's reasoning section </think> hello <think>",
"reasoning": "",
"content": "",
"is_reasoning_end": False,
}
TEST_CASES = [
pytest.param(
False,
SIMPLE_REASONING,
id="simple_reasoning",
),
pytest.param(
True,
SIMPLE_REASONING,
id="simple_reasoning_streaming",
),
pytest.param(
False,
COMPLETE_REASONING,
id="complete_reasoning",
),
pytest.param(
True,
COMPLETE_REASONING,
id="complete_reasoning_streaming",
),
pytest.param(
False,
NO_CONTENT,
id="no_content_token",
),
pytest.param(
True,
NO_REASONING_STREAMING,
id="no_reasoning_token_streaming",
),
pytest.param(
False,
MULTIPLE_LINES,
id="multiple_lines",
),
pytest.param(
True,
MULTIPLE_LINES,
id="multiple_lines_streaming",
),
pytest.param(
True,
SHORTEST_REASONING,
id="shortest",
),
pytest.param(
False,
SHORTEST_REASONING_NO_STREAMING,
id="shortest_streaming",
),
pytest.param(
False,
REASONING_WITH_THINK,
id="reasoning_with_think",
),
pytest.param(
True,
REASONING_WITH_THINK,
id="reasoning_with_think_streaming",
),
pytest.param(
False,
COMPLETE_REASONING_WITH_THINK,
id="complete_reasoning_with_think",
),
pytest.param(
True,
COMPLETE_REASONING_WITH_THINK,
id="complete_reasoning_with_think_streaming",
),
pytest.param(
False,
MULTIPLE_LINES_WITH_THINK,
id="multiple_lines_with_think",
),
pytest.param(
True,
MULTIPLE_LINES_WITH_THINK,
id="multiple_lines_with_think_streaming",
),
pytest.param(
False,
SHORTEST_REASONING_NO_STREAMING_WITH_THINK,
id="shortest_with_think",
),
pytest.param(
True,
SHORTEST_REASONING_WITH_THINK,
id="shortest_with_think_streaming",
),
pytest.param(
False,
THINK_NO_END,
id="think_no_end",
),
pytest.param(
True,
THINK_NO_END,
id="think_no_end_streaming",
),
pytest.param(
False,
EMPTY,
id="empty",
),
pytest.param(
True,
EMPTY_STREAMING,
id="empty_streaming",
),
pytest.param(
False,
NEW_LINE,
id="new_line",
),
pytest.param(
True,
NEW_LINE_STREAMING,
id="new_line_streaming",
),
pytest.param(
True,
NEW_LINE_STREAMING_COMPLEX_CONTENT,
id="new_line_streaming_complex_content",
),
pytest.param(
True,
MULTI_TURN_PROMPT_CONTENT,
id="multi_turn_prompt_content",
),
]
@pytest.mark.parametrize("streaming, param_dict", TEST_CASES)
def test_reasoning(
streaming: bool,
param_dict: dict,
step3p5_tokenizer,
request,
):
output = step3p5_tokenizer.tokenize(param_dict["output"])
# decode everything to tokens
output_tokens: list[str] = [
step3p5_tokenizer.convert_tokens_to_string([token]) for token in output
]
parser = ReasoningParserManager.get_reasoning_parser(parser_name)(step3p5_tokenizer)
assert isinstance(parser, Step3p5ParserReasoningAdapter)
reasoning, content = run_reasoning_extraction(
parser, output_tokens, streaming=streaming
)
print(f"reasoning: {reasoning}")
print(f"content: {content}")
test_id = request.node.callspec.id if hasattr(request.node, "callspec") else None
if request.node.callspec.id != "multi_turn_prompt_content":
assert reasoning == param_dict["reasoning"]
assert content == param_dict["content"]
# Test is_reasoning_end
output_ids = step3p5_tokenizer.convert_tokens_to_ids(output)
is_reasoning_end = parser.is_reasoning_end(output_ids)
assert is_reasoning_end == param_dict["is_reasoning_end"]
# Test is_reasoning_end_streaming
current_ids: list[int] = []
for token_id in output_ids:
current_ids.append(token_id)
if token_id in parser.reasoning_end_token_ids:
assert parser.is_reasoning_end_streaming(current_ids, [token_id])
if param_dict["is_reasoning_end"]:
assert parser.is_reasoning_end_streaming(output_ids, output_ids)
break
assert not parser.is_reasoning_end_streaming(current_ids, [token_id])
else:
assert not parser.is_reasoning_end_streaming(output_ids, output_ids)
# Test extract_content
if param_dict["content"] is not None:
content_ids = parser.extract_content_ids(output_ids)
# Fixed expected token ids for specific test cases
test_id = (
request.node.callspec.id if hasattr(request.node, "callspec") else None
)
# Match most specific first
if test_id not in [
"new_line_streaming_complex_content",
"new_line_streaming",
"new_line",
"multi_turn_prompt_content",
]:
expected_content_ids = step3p5_tokenizer.convert_tokens_to_ids(
step3p5_tokenizer.tokenize(param_dict["content"])
)
assert content_ids == expected_content_ids
elif param_dict["is_reasoning_end"]:
assert parser.extract_content_ids(output_ids) == []
-162
View File
@@ -1,162 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from typing import cast
from transformers import PreTrainedTokenizerBase
from vllm.entrypoints.generate.base.protocol import DeltaMessage
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
from vllm.reasoning import ReasoningParser
from vllm.utils.mistral import is_mistral_tokenizer
class StreamingReasoningReconstructor:
def __init__(self):
self.reasoning = None
self.other_content = None
def append_delta(self, delta: DeltaMessage):
# content and the reasoning content should not be present
# at the same time
assert delta.content is None or delta.reasoning is None, (
"Both content and reasoning content are present in the delta message"
)
if delta.content is not None:
if self.other_content is None:
self.other_content = delta.content
else:
self.other_content += delta.content
else:
if self.reasoning is None:
self.reasoning = delta.reasoning
else:
self.reasoning += delta.reasoning
def run_reasoning_extraction(
reasoning_parser: ReasoningParser,
model_output: list[str],
request: ChatCompletionRequest | None = None,
streaming: bool = False,
) -> tuple[str | None, str | None]:
if streaming:
reconstructor = run_reasoning_extraction_streaming(
reasoning_parser,
model_output,
request,
)
return (
reconstructor.reasoning,
reconstructor.other_content or None,
)
else:
reasoning, content = run_reasoning_extraction_nonstreaming(
reasoning_parser, model_output, request
)
return reasoning, content
def run_reasoning_extraction_mistral(
reasoning_parser: ReasoningParser,
model_output: list[int],
request: ChatCompletionRequest | None = None,
streaming: bool = False,
) -> tuple[str | None, str | None]:
assert is_mistral_tokenizer(reasoning_parser.model_tokenizer), type(
reasoning_parser.model_tokenizer
)
if streaming:
reconstructor = run_reasoning_extraction_streaming_mistral(
reasoning_parser,
model_output,
request,
)
return (
reconstructor.reasoning,
reconstructor.other_content or None,
)
else:
str_output = reasoning_parser.model_tokenizer.convert_ids_to_tokens(
model_output
)
reasoning, content = run_reasoning_extraction_nonstreaming(
reasoning_parser, str_output, request
)
return reasoning, content
def run_reasoning_extraction_nonstreaming(
reasoning_parser: ReasoningParser,
model_output: list[str],
request: ChatCompletionRequest | None = None,
) -> tuple[str | None, str | None]:
request = request or ChatCompletionRequest(messages=[], model="test-model")
return reasoning_parser.extract_reasoning(
model_output="".join(model_output), request=request
)
def run_reasoning_extraction_streaming(
reasoning_parser: ReasoningParser,
model_deltas: list[str],
request: ChatCompletionRequest | None = None,
) -> StreamingReasoningReconstructor:
request = request or ChatCompletionRequest(messages=[], model="test-model")
reconstructor = StreamingReasoningReconstructor()
previous_text = ""
previous_tokens: list[int] = []
tokenizer = cast(PreTrainedTokenizerBase, reasoning_parser.model_tokenizer)
for delta in model_deltas:
token_delta = [
reasoning_parser.vocab[token]
for token in tokenizer.tokenize(delta)
if token in reasoning_parser.vocab
]
current_text = previous_text + delta
current_tokens = previous_tokens + token_delta
delta_message = reasoning_parser.extract_reasoning_streaming(
previous_text,
current_text,
delta,
previous_tokens,
current_tokens,
token_delta,
)
if delta_message is not None:
reconstructor.append_delta(delta_message)
previous_text = current_text
previous_tokens = current_tokens
return reconstructor
def run_reasoning_extraction_streaming_mistral(
reasoning_parser: ReasoningParser,
model_deltas: list[int],
request: ChatCompletionRequest | None = None,
) -> StreamingReasoningReconstructor:
assert is_mistral_tokenizer(reasoning_parser.model_tokenizer), type(
reasoning_parser.model_tokenizer
)
request = request or ChatCompletionRequest(messages=[], model="test-model")
reconstructor = StreamingReasoningReconstructor()
previous_text = ""
previous_tokens: list[int] = []
for model_delta in model_deltas:
token_delta = [model_delta]
delta = reasoning_parser.model_tokenizer.convert_ids_to_tokens([model_delta])[0]
current_text = previous_text + delta
current_tokens = previous_tokens + token_delta
delta_message = reasoning_parser.extract_reasoning_streaming(
previous_text,
current_text,
delta,
previous_tokens,
current_tokens,
token_delta,
)
if delta_message is not None:
reconstructor.append_delta(delta_message)
previous_text = current_text
previous_tokens = current_tokens
return reconstructor