Skip to content

Studio: fit reasoning_effort to a template's enforced allowlist - #16527

Merged
oobabooga merged 3 commits into
unslothai:mainfrom
Lyxot:fix/fit-reasoning-effort-allowlist
Nov 4, 2026
Merged

oobabooga merged 3 commits into
unslothai:mainfrom
Lyxot:fix/fit-reasoning-effort-allowlist

Conversation

@Lyxot

@Lyxot Lyxot commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Follow-up to #16502. That PR publishes the reasoning levels a template enforces with a raise_exception guard. For Mistral Small 4 and Mistral Medium 3.5 those levels are none and high, so the chat UI now moves Low or Medium to High. Other paths can still send a level the template rejects, and the render then fails on every request:

  • API requests with minimal, low, medium, xhigh or max. OpenAI-compatible clients that default to medium fail against these models. On GGUF the error now quotes the template; on safetensors and MLX it is still "An internal error occurred".
  • A GGUF launch with reasoning off sends low as the server default, which breaks every request that does not name a level.
  • xhigh and max on GGUF fall through and send no level, so the template's default (none) silently turns thinking off.

Fix

_fit_reasoning_effort_to_template maps a level the template's enforced allowlist rejects to the nearest one it accepts. It uses the same clamp as the UI (_clamp_reasoning_effort_to_levels, mirrored in provider-capabilities.ts). Thinking off maps to none when the template offers it. It is applied in:

  • LlamaCppBackend._request_reasoning_kwargs (per-request kwargs for llama-server)
  • LlamaCppBackend._reasoning_kwargs (launch default)
  • apply_chat_template_for_generation (the shared transformers and MLX renderer)

The template used is the active override if one is set, otherwise the embedded template. Templates without such a guard are untouched, including gpt-oss and every family whose levels come from scanning. This follows the existing precedents: the enable_thinking_effort style already clamps requests to the template's levels, and Muse Spark coerces into its raising template's accepted set.

Tradeoff: an explicit API level is substituted silently rather than rejected, which is how the enable_thinking_effort clamp already treats it. The transformers/MLX renderer imports core.inference.llama_cpp lazily on its first render with an effort.

Validation

Rendered the real chat_template.jinja of Mistral Small 4 (also the template embedded in the GGUF), Mistral Medium 3.5 and gpt-oss with every combination of enable_thinking and effort, plus both launch defaults:

before: Mistral Small 4 fails for minimal/low/medium/xhigh/max requests and for the reasoning-off launch
after:  0 failures on all three templates; gpt-oss kwargs unchanged
pytest studio/backend/tests/test_reasoning_effort_allowlist.py studio/backend/tests/test_muse_spark.py \
  studio/backend/tests/test_deepseek_v4_thinking_effort.py studio/backend/tests/test_kimi_k3_reasoning_defaults.py \
  studio/backend/tests/test_chat_template_continuation.py studio/backend/tests/test_launch_reasoning_flag.py \
  studio/backend/tests/test_thinking_parameter.py
# 245 passed

Each new test was mutation-checked: reverting each of the four call sites (request, launch, override preference, transformers/MLX renderer) or the thinking-off mapping makes it fail.

An API request or a reasoning-off launch could still send a level the template's raise_exception guard rejects (Mistral Small 4 accepts only none and high), failing every render. llama-server requests, its launch default, and the shared transformers/MLX renderer now map such a level to the nearest accepted one, and thinking off to none when the template offers it.
@Lyxot

Lyxot commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T13:38:11.465287Z 7ad5012 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6bd340afb2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Lyxot

Lyxot commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 6bd340afb2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Callers that borrow the reasoning-kwargs methods unbound on lightweight backend stand-ins have no _rendered_chat_template method, so the lookup raised AttributeError there.
@Lyxot

Lyxot commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@Lyxot

Lyxot commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 9e54d42e4d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Lyxot

Lyxot commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

Review and CI summary for 9e54d42

  • Codex: Didn't find any major issues on 9e54d42. The single earlier finding, about diverging allowlists between default and tool_use templates, was declined with reasoning in its thread and resolved.

  • CI: upstream runners were queued, so CI ran on a staging copy with the same head and base (Lyxot/unsloth#216, now closed). 50 jobs passed and 4 skipped. The 5 failures are outside this change:

    • a FLUX.1-dev download needing network access
    • the Windows file-encoding lint on test_multi_lora_cold_compile_cpu.py
    • the raw px text lint on composer-settings.tsx, which already fails on main
    • parallel-isolation and PowerShell test lints
    • the no-torch unsloth / unsloth-zoo pairing

    None of the failing tests touch the reasoning or template code.

  • Changed during this workflow: 9e54d42 turns _rendered_chat_template from a method into a module-level function. The first staging run showed that tests calling LlamaCppBackend._request_reasoning_kwargs on stand-in backends raised AttributeError. test_openai_auto_switch.py, test_sampling_resolution.py and test_reasoning_effort_allowlist.py pass with it.

danielhanchen added a commit that referenced this pull request Nov 3, 2026
)

## Summary

`Repo tests (CPU, studio-install)` is still red on main after #16532.
That PR fixed the shared no-torch harness in
`test_no_torch_filtering.py`. A second test class has the same problem:
it runs `install_python_stack()` in no-torch mode with `subprocess.run`
mocked. It failed on #16534 and #16527, both of which ran on top of
#16532:

```
tests/studio/install/test_rocm_support.py::TestProgressStepCountMatchesTotal::test_progress_reaches_total_without_torch[linux-False]
tests/studio/install/test_rocm_support.py::TestProgressStepCountMatchesTotal::test_progress_reaches_total_without_torch[windows-False]
tests/studio/install/test_rocm_support.py::TestProgressStepCountMatchesTotal::test_progress_reaches_total_without_torch[macos_arm-False]
E   RuntimeError: Could not resolve a compatible unsloth / unsloth-zoo pair for no-torch mode
```

The cause is the same as in #16532. Since #16157, a no-torch install
resolves a compatible Unsloth / Zoo pair from wheel metadata, and it
refuses only when the feed has no such pair. That is right for users,
since such an install would fail anyway. `_run_stack` mocks
`subprocess.run` to return nothing, so the probe finds no metadata and
the full-pass (`SKIP_STUDIO_BASE=0`) cases stop before they reach the
progress count they check.

## Change

`tests/studio/install/test_rocm_support.py`,
`TestProgressStepCountMatchesTotal._run_stack` only: `_no_torch_pair`
returns a compatible pair, which models a feed that serves one. This is
the same model as #16532.

`NO_TORCH` and the new patch share one `patch.multiple`. That `with`
statement already holds 20 context managers, which is Python's limit on
nested blocks: a 21st `patch.object` makes the whole file fail to import
with `SyntaxError: too many statically nested blocks`. The patched
values are otherwise unchanged.

## Testing

`tests/studio/install/test_rocm_support.py`:
- main: 3 failed, 686 passed (the three cases above);
- this branch: 689 passed, 2 skipped.

I also ran `tests/studio/install` plus `tests/python` on this branch.
"Could not resolve a compatible" no longer appears anywhere. 22 other
tests fail locally, in `test_dependency_pass_skips.py` and
`test_e2e_no_torch_sandbox.py`, and they fail the same way on main here.
CI passes them in the same shard, so the cause is this host's
environment.
@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 7ad501224f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member

Reached Codex review convergence at oobabooga#453 ("Didn't find any major issues" on 7ad5012, the current head).

@oobabooga

Copy link
Copy Markdown
Member

#16502 made the chat UI clamp to the levels a template enforces, but API requests, the GGUF launch default and the transformers/MLX renderer still sent any level, so Mistral Small 4 and Mistral Medium 3.5 failed on low or medium, and xhigh or max silently turned thinking off. _fit_reasoning_effort_to_template now maps a rejected level to one the template accepts. For these models none turns thinking off rather than being a level below low, so every other level becoming high is right.

Rendering the real unsloth/Mistral-Small-4-119B-2603 template with thinking on, off and unset and every effort level, on main (ef65f8cafc) and this PR:

Path main PR
Per-request GGUF kwargs (24 cases) 12 fail, 2 more turn thinking off 0 fail
Launch default (8 cases) 7 fail 0 fail
Transformers/MLX renderer (24 cases) 15 fail 0 fail

CI is clear at 7ad501224f: 55 checks passed, and the one failure is the Local Agent Guides connection (pi, stable) job, cancelled while installing the agent CLI.

@oobabooga
oobabooga merged commit 5b219c9 into unslothai:main Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants