Repository navigation
Fix SGLANG_DEFAULT_THINKING ignored by dsv4/dsv32 reasoning parser - #28288
WeiLai5432 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request addresses an issue where the reasoning parser and prompt template disagree on the thinking mode for DeepSeek v4/v32, which can cause tags to leak into the output. It resolves this by persisting the resolved thinking decision back onto the request's chat_template_kwargs and adds unit tests to verify the behavior. However, the review feedback correctly points out that performing this normalization inside _apply_jinja_template is too late in the pipeline, as _process_messages evaluates the thinking mode beforehand and can bypass _apply_jinja_template entirely. It is recommended to move this logic to the beginning of _process_messages.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| # Single source of truth: persist the resolved thinking decision back | ||
| # onto the request so the reasoning parser agrees with the prompt we | ||
| # actually built. Otherwise SGLANG_DEFAULT_THINKING turns thinking ON | ||
| # in the dsv4/dsv32 prompt while _get_reasoning_from_request still | ||
| # treats the request as non-thinking, leaking </think> into content. | ||
| # Scoped to the custom encoder path, which is the only one env drives. | ||
| if self.chat_encoding_spec in ("dsv4", "dsv32"): | ||
| ctk = dict(request.chat_template_kwargs or {}) | ||
| ctk.setdefault("thinking", thinking_requested) | ||
| request.chat_template_kwargs = ctk |
There was a problem hiding this comment.
While persisting the resolved thinking decision back onto the request is the correct approach, doing it inside _apply_jinja_template is too late for some parts of the pipeline:\n\n1. At line 569 (inside _process_messages), thinking_mode = self._get_reasoning_from_request(request) is called before _apply_jinja_template runs. This means thinking_mode (and consequently xgrammar_reasoning) will be evaluated using the un-updated request.chat_template_kwargs, ignoring the SGLANG_DEFAULT_THINKING default.\n2. If request.input_ids is provided, _process_messages returns early (line 612) and bypasses _apply_jinja_template entirely, meaning the default is never persisted.\n\nTo make this more robust and centralized, we should perform this request normalization at the very beginning of _process_messages instead, and remove it from here.
|
Thanks for the review — good catch, the feedback is correct. Addressed in the latest commit:
|
…4600 — reasoning leak, effort mapping, tool-call streaming Three upstream fixes ported to b300-glm52 (v0.5.15 base): 1. sgl-project#28288 — SGLANG_DEFAULT_THINKING ignored by dsv4 reasoning parser Root cause: prompt encoder reads SGLANG_DEFAULT_THINKING but reasoning parser does not, so thinking prompt produces output that parser treats as non-thinking → reasoning text + leaks into content. Fix: _process_messages persists thinking=envs.SGLANG_DEFAULT_THINKING onto chat_template_kwargs so both prompt encoder and parser agree. 2. sgl-project#33140 — reasoning_effort mapped one level off (high is no-op) Root cause: encoding_dsv4.py only supports max/None/high (preview profile). Official profile has 3 levels: low (empty), high, max. Fix: add _detect_dsv4_reasoning_effort_profile() that reads encoding/encoding_dsv4.py from model path to detect official vs preview. Official profile accepts low/high/max; preview accepts high/max. Override via SGLANG_DSV4_REASONING_EFFORT_PROFILE env. 3. sgl-project#34600 — Harden DeepSeek-V4 tool-call streaming (4 bugs) Bug 1: exception handler clears self._buffer → tool call permanently lost Bug 2: rstrip(token) truncates values ending with same chars Bug 3: bare '|DSML|' marker traps response text mentioning DSML tags Bug 4: finish() leaks partial DSML tags into normal_text Fix: _DSML_TOOL_TAGS (specific tags), removesuffix, targeted trap check, finish() strips partial tags.
For the DeepSeek V4 / V3.2 custom encoder path, SGLANG_DEFAULT_THINKING
was only read on the prompt-encoding side (`_apply_jinja_template`). The
reasoning parser side (`_get_reasoning_from_request`) never consults the
env, so when an operator set SGLANG_DEFAULT_THINKING=1 and the client did
not pass `chat_template_kwargs={"thinking": true}`, the model was forced
into thinking mode in the prompt while the parser still treated the
request as non-thinking. The leading reasoning and the literal `</think>`
marker then leaked into `content` and `reasoning_content` came back empty.
Make the resolved thinking decision a single source of truth: persist it
back onto `request.chat_template_kwargs` so the parser agrees with the
prompt we actually built. Scoped to the dsv4/dsv32 custom encoder path,
the only path the env actually drives, so jinja-template models are
unaffected. An explicit `thinking` value in the request still wins.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
…om encoders
Address review feedback: normalizing inside _apply_jinja_template was too
late and could be bypassed.
- _process_messages evaluates thinking_mode (for the xgrammar tool-call
constraint) via _get_reasoning_from_request *before* _apply_jinja_template
runs, so the constraint saw the un-normalized request.
- _apply_jinja_template is skipped entirely when request.input_ids is set or
a conversation template is used, so the normalization never ran for those.
Move the resolve-and-persist block to the top of _process_messages, before
any consumer reads the thinking decision, and broaden the scope guard from
the hardcoded ("dsv4", "dsv32") to `chat_encoding_spec is not None` so every
custom encoder path is covered. Add a regression test for the input_ids
bypass and assert unrelated chat_template_kwargs keys are preserved.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
8b6be18 to
ade835b
Compare
|
Rebased onto latest main ( Recap for reviewers:
Note: cc @hnyls2002 for a look when convenient — happy to adjust if you'd rather resolve the env fallback inside the reasoning parser instead. |
|
Verified this failure mode is also live on the deepseek-v41 parser
Happy to send a dsv41 unit-test addition if useful. |
|
We hit the same leak on DeepSeek-V4.1-Flash ( Setup: DeepSeek-V4.1-Flash on 8×H200, TP8, image
So the env default now drives both the prompt and the parser, and an explicit flag still wins. No server errors in either run. This also answers the question above about Tests on current
With this change to the test file only, the four new tests pass. Without the source change, three of them fail ( - from sglang.srt.managers.template_manager import TemplateManager
+ from sglang.srt.parser.template_manager import TemplateManager
@@
mock_hf_config.architectures = ["DeepseekV4ForCausalLM"]
+ mock_hf_config.to_dict.return_value = {}The test hunk also needs a rebase onto current |
Motivation
For the DeepSeek V4 / V3.2 custom encoder path,
SGLANG_DEFAULT_THINKINGis only read on the prompt-encoding side (_apply_jinja_template,serving_chat.py:648). The reasoning parser side (_get_reasoning_from_request) never consults the env.As a result, when an operator sets
SGLANG_DEFAULT_THINKING=1and the client does not passchat_template_kwargs={"thinking": true}:<|Assistant|><think>), so the model does think;_get_reasoning_from_requestreturnsFalse(dsv4 has no chat template →reasoning_config=None→ theexplicit_thinkingbranch only returnsTruewhen the request explicitly setsthinking=True);is_force_reasoning=False, and since the model output does not re-emit a leading<think>(it's already in the prompt), the detector treats nothing as reasoning.Net effect: the leading reasoning text and the literal
</think>marker leak intocontent, whilereasoning_contentcomes back empty. This is confusing — the env appears to "half-enable" thinking.Modification
Make the resolved thinking decision a single source of truth: after resolving
thinking_requested(request value, falling back to the env), persist it back ontorequest.chat_template_kwargsso the reasoning parser agrees with the prompt we actually built.chat_encoding_spec in ("dsv4", "dsv32")) — the only path the env actually drives. Jinja-template models are unaffected.setdefault, so an explicitthinkingvalue in the request still wins over the env default.Tests
Added three unit tests in
test_serving_chat.py:{"thinking": True}and parser agrees (reasoningenabled);{"thinking": False}, parser stays off;thinking=Falsein request + env ON → explicit value wins.🤖 Generated with Claude Code
CI States
Latest PR Test (Base): ❌ Run #33157064760
Latest PR Test (Extra): ❌ Run #33157064494
Latest PR Test (AMD ROCm 7.2): ❌ Run #33157064716