Skip to content

Studio: offer only the reasoning levels a template allows and show its raise message - #16502

Merged
Lyxot merged 3 commits into
unslothai:mainfrom
oobabooga:worktree-mistral4-reasoning-effort
Nov 3, 2026
Merged

Lyxot merged 3 commits into
unslothai:mainfrom
oobabooga:worktree-mistral4-reasoning-effort

Conversation

@oobabooga

Copy link
Copy Markdown
Member

Mistral Small 4 GGUFs fail in Studio when thinking is set to Low or Medium, showing only "An internal error occurred". The model's chat template accepts only none and high. Reported on r/unsloth: https://www.reddit.com/r/unsloth/comments/1xksg60/

Cause

Studio already detects thinking levels and adjusts unsupported selections. For templates using reasoning_effort alone, detection scans for quoted level names and only trusts the result when it contains both low and high. This avoids mistaking a default for the complete list: GPT-OSS's template only quotes medium, but supports Low and High too.

That check discards Mistral's none/high list. The UI then falls back to Low / Medium / High, so Medium looks supported and the existing adjustment leaves it unchanged.

Fix

This PR first looks for an explicit validation block: if reasoning_effort not in [...] followed by raise_exception(...). It uses that allowed list when present and keeps the existing detection otherwise. Mistral now reports ['none', 'high'], so the existing UI adjustment moves Low or Medium to High and the menu offers only High. No frontend changes are needed; adding Off remains separate.

It also surfaces the template's error message by decoding llama-server's JSON before extracting it, preserving embedded quotes, and checking against the active template override when one is set. Only text found in the template is shown. An API request explicitly sending low still fails, but now explains that the accepted values are "none" and "high".

Verification

  • 410 focused backend tests pass, including the new detection and error-message cases.
  • Main/branch comparison using a small Gemma GGUF with the real Mistral template, both embedded and as an override: the effort selected by the UI changes from medium to high, the request completes, and an explicit low request returns the template's explanation. The full Mistral model was not used for this test.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 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 ⚠️ Failed 2026-11-02T22:44:26.086235Z
We couldn’t complete this review. Comment @codex review to try again.
da1c5df PR opened
ℹ️ 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.

@oobabooga

Copy link
Copy Markdown
Member Author

Reached Codex review convergence at oobabooga#451 on b9fded5.

@Lyxot Lyxot self-assigned this Nov 3, 2026

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thanks!

@Lyxot
Lyxot merged commit 139ba23 into unslothai:main Nov 3, 2026
oobabooga added a commit that referenced this pull request Nov 4, 2026
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:

```text
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
```

```bash
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.

---------

Co-authored-by: oobabooga <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants