#35083·sglang

[Bug] Reasoning parsers: text before the opening think token is routed four different ways (chunk-dependent, and one-shot keeps the literal marker)

Author: tancheng33Created Aug 17, 2026Updated Sep 26, 2026

Checklist

  • I searched related issues but found no solution.
  • The bug persists in the latest version.
  • Issues without environment info and a minimal reproducible demo are hard to resolve and may receive no feedback.
  • If this is not a bug report but a general question, please start a discussion at https://github.com/sgl-project/sglang/discussions. Otherwise, it will be closed.
  • Please use English. Otherwise, it will be closed.

Describe the bug

BaseReasoningFormatDetector decides that a text is a reasoning block with self.think_start_token in text (the marker anywhere), but removes the marker with while processed_text.startswith(think_start_text) (the marker at index 0). Anything the model writes in front of the opening marker — including a single space or newline — falls into that gap.

The streaming path has the mirror-image gap: it only recognises a partial opening marker when the whole buffer is a prefix of it, so a chunk that carries content first ("Sure." + "<") clears the buffer and the rest of the marker arrives with nothing to attach to.

Same string, four different answers on main (b6d760291, --reasoning-parser qwen3):

path reasoning_content content
non-streaming detect_and_parse 'Sure.<think>checking' 'Done.'
streaming, marker whole in one chunk 'checking' 'Sure.Done.'
streaming, chunk ends inside <think> '' 'Sure.<think>checking</think>Done.'
streaming, whole turn in one chunk 'Sure.checking' 'Done.'

Row 1 hands the client a literal <think> inside reasoning_content. Row 3 is the damaging one: the entire reasoning block, markers included, is streamed as assistant content and reasoning_content is empty.

Reproduction

CPU only, no model needed:

python
from sglang.srt.parser.reasoning_parser import ReasoningParser

TEXT = "Sure.<think>checking</think>Done."

def stream(chunks):
    p = ReasoningParser(model_type="qwen3")
    r = n = ""
    for c in chunks:
        a, b = p.parse_stream_chunk(c)
        r += a or ""; n += b or ""
    a, b = p.parse_stream_end()
    return r + (a or ""), n + (b or "")

print(ReasoningParser(model_type="qwen3").parse_non_stream(TEXT))
print(stream(["Sure.", "<think>", "checking", "</think>", "Done."]))
print(stream(["Sure.<", "think>", "checking", "</think>", "Done."]))
print(stream([TEXT]))
('Sure.<think>checking', 'Done.')
('checking', 'Sure.Done.')
('', 'Sure.<think>checking</think>Done.')
('Sure.checking', 'Done.')

A single leading newline is enough for the non-streaming half — this is the common shape, not a contrived one:

python
ReasoningParser(model_type="qwen3").parse_non_stream("\n<think>checking</think>Done.")
# ('\n<think>checking', 'Done.')   <- the marker reaches the client

Mechanism

python/sglang/srt/parser/reasoning_parser.py

  • One-shot, _detect_and_parse_impl: in_reasoning is decided by think_start_token in text (any position), but the strip loop is startswith. With a lead the marker is never removed, and the lead itself is classified as reasoning.
  • Streaming, _parse_streaming_increment_impl:
    • the strip is current_text.replace(think_start_text, "", 1) with no split of what preceded it, so a lead that arrives in the same buffer as the marker is absorbed into reasoning;
    • the not-in-reasoning branch ends with self._buffer = "" and emits the whole buffer as content — no _ends_with_partial_token holdback for the opening marker, unlike the in-reasoning branch which holds back the closing one. That is row 3.

When row 3 is reachable

It needs one chunk to carry content followed by a strict prefix of the opening marker. With stream_interval=1 and <think> as a single special token that cannot happen; it becomes reachable when a decode step delivers several tokens at once — --stream-interval > 1, sampling_params.stream_interval, speculative decoding, or the detokenizer releasing several tokens after holding an incomplete UTF-8 sequence. Rows 1, 2 and 4 need no such condition.

Blast radius

19 of the 26 entries in ReasoningParser.DetectorMap — every one that inherits the base implementations: deepseek-r1, deepseek-v3, deepseek-v4, glm45, hunyuan, kimi, kimi_k2, mimo, poolside_v1, qwen3, qwen3-thinking, minimax, minimax-m3, step3, step3p5, mistral, nemotron_3, interns1, gemma4. (gpt-oss, kimi_k3, muse, inkling, apertus2509, cohere_command4 override both methods and are not affected by this specific gap.)

Related

TestStreamingChunkSizeInvariance in test/registered/unit/parser/test_reasoning_parser.py, added by #34458, already names this case and pins it as a known exception:

python
def test_text_before_think_token_is_chunk_dependent(self):
    """Accepted divergence, inherited from main: text before `<think>` lands
    in reasoning or content depending on where the chunk boundary falls."""

That test asserts the three streaming variants and the fourth one-shot split as the expected outcome. This issue is a request to close that exception rather than keep it pinned.

Expected behaviour

The block opens at the marker, so the routing should not depend on chunking:

  • the marker is removed wherever it first appears, not only at index 0;
  • text in front of it is content — except under force_reasoning, where the chat template already opened the block, and it is reasoning;
  • both are what the streaming path already does when the marker happens to land whole at a chunk start.

I have a fix and regression tests ready (the four rows collapse to one, and the pinned test above becomes an invariance test). Happy to open the PR.

Environment

main @ b6d760291, CPU-only unit environment, Python 3.12.