Repository navigation
fix(server): derive the thinking_mode sentinel from the chat template - #1547
Merged
Merged
Conversation
A DeepSeek-V4 request with thinking enabled rendered the non-thinking branch and returned a normal completion, with no error anywhere. #811 injects thinking_mode: "enabled" when a template mentions the identifier. That is one family's convention. DeepSeek-V4 uses another: alias the variable through default(), then compare the alias against 'thinking'. "enabled" is not 'thinking', so the injected value fell through to the else branch. The alias fired, the kwarg was delivered, and the template ignored it. The sentinel now comes from the template. Collect the identifiers carrying the value (thinking_mode itself, plus any `set <name> = thinking_mode` alias), collect the string literals those are compared against with ==, on either side, and inject the literal when exactly one is found. Zero or several, fall back to "enabled" so every template that works today keeps working: a wrong sentinel is silent, so guessing would be worse than the bug it replaces. The default() argument is deliberately not a candidate. It is the template's OFF value ('chat' here), and injecting it would be worse than injecting nothing. #811's precedence is unchanged: an explicit client thinking_mode always wins, and nothing is injected when thinking is disabled. Why the existing tests missed it: THINKING_MODE_LIKE_TEMPLATE compares thinking_mode directly against "enabled", the convention #811 was written for, and wants_thinking_mode_alias is only a substring scan, so it returns true for both conventions while the suite could only see one. The new tests cover the aliased shape, the real checkpoint's own chat_template.jinja, both fallback cases, whole-word identifier matching, and the reversed comparison order.
4 tasks done
inureyes
added a commit
that referenced
this pull request
Aug 31, 2026
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.
Summary
A DeepSeek-V4 request with thinking enabled rendered the non-thinking branch and returned a normal completion, with no error anywhere. This derives the
thinking_modesentinel from the chat template instead of hardcoding one.The bug
#811 (issue #775) injects
thinking_mode: "enabled"when thinking is enabled and the template mentions the identifier. That is one family's convention. DeepSeek-V4 uses another:"enabled"is not'thinking', so the injected value fell through to the else branch. The alias fired, the kwarg was delivered, the template ignored it, and the caller got a completion with no reasoning and no indication anything had gone wrong.Reproduced against the checkpoint's own
chat_template.jinjathroughChatTemplateProcessor::apply_with_kwargsbefore fixing:This was latent when filed and is live now. The issue said "None at runtime yet:
model_type: deepseek_v4is not loadable ... The gap becomes user-visible the moment #523 lands." #523 landed in PR #1455.The fix
thinking_mode_sentinel()reads the gate out of the template:thinking_mode, plus any alias from{% set <name> = thinking_mode ... %}.==, on either side."enabled".The fallback is the load-bearing part. A wrong sentinel is silent, so a derivation that guesses would be worse than the bug it replaces. Anything unreadable keeps today's behaviour.
The
default(...)argument is deliberately not a candidate. It is the template's OFF value ('chat'here), so injecting it would be worse than injecting nothing.#811's precedence is unchanged: an explicit client-supplied
thinking_modealways wins, and nothing is injected when thinking is disabled.Why the existing tests did not catch it
THINKING_MODE_LIKE_TEMPLATEcomparesthinking_modedirectly against"enabled", which is exactly the convention #811 was written for, andwants_thinking_mode_alias()is only a substring scan for the identifier, so it returnstruefor both conventions while the suite could only see one. The gap was not that the mechanism was untested; it was that the test and the implementation shared an assumption.Test plan
issue_819_deepseek_v4_gate_opens_from_a_derived_sentinel: the aliased shape now opens<think>, and the derived sentinel is asserted to be"thinking"rather than just checking the render.issue_819_real_checkpoint_template_opens_the_thinking_branch: same againstmodels/deepseek-v4-flash-4bit/chat_template.jinjaitself, so the inline shape cannot drift from the file it stands in for. Skips when the checkpoint is absent. Asserts the derived render equals the explicit-workaround render.thinking_mode_sentinel_falls_back_when_the_gate_is_not_readable: no mention, mentioned but never compared, two different gate literals, the feat(server): derive thinking_mode chat-template kwarg from template #811 convention unchanged, the same literal compared twice, reversed comparison order, a longer identifier ending in the variable's name, and thedefault()OFF value never chosen.derived_sentinel_keeps_811_precedence: explicit kwarg overrides the derived value; nothing injected when thinking is off.cargo test --lib server::: 2752 passed, 0 failed.reasoning_stream: 23 passed.cargo clippy --lib --tests -- -D warnings: exit 0.cargo fmt --check: clean.Both reproduction tests were written to assert the broken behaviour first and confirmed failing against the fix before their assertions were flipped, so they are known to discriminate rather than merely pass.
Closes #819