Thinking text doubled when server mirrors reasoning deltas into both `reasoning_content` and `reasoning`
Summary
When an OpenAI-compatible server mirrors each reasoning delta into both delta.reasoning_content and delta.reasoning (identical text in the same chunk), the openai provider adapter appends both copies. Result: the thinking chain is rendered word-by-word doubled in the TUI, and the doubled text is also written into the session log.
This is a client-side handling gap, not a server bug: many harness-compatible servers deliberately emit reasoning twice (once per field name) so that clients reading either field work. A client that consumes both fields silently corrupts the reasoning display for every such endpoint.
Environment
- Kimi Code CLI 2.0.1 (Windows 11)
- Custom model pointing to an OpenAI-compatible server on the LAN (mlx-vlm 0.7.0,
Server: mlx_vlm/0.7.0header)
[providers."local-qwen"]
type = "openai"
base_url = "http://192.168.50.243:1919"
api_key = "test"
[models."qwen3.8-flash-next-4bit"]
provider = "local-qwen"
model = "models/Qwen3.8-Flash-Next-4bit"
max_context_size = 262144
capabilities = [ "thinking", "image_in", "tool_use" ]
support_efforts = [ "low", "medium", "xhigh" ]
default_effort = "xhigh"
What it looks like
The thinking panel renders every token twice:
TheThe user user greeted greeted me me in in Chinese Chinese,, so so I I should should respond respond with with a a brief brief greeting greeting in in return return..
The final answer text (content) is unaffected — only the reasoning chain is doubled.
Evidence
Raw SSE captured directly from the server with curl — the same delta text arrives under two field names in every chunk:
data: {"choices":[{"index":0,"delta":{"role":"assistant","content":null,
"reasoning_content":"The","reasoning":"The", "...": "..."}}]}
data: {"choices":[{"index":0,"delta":{"role":"assistant","content":null,
"reasoning_content":" user","reasoning":" user", "...": "..."}}]}
Kimi Code then records two content.part think entries per token in wire.jsonl, one per field:
{"type":"content.part",...,"part":{"type":"think","think":" greeted","reasoningKey":"reasoning_content"}}
{"type":"content.part",...,"part":{"type":"think","think":" greeted","reasoningKey":"reasoning"}}
In one test turn: 40 parts with "reasoningKey":"reasoning_content" + 40 identical parts with "reasoningKey":"reasoning" — the accumulated reasoning string is exactly twice its natural length.
Workaround tried: reasoning_key
Setting reasoning_key = "reasoning_content" on the model entry does not prevent the doubling: after pinning it, the session log still contained both keys (40 + 40). reasoning_key appears to add a non-standard field name to auto-detection rather than restrict the adapter to one field.
Expected / suggested behavior
When a chunk carries identical text in both reasoning_content and reasoning, the text should be consumed once. Any of:
- Dedupe identical delta content between the two fields (e.g. prefer
reasoning_contentwhenreasoningrepeats it); or - Make
reasoning_keya hard pin — when set, read only that field; or - Treat "both fields present and equal in the same chunk" as one occurrence at the accumulate/normalize layer, before display and context storage.
Impact
- Thinking display is unreadable and twice as long.
- Doubled reasoning is written into session context; if thinking is kept in context (
thinking.keep), this also wastes tokens.
I've prepared a complete fix for this on my fork — posting it here first, per the contributing guide (waiting for a maintainer's /approve before opening a PR).
Proposed fix — branch fix/openai-mirrored-reasoning-dedupe on cunminF/kimi-code, full diff:
- Dedupe mirrored fields (option 1). Inbound stream parsing now dedupes reasoning fields that carry an identical string value within the same SSE chunk, keeping the first key in scan order (
reasoning_contentfirst by default). Fields with distinct values (e.g.reasoning_content: 'A'plusreasoning: 'B') are still all preserved, and empty-string reasoning parts behave exactly as before. reasoning_keyas a hard pin (option 2). A configuredreasoning_keyis now honored by the v2 stream parser: a string restricts inbound scanning to that single field.reasoning_detailsextraction still only runs when no explicit key is configured.reasoning_keyarray form. It also accepts a non-empty array of field names, customizing which fields are scanned and in what order. Outbound echo uses the observed key, falling back to the first array entry before any field has been observed; the defaults (reasoning_contentfirst, echo keyreasoning_content) are unchanged.
Also included in the diff: model config zod schemas accept string | string[] (non-empty array), klient contract schema updated for parity, config docs updated (en/zh), tests covering the above, and the corresponding changesets.
This was previously opened as #3930, which I've now closed to follow the issue-first flow. Once this issue is approved, I'll open a fresh PR (or a maintainer can reopen the previous one).
+1 — confirming the same bug on a production endpoint with some extra detail that may help the fix.
Another affected upstream: StepFun (api.stepfun.com). All of its thinking models mirror reasoning into both fields — verified with raw SSE capture on step-5-preview, step-3.5-flash and step-3.7-flash:
{"choices":[{"delta":{"role":"assistant","content":"","reasoning":"The","reasoning_content":"The"}}]}
Every chunk carries identical text in both fields, so every thinking model on this endpoint renders doubled. Non-mirroring endpoints are unaffected (e.g. a swe-2-max gateway returning only reasoning_content), which matches the reporter's analysis: the trigger is the mirroring, not any specific server.
Code path confirmed in the shipped desktop app 1.0.2 (agent-core 2.0.0). Extracted from app.asar, packages/agent-core-v2/src/human/llm/requester/bases/openai/reasoning-key.ts:
const KNOWN_REASONING_KEYS = ["reasoning_content", "reasoning_details", "reasoning"];
// in the chunk handler:
for (const reasoning of extractReasoningStrings(delta)) {
sink.onDelta({ type: "think", think: reasoning.value, reasoningKey: reasoning.key });
}
extractReasoningStrings pushes one entry per key present in the delta, and the downstream merge only coalesces adjacent parts with the same reasoningKey, so both copies survive to render.
Two notes for the fix:
- The model config's
reasoning_keydoes not gate inbound stream parsing — it only affectslowerMessageserialization of history. Settingreasoning_key = "reasoning_content"on the model does not avoid the doubling (verified on step-3.5-flash, which has it set). So the fix has to be in the inbound parse path, not in serialization. - There is a second symptom beyond display: the doubled reasoning is written back into the assistant history under both fields, so every follow-up request carries the reasoning twice — wasted tokens on every turn.
Repro: replaying the shipped extractReasoningStrings logic against both a direct and a deduped stream — 15 mirrored chunks produce 30 think parts direct vs. 16 deduped; CoT text goes from "TheThe user said user said..." to clean. I'm currently working around it with a local proxy that strips the mirrored field.
The proposed fix (dedupe identical values within a chunk, keep the first key in scan order) is the approach I'd expect too — +1. Happy to test the PR against the StepFun endpoint once it's up.