Skip to content

feat(dialect): round-trip Chat reasoning_content across dialects - #316

Merged
fylorn merged 2 commits into
mainfrom
feat/chat-reasoning-roundtrip
Oct 10, 2026
Merged

fylorn merged 2 commits into
mainfrom
feat/chat-reasoning-roundtrip

Conversation

@fylorn

@fylorn fylorn commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

What

Thinking decoded from a Chat upstream (reasoning_content, or reasoning as OpenRouter and vLLM spell it) now carries a signature of a new vendor, Vendor::Chat (empty value), so it is vendor-tagged like Anthropic / OpenAI / Google thinking.

  • Carried across dialects: for Anthropic / Responses / Gemini clients the tag travels in the client's signature field with the existing tw1.<tag>. scheme, as tw1.ch. (c alone is the compaction item's tag). It is read back on the next turn.
  • Written back to Chat upstreams: an assistant message's Thinking with the Chat vendor is encoded as reasoning_content on that message. Thinking signed by any other vendor, or unsigned, is still dropped for Chat upstreams and recorded as before — OpenAI's own Chat API rejects unknown message fields.
  • Chat stream parser closes a thinking block with that signature; the non-stream decoders (upstream response and request history) read reasoning as well as reasoning_content, matching the stream decoder.
  • Chat encoder merges consecutive assistant messages before writing. A Responses client sends one item per message, so reasoning, text and tool call of one turn arrived as three assistant messages and the reasoning-only one was discarded. DeepSeek also requires reasoning_content on the same assistant message as tool_calls. User messages are not merged (a mid-conversation system turn folded into a user message stays in place; the gateway test for that still passes).
  • Chat → Chat passthrough is untouched (strip_carried still returns None for Chat clients); on the other dialects tw1.ch. is stripped on passthrough like every carried value, which is pinned by the existing strip_carried test.

Result: a Claude Code (Anthropic) or Codex (Responses) conversation with tools against DeepSeek keeps its reasoning across the tool-calling turns.

Tests

crates/tw-dialect/tests/matrix.rs now covers thinking for every client × upstream pair:

  • history signed by each of the four vendors reaches only the upstream that signed it (and the drop is recorded for the others; the Chat client can only carry Chat reasoning),
  • whole, streamed and collected responses bring the thinking text and the signature back to every client (Chat clients get the text).

Unit tests: Chat request/response/stream decoding tags the vendor, reasoning is read, other vendors' thinking is not written to a Chat upstream.

Verified

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo test -p tw-dialect, cargo test -p tw-gateway, and cargo test --workspace (local proxy unset)

New messages

None. No control API / protocol change.

🤖 Generated with Claude Code

fylorn and others added 2 commits October 10, 2026 18:36
Thinking decoded from a Chat upstream (`reasoning_content`, or
`reasoning` as OpenRouter and vLLM call it) used to carry no signature,
so it was dropped when the next turn was encoded for a Chat upstream.
DeepSeek requires the reasoning of a tool-calling turn to be sent back
on the following requests of that turn, so a Claude Code or Codex
conversation with tools against DeepSeek lost its reasoning every turn.

Give such thinking a signature of a new vendor, `Vendor::Chat`, with an
empty value. It is carried in the client's signature field like the
other vendors (`tw1.ch.`), read back on the next turn, and only then
written as `reasoning_content` for a Chat upstream. Thinking signed by
any other vendor, or unsigned, is still dropped for Chat upstreams:
OpenAI's own Chat API rejects unknown message fields.

The Chat encoder now merges consecutive assistant messages first. A
Responses client sends one item per message, so the reasoning, the text
and the tool call of one turn arrived as three assistant messages, and
the reasoning-only one was discarded; DeepSeek also wants
`reasoning_content` on the same assistant message as `tool_calls`. User
messages are left alone so a mid-conversation system turn folded into a
user message stays where it was.

The Chat stream parser closes a thinking block with that signature, and
the non-stream decoders read `reasoning` as well, matching the stream
decoder. Chat -> Chat passthrough is untouched; `strip_carried` removes
the carried value on the other dialects as it does for any `tw1.` value.

The cross-dialect matrix now covers thinking: history signed by each
vendor reaches only the upstream that signed it, and whole, streamed and
collected responses bring the text and signature back to every client.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@fylorn
fylorn merged commit 9fc74ee into main Oct 10, 2026
5 checks passed
@fylorn
fylorn deleted the feat/chat-reasoning-roundtrip branch October 10, 2026 10:58
@fylorn fylorn mentioned this pull request Oct 10, 2026
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.

1 participant