Skip to content

fix(server): derive the thinking_mode sentinel from the chat template - #1547

Merged
inureyes merged 1 commit into
mainfrom
fix/issue-819-thinking-mode-sentinel
Aug 31, 2026
Merged

inureyes merged 1 commit into
mainfrom
fix/issue-819-thinking-mode-sentinel

Conversation

@inureyes

Copy link
Copy Markdown
Member

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_mode sentinel 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:

{%- set mode = thinking_mode|default('chat') -%}
{%- if loop.last and mode == 'thinking' -%}<think>{%- else -%}</think>{%- endif -%}

"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.jinja through ChatTemplateProcessor::apply_with_kwargs before fixing:

[819] enable_thinking=true   tail: ">hi<|Assistant|></think>"   <- non-thinking branch
[819] thinking_mode=thinking tail: "|>hi<|Assistant|><think>"   <- the workaround

This was latent when filed and is live now. The issue said "None at runtime yet: model_type: deepseek_v4 is 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:

  1. Collect identifiers carrying the value: thinking_mode, plus any alias from {% set <name> = thinking_mode ... %}.
  2. Collect the string literals those identifiers are compared against with ==, on either side.
  3. Exactly one distinct literal, inject it. Zero or several, fall back to "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_mode always wins, and nothing is injected when thinking is disabled.

Why the existing tests did not catch it

THINKING_MODE_LIKE_TEMPLATE compares thinking_mode directly against "enabled", which is exactly the convention #811 was written for, and wants_thinking_mode_alias() is only a substring scan for the identifier, so it returns true for 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 against models/deepseek-v4-flash-4bit/chat_template.jinja itself, 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 the default() 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

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.
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:low Low priority status:review Under review labels Aug 31, 2026
@inureyes
inureyes merged commit 030a649 into main Aug 31, 2026
11 checks passed
@inureyes
inureyes deleted the fix/issue-819-thinking-mode-sentinel branch August 31, 2026 07:50
@inureyes inureyes mentioned this pull request Aug 31, 2026
4 tasks done
inureyes added a commit that referenced this pull request Aug 31, 2026
@inureyes inureyes added status:done Completed and removed status:review Under review labels Sep 6, 2026
@inureyes inureyes added this to the 0.7 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:low Low priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(server): DeepSeek-V4 thinking requests silently render the non-thinking branch (thinking_mode sentinel mismatch)

1 participant