fix: prevent infinite thinking loop from cross-chunk tag truncation in Qwen3 streaming#31119
Open
HH1162 wants to merge 4 commits into
Open
fix: prevent infinite thinking loop from cross-chunk tag truncation in Qwen3 streaming#31119HH1162 wants to merge 4 commits into
HH1162 wants to merge 4 commits into
Conversation
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
Author
|
Fixes #31118 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When deploying Qwen3 models with reasoning capabilities via SGLang's streaming mode, the client occasionally enters an "infinite thinking" state. The model continuously generates reasoning content without transitioning to the final answer phase, causing request timeouts and GPU resource exhaustion.
Root Cause
In streaming scenarios, the engine's chunking mechanism can physically split the thinking tags (
and) across different network chunks. Traditional regex or naive string matching on individual chunks fails to detect the complete tag boundary. As a result, the parser never receives the "end of thinking" signal, causing the state machine to hang indefinitely. This is an application-layer chunking boundary issue, not a network MTU problem.Solution
Introduce a robust streaming parser (
ProductionQwen3Detector) based on a strict state machine and prefix buffering mechanism:_calculate_safe_length()inspects chunk tails for partial tag prefixes (e.g.,<or</th) and retains them in a buffer until the next chunk arrives, preventing tag fragmentation.whileloop continuously consumes concatenated text. Once a complete tag is found, it performs precise slicing (before_tag/after_tag), ensuring matched tags are completely discarded and never linger in the buffer.reset()at pipeline entry, preventing worker reuse from polluting cross-request state.Impact
qwen3reasoning parser (e.g., Qwen3, QwQ).stream: true).reasoning_contentandcontentfields to streaming responses; existing fields remain unchanged. No breaking changes.Testing
CI States
Latest PR Test (Base): ❌ Run #29313189894
Latest PR Test (Extra): ❌ Run #29313189851