Skip to content

Studio: Forward thinking controls and return reasoning on /v1/messages and /v1/responses - #9107

Merged
danielhanchen merged 32 commits into
unslothai:mainfrom
NilayYadav:anthropic-thinking-passthrough
Aug 20, 2026
Merged

danielhanchen merged 32 commits into
unslothai:mainfrom
NilayYadav:anthropic-thinking-passthrough

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

Local models reasoning was invisible over Studio's Anthropic- and OpenAI-compatible endpoints: thinking / reasoning request fields were parsed and silently dropped, and llama-server's reasoning_content was never relayed back so clients like Claude Code and Codex could neither turn thinking on nor see the trace.

Changes:

  • /v1/messages: accept Anthropic's thinking config (plus x-unsloth enable_thinking / reasoning_effort / preserve_thinking) and forward them to llama-server as chat_template_kwargs
  • Treat unknown thinking.type values (adaptive tiers) as "enabled" instead of 400ing; accept and ignore budget_tokens
  • Emit reasoning_content as Anthropic thinking blocks (streaming SSE + non-streaming)
  • /v1/responses: forward reasoning.effort and return reasoning_content as reasoning output items (streaming + non-streaming)
  • Accept replayed thinking / redacted_thinking blocks in /v1/messages input so tool-use turns don't 422 on turn 2

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

💡 Codex Review

"content_block": {"type": "thinking", "thinking": ""},

P2 Badge Include signatures in streamed thinking block starts

When reasoning is streamed, both _open_thinking_block() implementations announce a thinking content block without the required signature field. The repository's Anthropic stream fixtures use {"type":"thinking","thinking":"","signature":""}, and the non-streaming response model likewise declares signature, so schema-validating clients can reject the streaming event before consuming its thinking_deltas. Include the empty signature placeholder in both streamed block-start payloads.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

💡 Codex Review

"content_block": {"type": "thinking", "thinking": ""},

P1 Badge Add signatures to streamed thinking block starts

When either streaming path emits reasoning, the content_block_start omits the signature field required by the Anthropic thinking-block shape, even though the non-streaming response model and existing translation fixtures explicitly include it. Strict Anthropic stream decoders can therefore reject the block before processing any thinking_delta; include a signature field, even if empty, in both this emitter and AnthropicPassthroughEmitter._open_thinking_block().


preserve_thinking: Optional[bool] = None

P2 Badge Preserve replayed thinking when requested

When an Anthropic client replays a returned thinking block and sets preserve_thinking=true, anthropic_messages_to_openai() still discards every assistant thinking block and retains only text and tool calls before this flag reaches the backend. Consequently templates such as Qwen3.6 never receive the historical <think> content, so the newly exposed control cannot perform its advertised function; the conversion needs to retain typed thinking when preservation is requested.


https://github.com/unslothai/unsloth/blob/2cd0678fb7ffcb6cf4d9012173e8e9f6c394d041/studio/backend/routes/inference.py#L2962-L2963
P2 Badge Match parsing to the template's effective reasoning mode

When a caller sends only reasoning_effort="none" to a default-on, enable_thinking-style template, this disables response parsing, but LlamaCppBackend._request_reasoning_kwargs() ignores reasoning_effort for that style unless enable_thinking is also supplied. Generation therefore still emits <think>...</think>, while both plain and tool response paths now relay the tags and reasoning as ordinary text. Determine parsing and forwarding from the same effective template controls, or map this sentinel to enable_thinking=false for these models.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

All three addressed in 963131a: (1) thinking content_block_start now carries an empty signature field in both stream emitters; (2) anthropic_messages_to_openai() keeps replayed assistant thinking blocks as reasoning_content when preserve_thinking=true (dropped otherwise, redacted_thinking always dropped); (3) _anthropic_reasoning_args() now maps an effort-only request onto enable_thinking exactly like /v1/responses (none=off, named level=on) and the think-markup parsing gate reads those same effective args, so generation and parsing can no longer disagree. Tests added for each.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 20, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

preserve_thinking is three valued: omitted means use the template the
model was launched with, and only an explicit value overrides it. Coercing
it with bool() made an omitted field indistinguishable from an explicit
false, so on a model whose launch template sets preserve_thinking_default
the request stripped reasoning_content from every replayed turn while
still leaving the server in preserve mode. The template was told to
preserve history that had already been deleted.

Resolve the effective value once and use it for both conversion and
counting so the two cannot drift.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 20, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 479812185a

ℹ️ 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".

pre-commit-ci Bot and others added 2 commits August 20, 2026 14:00
The request model states the x-unsloth reasoning controls win over
thinking when both are present, and resolved_enable_thinking promises the
same, but it consults only the boolean. Reading it first made the
effort-to-boolean mapping dead whenever thinking was supplied, so on a
plain enable_thinking template a request asking for thinking enabled with
reasoning_effort none had the effort dropped and the model thought
anyway, and the inverse stayed off. The effort-dial templates already
honoured it, so one request meant three different things.

llama.cpp resolves this itself, but only for a top-level reasoning_effort
body field, and the passthrough payload forwards chat_template_kwargs
only, so that never fires here.
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@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: 003d63b80c

ℹ️ 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".

enable_thinking = payload.resolved_enable_thinking()
return {
"enable_thinking": enable_thinking,
"reasoning_effort": reasoning_effort,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Drop contradictory effort when enable_thinking is explicit

When a caller supplies both x-unsloth controls, this returns the explicit boolean and the contradictory effort together even though the new precedence test documents the boolean as authoritative. The downstream LlamaCppBackend._request_reasoning_kwargs at studio/backend/core/inference/llama_cpp.py:5125-5131 prioritizes a recognized effort for effort-dial models, so enable_thinking=false, reasoning_effort="high" becomes high effort on gpt-oss while the same request disables Qwen-style templates; the inverse pair likewise selects none. Normalize or discard reasoning_effort when enable_thinking is explicitly present so the request has consistent behavior across model families.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not changing this. The premise is a misread of what the model documents. The comment above the x-unsloth group says These win over thinking when both are present, which is cross-group, the pair versus the native block; it says nothing about the two x-unsloth controls relative to each other. resolved_enable_thinking is documented the same way, preferring the x-unsloth field over thinking. Worth noting the authors did document independence where they meant it: preserve_thinking is explicitly Independent of enable_thinking / reasoning_effort. And the test cited as documenting the boolean as authoritative asserts only on the resolved boolean, never that reasoning_effort is dropped and never on template kwargs; it exists so an effort-only request can still drive a boolean-only template. So intra-group precedence is undefined, which makes this a design question rather than a bug. The family split reproduces, but it is a property of the families, not of this resolution. gpt-oss already ignores the boolean by design, and its published chat template contains no enable_thinking at all while Harmony defines only low, medium and high with no none, so enable_thinking false never means off there whatever we do. The split also predates this PR: the downstream branch is unchanged from the merge base and already governs chat completions on main, so this PR gives messages and responses the semantics the OpenAI endpoint already had. Both suggested resolutions regress well-formed input. Studio's own frontend sends both controls together and agreeing, because each family reads a different key. Discarding the effort when the boolean is explicit strips the effort level from every GLM turn the UI sends, and normalizing the effort to none when the boolean is false makes llama-server erase the kwarg so gpt-oss falls back to its medium template default, which is more reasoning than the low it gets today while claiming to disable. llama.cpp itself defines no precedence and treats the two as orthogonal, with only reasoning_effort none having a cross-effect. If you want intra-group precedence to become defined, that is an API decision to document in the request model first.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 003d63b80c

ℹ️ 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".

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.

4 participants