-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[Bugfix] Fix step3p5 reasoning with interleaved thinking #34211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chaunceyjiang
merged 2 commits into
vllm-project:main
from
mariohong128:fix_step3p5_reasoning
Feb 25, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,341 @@ | ||
| # 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 = "step3p5" | ||
| start_token = "<think>" | ||
| end_token = "</think>" | ||
|
|
||
| REASONING_MODEL_NAME = "stepfun-ai/Step-3.5-Flash" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def step3p5_tokenizer(): | ||
| return AutoTokenizer.from_pretrained(REASONING_MODEL_NAME) | ||
|
|
||
|
|
||
| SIMPLE_REASONING = { | ||
| "output": "This is a reasoning section</think>This is the rest", | ||
| "reasoning_content": "This is a reasoning section", | ||
| "content": "This is the rest", | ||
| "is_reasoning_end": True, | ||
| } | ||
| # need to get into parser again to remove newline after </think> | ||
| COMPLETE_REASONING = { | ||
| "output": "This is a reasoning section</think>", | ||
| "reasoning_content": "This is a reasoning section", | ||
| "content": None, | ||
| "is_reasoning_end": False, | ||
| } | ||
| NO_CONTENT = { | ||
| "output": "This is content", | ||
| "reasoning_content": "This is content", | ||
| "content": None, | ||
| "is_reasoning_end": False, | ||
| } | ||
| NO_REASONING_STREAMING = { | ||
| "output": "This is a reasoning section", | ||
| "reasoning_content": "This is a reasoning section", | ||
| "content": None, | ||
| "is_reasoning_end": False, | ||
| } | ||
| MULTIPLE_LINES = { | ||
| "output": "This\nThat</think>This is the rest\nThat", | ||
| "reasoning_content": "This\nThat", | ||
| "content": "This is the rest\nThat", | ||
| "is_reasoning_end": True, | ||
| } | ||
| SHORTEST_REASONING_NO_STREAMING = { | ||
| "output": "</think>This is the rest", | ||
| "reasoning_content": None, | ||
| "content": "This is the rest", | ||
| "is_reasoning_end": True, | ||
| } | ||
| SHORTEST_REASONING = { | ||
| "output": "</think>This is the rest", | ||
| "reasoning_content": 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_content": "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_content": "This is a reasoning section", | ||
| "content": None, | ||
| "is_reasoning_end": False, | ||
| } | ||
| MULTIPLE_LINES_WITH_THINK = { | ||
| "output": "<think>This\nThat</think>This is the rest\nThat", | ||
| "reasoning_content": "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": None, | ||
| "content": "This is the rest", | ||
| "is_reasoning_end": True, | ||
| } | ||
| SHORTEST_REASONING_WITH_THINK = { | ||
| "output": "</think>This is the rest", | ||
| "reasoning_content": None, | ||
| "content": "This is the rest", | ||
| "is_reasoning_end": True, | ||
| } | ||
| THINK_NO_END = { | ||
| "output": "<think>This is a reasoning section", | ||
| "reasoning_content": "This is a reasoning section", | ||
| "content": None, | ||
| "is_reasoning_end": False, | ||
| } | ||
| EMPTY = { | ||
| "output": "", | ||
| "reasoning_content": None, | ||
| "content": None, | ||
| "is_reasoning_end": False, | ||
| } | ||
| EMPTY_STREAMING = { | ||
| "output": "", | ||
| "reasoning_content": None, | ||
| "content": None, | ||
| "is_reasoning_end": False, | ||
| } | ||
| NEW_LINE = { | ||
| "output": "\n<think>This is a reasoning section</think>\nThis is the rest", | ||
| "reasoning_content": "This is a reasoning section", | ||
| "content": "This 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_content": "\nThis is a reasoning section", | ||
| "content": "This 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_content": "\n This is a \n reasoning section\n\n", | ||
| "content": "\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": "", | ||
| "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: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)( | ||
| step3p5_tokenizer | ||
| ) | ||
|
|
||
| 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_content"] | ||
| assert content == param_dict["content"] | ||
|
|
||
| # Test is_reasoning_end | ||
| output_ids = step3p5_tokenizer.convert_tokens_to_ids(output) | ||
| if streaming: | ||
| 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 = 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 == expected_content_ids | ||
| else: | ||
| content = parser.extract_content_ids(output) | ||
| assert content == [] | ||
|
|
||
|
|
||
| def test_step3p5_streaming_drops_leading_newline(step3p5_tokenizer): | ||
| parser_cls = ReasoningParserManager.get_reasoning_parser("step3p5") | ||
| parser = parser_cls(step3p5_tokenizer) | ||
| output = "<think>calc</think>\nAnswer" | ||
| tokens = step3p5_tokenizer.tokenize(output) | ||
| output_tokens = [ | ||
| step3p5_tokenizer.convert_tokens_to_string([token]) for token in tokens | ||
| ] | ||
|
|
||
| _, content = run_reasoning_extraction(parser, output_tokens, streaming=True) | ||
| assert content == "Answer" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test for
is_reasoning_endin streaming mode is not correctly testing the streaming behavior. It calls the non-streamingparser.is_reasoning_end(output_ids)method, which evaluates the entire output at once. The stateful, incremental logic ofis_reasoning_end_streamingwhich operates ondelta_idsis not being exercised. This is a significant testing gap for a critical part of the bug fix.To properly test the streaming logic, you should simulate the streaming process and call
is_reasoning_end_streamingat each step. Here is an example of how you could structure such a test:This would ensure the state transitions of the parser are correctly handled in streaming mode.