Skip to content

fix(hf): forward string THINKING levels as reasoning_effort, not low_effort - #1639

Merged
planetf1 merged 2 commits into
generative-computing:mainfrom
planetf1:issue-1636
Sep 10, 2026
Merged

planetf1 merged 2 commits into
generative-computing:mainfrom
planetf1:issue-1636

Conversation

@planetf1

@planetf1 planetf1 commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

What this fixes

If you passed ModelOption.THINKING="low" to a LocalHFBackend session (e.g. Granite 4.2), it was silently ignored — the model reasoned at full length anyway, with no error or warning. In practice this could burn your whole token budget on reasoning and never produce an answer.

After this fix, THINKING="low" genuinely engages Granite's low-effort reasoning mode, producing a short reasoning trace and reaching an answer within the token budget. The same fix also makes THINKING="low"/"medium"/"high" work for openai/gpt-oss-20b and gpt-oss-120b on LocalHFBackend, which had the same gap.

Why it was broken

LocalHFBackend only forwarded THINKING to the chat template when it was a bool (True/False). A string level like "low" failed that check and was dropped before it ever reached the template.

How it's fixed

Granite 4.2's chat template computes its low-effort behaviour from a reasoning_effort variable: {%- set low_effort = reasoning_effort == "low" %}. The fix forwards a string THINKING value verbatim as reasoning_effort whenever the tokenizer's chat template declares that variable — the same mechanism the OpenAI backend already uses (_map_thinking_option in openai.py). This also preserves the existing precedence rule: an explicit ModelOption.THINKING always wins over a raw reasoning_effort key set some other way.

The ModelOption.THINKING docstring is also corrected — it previously said HuggingFace only supports boolean values, which is no longer true.

Verification

  • Live end-to-end: ran the issue's exact repro against ibm-granite/granite-4.2-3b. Before: THINKING="low" produced full-length reasoning and exhausted a 400-token budget with no answer. After: short reasoning, correct answer, well within budget.
  • Integration test against the real ibm-granite/granite-4.2-3b tokenizer/chat-template, confirming the rendered prompt text actually changes ({reasoning effort: low} appears/disappears).
  • Unit tests covering: string levels forward correctly when the template declares reasoning_effort, are a no-op when it doesn't, an explicit THINKING sentinel wins over a conflicting raw reasoning_effort key, and bool THINKING values are unaffected.
  • uv run pytest test/backends/test_huggingface_filter_options.py — 41 passed, 3 skipped (pre-existing skips, unrelated model not cached locally).
  • Full suite (uv sync --all-extras --all-groups; uv run pytest test/ -m "not qualitative") — 3629+ passed, no failures from this change.
  • ruff format --check / ruff check / mypy — all clean.

Fixes #1636

Assisted-by: Claude Code

LocalHFBackend._filter_for_chat_template only set the chat template's
thinking variable when ModelOption.THINKING was a bool, so a string
level like "low" was silently dropped and Granite fell back to full
default thinking with no error.

Granite 4.2's chat template implements a separate boolean `low_effort`
variable, distinct from the bool-valued thinking vars (think/thinking/
enable_thinking), driven by a string reasoning-effort level. Mirror the
OpenAI backend's reasoning_effort handling: translate THINKING="low"
into low_effort=True when the template declares that variable. Other
string levels remain no-ops, matching Granite's template (no "high"
branch on the model side).

Verified end-to-end against ibm-granite/granite-4.2-3b: with
THINKING="low" the model now produces short reasoning and reaches an
answer, instead of exhausting the token budget in full-length
reasoning.

Fixes generative-computing#1636

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working label Sep 9, 2026
…effort

Code review found the previous fix targeted the wrong mechanism:
Granite 4.2's chat template derives its boolean `low_effort` variable
from `reasoning_effort == "low"` (chat_template.jinja), and gpt-oss's
HF chat template reads `reasoning_effort` directly. Forward the string
THINKING value verbatim as `reasoning_effort` when the template
declares that variable, instead of translating "low" into a
template-specific derived boolean — this is what actually mirrors the
OpenAI backend's reasoning_effort handling, covers gpt-oss for free,
and preserves the documented sentinel-wins precedence when a raw
reasoning_effort key is also present.

Also fixes the ModelOption.THINKING docstring in model_options.py,
which stated HF supports boolean values only — now false.

Verified end-to-end against ibm-granite/granite-4.2-3b: THINKING="low"
reaches a short answer within the token budget via the corrected
reasoning_effort path.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
@planetf1
planetf1 marked this pull request as ready for review September 9, 2026 11:25
@planetf1
planetf1 requested a review from a team as a code owner September 9, 2026 11:25
@psschwei

psschwei commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

one nit: there's a bit of drift between what the PR fixes vs. what's specified in the title. Maybe retitle similar to the 2nd commit?

@planetf1 planetf1 changed the title fix(hf): forward THINKING="low" as low_effort=True to chat template fix(hf): forward string THINKING levels as reasoning_effort, not low_effort Sep 10, 2026
@planetf1

Copy link
Copy Markdown
Contributor Author

Good catch — retitled to match the second commit. The title was left over from the first pass (low_effort=True), which review found targeted the wrong template variable; the actual fix forwards the string level verbatim as reasoning_effort. Body already described the current mechanism correctly.

@planetf1
planetf1 added this pull request to the merge queue Sep 10, 2026
Merged via the queue into generative-computing:main with commit ad41c60 Sep 10, 2026
17 checks passed
@planetf1
planetf1 deleted the issue-1636 branch September 10, 2026 13:29
planetf1 added a commit that referenced this pull request Sep 10, 2026
Documents ModelOption.THINKING string levels and result.thinking as
supported on LocalHFBackend too, matching the state after #1639 (HF string
forwarding fix) and #1616 (HF <think> tag parsing) merge. Both are still
open — this docs PR should merge after them, or the LocalHFBackend row will
describe behaviour that isn't live yet.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
planetf1 added a commit that referenced this pull request Sep 10, 2026
- Qualify the OpenAIBackend/LiteLLM `False` cell: real OpenAI reasoning
  models and non-Ollama LiteLLM targets never receive `reasoning_effort=
  "none"` (openai.py's server-type guard, litellm.py's ollama-prefix guard),
  so `False` does not actually disable thinking there.
- Add an inline callout marking the LocalHFBackend string-forwarding and
  result.thinking claims as contingent on #1639/#1616 merging, instead of
  only noting it in the PR description/comment.
- Correct "same mechanism as the OpenAI backend" — HF forwards through a
  gated chat-template variable, OpenAI sends an ungated top-level param.
- Note the runtime-forwarding dependency for the cross-backend Granite
  claim, add a `> Full example:` link, fix the unresolvable bare "#1617"
  reference, add a non-determinism note to the new code block, drop the
  unnecessary `qualitative` marker, and fix US-English spelling in new
  content per CONTRIBUTING_DOCS.md.
- Add cheap assertions to docs/examples/thinking_mode.py pinning the
  documented per-arm behaviour; reran live against granite4.2:3b, all pass.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
planetf1 added a commit that referenced this pull request Sep 15, 2026
Documents ModelOption.THINKING string levels and result.thinking as
supported on LocalHFBackend too, matching the state after #1639 (HF string
forwarding fix) and #1616 (HF <think> tag parsing) merge. Both are still
open — this docs PR should merge after them, or the LocalHFBackend row will
describe behaviour that isn't live yet.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
planetf1 added a commit that referenced this pull request Sep 15, 2026
- Qualify the OpenAIBackend/LiteLLM `False` cell: real OpenAI reasoning
  models and non-Ollama LiteLLM targets never receive `reasoning_effort=
  "none"` (openai.py's server-type guard, litellm.py's ollama-prefix guard),
  so `False` does not actually disable thinking there.
- Add an inline callout marking the LocalHFBackend string-forwarding and
  result.thinking claims as contingent on #1639/#1616 merging, instead of
  only noting it in the PR description/comment.
- Correct "same mechanism as the OpenAI backend" — HF forwards through a
  gated chat-template variable, OpenAI sends an ungated top-level param.
- Note the runtime-forwarding dependency for the cross-backend Granite
  claim, add a `> Full example:` link, fix the unresolvable bare "#1617"
  reference, add a non-determinism note to the new code block, drop the
  unnecessary `qualitative` marker, and fix US-English spelling in new
  content per CONTRIBUTING_DOCS.md.
- Add cheap assertions to docs/examples/thinking_mode.py pinning the
  documented per-arm behaviour; reran live against granite4.2:3b, all pass.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
planetf1 added a commit that referenced this pull request Sep 15, 2026
#1639 (HF string-forwarding) merged 2026-09-10; only #1616
(result.thinking parsing) remains open. Also note that #1616's
<think>-tag split is skipped for streaming (m serve) calls, per
its own PR description.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
planetf1 added a commit that referenced this pull request Sep 16, 2026
#1616 and #1639 have both merged, so LocalHFBackend's result.thinking
split is now current-main behaviour, not a future dependency.

Assisted-by: Claude Code
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Signed-off-by: Nigel Jones <[email protected]>
akihikokuroda pushed a commit to akihikokuroda/mellea that referenced this pull request Sep 16, 2026
…-computing#1645)

* docs: document ModelOption.THINKING behaviour for Granite and add example

Adds a per-backend reasoning/thinking-mode reference table (native Ollama,
OpenAI/LiteLLM, HF) to configure-model-options.md, including Granite 4.2's
"low"-only effort distinction and its default-on behaviour, and cross-links
it from openai.md's existing Qwen3/vLLM extra_body section as the preferred
portable entry point. Adds docs/examples/thinking_mode.py demonstrating
THINKING=True/False/"low" against a local Ollama Granite model, verified
live against granite4.2:3b.

Fixes generative-computing#1638

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: describe THINKING doc as post-generative-computing#1639/generative-computing#1616 target state

Documents ModelOption.THINKING string levels and result.thinking as
supported on LocalHFBackend too, matching the state after generative-computing#1639 (HF string
forwarding fix) and generative-computing#1616 (HF <think> tag parsing) merge. Both are still
open — this docs PR should merge after them, or the LocalHFBackend row will
describe behaviour that isn't live yet.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: address code-review findings on THINKING docs

- Qualify the OpenAIBackend/LiteLLM `False` cell: real OpenAI reasoning
  models and non-Ollama LiteLLM targets never receive `reasoning_effort=
  "none"` (openai.py's server-type guard, litellm.py's ollama-prefix guard),
  so `False` does not actually disable thinking there.
- Add an inline callout marking the LocalHFBackend string-forwarding and
  result.thinking claims as contingent on generative-computing#1639/generative-computing#1616 merging, instead of
  only noting it in the PR description/comment.
- Correct "same mechanism as the OpenAI backend" — HF forwards through a
  gated chat-template variable, OpenAI sends an ungated top-level param.
- Note the runtime-forwarding dependency for the cross-backend Granite
  claim, add a `> Full example:` link, fix the unresolvable bare "generative-computing#1617"
  reference, add a non-determinism note to the new code block, drop the
  unnecessary `qualitative` marker, and fix US-English spelling in new
  content per CONTRIBUTING_DOCS.md.
- Add cheap assertions to docs/examples/thinking_mode.py pinning the
  documented per-arm behaviour; reran live against granite4.2:3b, all pass.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: fix stale THINKING dependency note now that generative-computing#1639 merged

generative-computing#1639 (HF string-forwarding) merged 2026-09-10; only generative-computing#1616
(result.thinking parsing) remains open. Also note that generative-computing#1616's
<think>-tag split is skipped for streaming (m serve) calls, per
its own PR description.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: drop merged-PR references from THINKING doc

generative-computing#1616 and generative-computing#1639 have both merged, so LocalHFBackend's result.thinking
split is now current-main behaviour, not a future dependency.

Assisted-by: Claude Code
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Signed-off-by: Nigel Jones <[email protected]>

---------

Signed-off-by: Nigel Jones <[email protected]>
Co-authored-by: Claude Sonnet 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(hf): ModelOption.THINKING string values (e.g. "low") are silently dropped, falling back to full thinking

2 participants