Repository navigation
feat(server): derive thinking_mode chat-template kwarg from template - #811
Merged
Merged
Conversation
Some HuggingFace chat templates gate reasoning on a thinking_mode string variable rather than the boolean enable_thinking mlxcel forwards by default (issue #775), so those templates silently took their non-thinking branch even when thinking was requested. Adds ChatTemplateProcessor::wants_thinking_mode_alias, a substring scan for the thinking_mode identifier in the template source, mirroring the existing wants_bare_thinking_alias detection for the DeepSeek-V3.2 bare-thinking idiom. build_template_context now injects thinking_mode: "enabled" whenever the template references the identifier and the resolved enable_thinking value is true, unless the caller already supplied an explicit thinking_mode kwarg (client value always wins). Nothing is injected when thinking is disabled, since affected templates default the variable themselves in that case. Adds unit tests covering: detection of the identifier across template shapes, injection when thinking is enabled, no injection when thinking is disabled (verified via a template that distinguishes "undefined" from any injected value), honoring the tokenizer-derived default_enable_thinking when no request kwarg is present, client-supplied thinking_mode overriding the derived alias, parity across apply_with_kwargs and apply_raw_with_kwargs, and a no-op guard for templates that never reference the identifier. Existing qwen3 and gemma4 template rendering tests are unchanged and pass. Validated with cargo test --release --features cuda --lib -- server::chat_template server::chat_template_kwargs (89 passed, 0 failed, 1 ignored) and cargo fmt --all -- --check.
Member
Author
Implementation Review SummaryIntent
Findings AddressedNone. Independent review plus a supplementary automated review pass found no actionable correctness, security, or maintainability issues. Remaining ItemsNone. Verification
Reviewer notes (non-blocking)
|
5 tasks
6 tasks done
inureyes
added a commit
that referenced
this pull request
Aug 31, 2026
…#1547) 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.
This was referenced Aug 31, 2026
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
…ablup#811) ## Summary Some HuggingFace chat templates gate reasoning on a thinking_mode string variable rather than the boolean enable_thinking mlxcel forwards by default, so those templates silently took their non-thinking branch even when thinking was requested. This adds detection of the thinking_mode identifier in the template source and injects thinking_mode: "enabled" into the template kwargs whenever thinking is enabled and the template references it, unless the client already supplied its own thinking_mode value. ## What changed - `src/server/chat_template.rs`: added `ChatTemplateProcessor::wants_thinking_mode_alias`, a substring scan for the `thinking_mode` identifier, mirroring the existing `wants_bare_thinking_alias` detection used for the DeepSeek-V3.2 bare-thinking idiom. - `src/server/chat_template.rs`: `build_template_context` now injects `thinking_mode: "enabled"` when the template references the identifier and the resolved `enable_thinking` value is true, and does nothing when thinking is disabled (affected templates default the variable themselves). An explicit client-supplied `thinking_mode` kwarg always wins. - `src/server/chat_template.rs`: added unit tests for identifier detection across template shapes, injection when thinking is enabled, no injection when thinking is disabled, honoring the tokenizer-derived default when no request kwarg is present, client override of the derived alias, parity across `apply_with_kwargs` and `apply_raw_with_kwargs`, and a no-op guard for templates that never reference the identifier. ## Test plan - [x] `cargo test --release --features cuda --lib -- server::chat_template server::chat_template_kwargs` (89 passed, 0 failed, 1 ignored) - [x] `cargo fmt --all -- --check` - [x] Existing qwen3 and gemma4 template rendering tests pass unchanged Closes lablup#775
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
Some HuggingFace chat templates gate reasoning on a thinking_mode string variable rather than the boolean enable_thinking mlxcel forwards by default, so those templates silently took their non-thinking branch even when thinking was requested. This adds detection of the thinking_mode identifier in the template source and injects thinking_mode: "enabled" into the template kwargs whenever thinking is enabled and the template references it, unless the client already supplied its own thinking_mode value.
What changed
src/server/chat_template.rs: addedChatTemplateProcessor::wants_thinking_mode_alias, a substring scan for thethinking_modeidentifier, mirroring the existingwants_bare_thinking_aliasdetection used for the DeepSeek-V3.2 bare-thinking idiom.src/server/chat_template.rs:build_template_contextnow injectsthinking_mode: "enabled"when the template references the identifier and the resolvedenable_thinkingvalue is true, and does nothing when thinking is disabled (affected templates default the variable themselves). An explicit client-suppliedthinking_modekwarg always wins.src/server/chat_template.rs: added unit tests for identifier detection across template shapes, injection when thinking is enabled, no injection when thinking is disabled, honoring the tokenizer-derived default when no request kwarg is present, client override of the derived alias, parity acrossapply_with_kwargsandapply_raw_with_kwargs, and a no-op guard for templates that never reference the identifier.Test plan
cargo test --release --features cuda --lib -- server::chat_template server::chat_template_kwargs(89 passed, 0 failed, 1 ignored)cargo fmt --all -- --checkCloses #775