From 56f3dd2441ba5c0db7c74d5b934e90b462370e2f Mon Sep 17 00:00:00 2001 From: JamesYuuu <95520900+JamesYuuu@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:05:06 +0800 Subject: [PATCH 1/2] fix: read the delta.reasoning alias in the OpenAI adapter (#7432) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ollama Cloud and Magpie stream the thinking field as `delta.reasoning`; DeepSeek and llama.cpp stream `delta.reasoning_content`. adapter-openai.ts read `reasoning_content` only, so every reasoning-only chunk of an alias upstream matched no branch of the dispatch ladder (dual-field, reasoning, text) and was dropped. The one frame that also carried `role:"assistant"` still took the meta fallback, which is exactly why the client kept the first token of the whole thinking block and nothing after it. Read the field through one alias table (reasoning_content, reasoning, reasoning_text — the same set and order pi-ai's openai-completions reader uses) and keep the wire canonical: an alias frame is rewritten to `reasoning_content` before it reaches the client, because an upstream only reads `reasoning_content` when a replayed assistant message comes back — a replayed `reasoning` field is silently discarded (verified against Magpie 0.1.4623 / ollama-cloud). Both raw paths of the withheld-prose fallbacks (#1881) read the alias too, so a proxy-only tool frame no longer swallows its sibling prose. Regression: tests/issue7432-reasoning-alias.test.ts pins the trigger (three alias chunks stream as three reasoning events) and the three non-triggers (`reasoning_content` frames stay byte-verbatim, frames without a reasoning field invent none, role-only frames keep the meta path), plus the dual-field single-forward and the tool-frame replay path. Fixes #7432 --- src/loop/adapter-openai.ts | 51 +++++++-- tests/issue7432-reasoning-alias.test.ts | 143 ++++++++++++++++++++++++ 2 files changed, 186 insertions(+), 8 deletions(-) create mode 100644 tests/issue7432-reasoning-alias.test.ts diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts index a195352343..94efbafa5a 100644 --- a/src/loop/adapter-openai.ts +++ b/src/loop/adapter-openai.ts @@ -57,6 +57,31 @@ function filterRealToolFragments( return filtered; } +// OpenAI-compatible upstreams disagree on the thinking field's name: DeepSeek +// and llama.cpp stream reasoning_content, Ollama Cloud / vLLM / Magpie stream +// reasoning (same alias set and order as pi-ai's openai-completions reader). +// #7432: the adapter used to read reasoning_content only, so every +// reasoning-only chunk of an alias upstream fell through the dispatch ladder +// and was dropped — the client kept exactly the first token, the one frame +// that also carries `role` and took the meta fallback. +const REASONING_DELTA_FIELDS = ["reasoning_content", "reasoning", "reasoning_text"] as const; + +function reasoningDeltaIn(delta: Record): { field: string; value: string } | null { + for (const field of REASONING_DELTA_FIELDS) { + const value = delta[field]; + if (typeof value === "string" && value.length > 0) return { field, value }; + } + return null; +} + +// Client-visible frames always speak the canonical name: an alias frame is +// rewritten to reasoning_content and its original key dropped, so a client that +// saw the alias never echoes it back (upstreams silently discard a replayed +// `reasoning` field, they only read reasoning_content). +function reasoningFrameFields(field: string, value: string): Record { + return field === "reasoning_content" ? { reasoning_content: value } : { [field]: "", reasoning_content: value }; +} + // #1881: settle drops proxy-only frames without forwarding their sibling prose. // Rebuild such a frame without tool_calls so that prose still reaches the // client. The stored copy already carries cleaned values; null when nothing @@ -67,10 +92,14 @@ function withheldProseFrame(parsed: Record): Buffer | null { const delta = choice?.delta as Record | undefined; if (!choice || !delta) return null; const ct = typeof delta.content === "string" ? delta.content : ""; - const rc = typeof delta.reasoning_content === "string" ? delta.reasoning_content : ""; - if (ct.length === 0 && rc.length === 0) return null; + const rcDelta = reasoningDeltaIn(delta); + if (ct.length === 0 && rcDelta === null) return null; const cleanDelta = { ...delta }; delete cleanDelta.tool_calls; + if (rcDelta !== null && rcDelta.field !== "reasoning_content") { + delete cleanDelta[rcDelta.field]; + cleanDelta.reasoning_content = rcDelta.value; + } return Buffer.from("data: " + JSON.stringify({ ...parsed, choices: [{ ...choice, delta: cleanDelta }, ...(choices ?? []).slice(1)] }) + "\n\n", "utf8"); } @@ -600,7 +629,13 @@ export function createOpenaiAdapter(requestBody: Record, client // was filtered — a dual-field frame leaked unfiltered content // to the client while the filtered text event suppressed its // raw to avoid double-sending. - const rcIn = typeof delta.reasoning_content === "string" && delta.reasoning_content.length > 0 ? delta.reasoning_content : null; + const rcDelta = reasoningDeltaIn(delta); + const rcIn = rcDelta !== null ? rcDelta.value : null; + // #7432: an alias field (`reasoning`, `reasoning_text`) must ride + // out as reasoning_content — mark the frame changed so every raw + // path below rewrites it instead of forwarding the alias name. + const rcAliased = rcDelta !== null && rcDelta.field !== "reasoning_content"; + const rcField = rcDelta !== null ? rcDelta.field : "reasoning_content"; const ctIn = typeof delta.content === "string" && delta.content.length > 0 ? delta.content : null; let rcOut = rcIn; let ctOut = ctIn; @@ -621,10 +656,10 @@ export function createOpenaiAdapter(requestBody: Record, client // echo (#1881). Structured events stay internal here. let json = jsonStr; let obj = parsed; - if (rcChanged || ctChanged) { + if (rcChanged || ctChanged || rcAliased) { const fields: Record = {}; if (ctIn !== null) fields["content"] = ctOut ?? ""; - if (rcIn !== null) fields["reasoning_content"] = rcOut ?? ""; + if (rcIn !== null) Object.assign(fields, reasoningFrameFields(rcField, rcOut ?? "")); obj = applyDeltaFields(parsed, fields); json = JSON.stringify(obj); } @@ -640,8 +675,8 @@ export function createOpenaiAdapter(requestBody: Record, client const fin = (b: Buffer): Buffer => (finishReason ? stripFinishReasonChunk(b) : b); if (rcIn !== null && ctIn !== null) { - const raw = rcChanged || ctChanged - ? rewriteDeltaFields(parsed, { content: ctOut ?? "", reasoning_content: rcOut ?? "" }) + const raw = rcChanged || ctChanged || rcAliased + ? rewriteDeltaFields(parsed, { content: ctOut ?? "", ...reasoningFrameFields(rcField, rcOut ?? "") }) : rawBuf; const fwd = fin(raw); const hasText = ctOut !== null && ctOut.length > 0; @@ -660,7 +695,7 @@ export function createOpenaiAdapter(requestBody: Record, client yield { kind: "text", delta: ctOut!, raw: fwd } as ParsedStreamEvent; } } else if (rcIn !== null) { - const raw = rcChanged ? rewriteDeltaFields(parsed, { reasoning_content: rcOut ?? "" }) : rawBuf; + const raw = rcChanged || rcAliased ? rewriteDeltaFields(parsed, reasoningFrameFields(rcField, rcOut ?? "")) : rawBuf; yield { kind: "reasoning", delta: rcOut ?? "", raw: fin(raw) } as ParsedStreamEvent; } else if (ctIn !== null) { const raw = ctChanged ? rewriteDeltaFields(parsed, { content: ctOut ?? "" }) : rawBuf; diff --git a/tests/issue7432-reasoning-alias.test.ts b/tests/issue7432-reasoning-alias.test.ts new file mode 100644 index 0000000000..1f044fe04b --- /dev/null +++ b/tests/issue7432-reasoning-alias.test.ts @@ -0,0 +1,143 @@ +// #7432: OpenAI-compatible upstreams disagree on the thinking field's name. +// DeepSeek/llama.cpp stream `reasoning_content`; Ollama Cloud, vLLM and Magpie +// stream `reasoning`. The adapter used to read reasoning_content only, so every +// reasoning-only chunk of an alias upstream fell through the dispatch ladder — +// the client kept exactly the ONE frame that also carried `role` (meta +// fallback), i.e. the first token of the whole thinking block. +// +// Two-way pins (#3617 gate discipline): the alias MUST stream as reasoning, and +// the canonical field plus the non-reasoning frames MUST behave exactly as +// before (raw bytes verbatim, no extra events). +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createOpenaiAdapter } from "../src/loop/index.ts"; +import type { ParsedStreamEvent } from "../src/loop/index.ts"; + +function mockStream(...chunks: string[]): ReadableStream { + return new ReadableStream({ + start(controller) { + const enc = new TextEncoder(); + for (const c of chunks) controller.enqueue(enc.encode(c)); + controller.close(); + }, + }); +} + +function sseChunk(delta: Record, finishReason: string | null = null): string { + return `data: ${JSON.stringify({ + id: "c1", + object: "chat.completion.chunk", + created: 1, + model: "gpt", + choices: [{ index: 0, delta, finish_reason: finishReason }], + })}\n\n`; +} + +async function collect(...chunks: string[]): Promise { + const adapter = createOpenaiAdapter({ model: "deepseek-v4.1-flash" }); + const events: ParsedStreamEvent[] = []; + for await (const ev of adapter.parseStream(mockStream(...chunks), 1)) events.push(ev); + return events; +} + +function reasoningDeltas(events: ParsedStreamEvent[]): string[] { + return events.flatMap((e) => (e.kind === "reasoning" ? [e.delta] : [])); +} + +function clientBytes(events: ParsedStreamEvent[]): string { + return events + .map((e) => (e as { raw?: Buffer; chunk?: Buffer }).raw ?? (e as { chunk?: Buffer }).chunk) + .filter((b): b is Buffer => b !== undefined) + .map((b) => b.toString("utf8")) + .join(""); +} + +// 1. The trigger: a provider that streams `delta.reasoning` keeps its whole +// thinking block, one reasoning event per upstream chunk. +test("#7432 openai adapter: every delta.reasoning chunk streams as kind:reasoning", async () => { + const events = await collect( + sseChunk({ role: "assistant", content: "", reasoning: "Let" }), + sseChunk({ content: "", reasoning: " me compute" }), + sseChunk({ content: "", reasoning: " 17*23." }), + sseChunk({ content: "391" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + assert.deepEqual(reasoningDeltas(events), ["Let", " me compute", " 17*23."], "no alias chunk is dropped, none is merged"); + const text = events.find((e) => e.kind === "text"); + assert.ok(text && text.kind === "text", "answer text still streams"); + assert.equal(text!.delta, "391"); +}); + +// 2. Client-visible frames speak the canonical field name: the alias key is +// rewritten away, so a client that saw `reasoning` never echoes it back (an +// upstream silently discards a replayed `reasoning` field). +test("#7432 openai adapter: alias frames are rewritten to reasoning_content before the client sees them", async () => { + const events = await collect( + sseChunk({ role: "assistant", content: "", reasoning: "Let" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + const reasoning = events.find((e) => e.kind === "reasoning"); + assert.ok(reasoning && reasoning.kind === "reasoning" && reasoning.raw, "reasoning event carries client bytes"); + const raw = reasoning!.raw!.toString("utf8"); + assert.ok(raw.includes('"reasoning_content":"Let"'), `canonical field on the wire, got: ${raw}`); + assert.ok(!/"reasoning":/.test(raw), `alias key dropped, got: ${raw}`); +}); + +// 3. The non-trigger: `reasoning_content` upstreams are untouched — the raw +// frame reaches the client byte-verbatim, exactly as before the fix. +test("#7432 openai adapter: delta.reasoning_content frames stay byte-verbatim", async () => { + const frame = sseChunk({ reasoning_content: "thinking..." }); + const events = await collect(frame, sseChunk({ content: "answer" }), sseChunk({}, "stop"), `data: [DONE]\n\n`); + assert.deepEqual(reasoningDeltas(events), ["thinking..."]); + const reasoning = events.find((e) => e.kind === "reasoning"); + assert.equal(reasoning!.raw!.toString("utf8"), frame, "canonical frames are forwarded untouched"); +}); + +// 4. The other non-trigger: a frame with no reasoning field of either name is +// not promoted into one (role-only frames keep the meta path). +test("#7432 openai adapter: frames without a reasoning field yield no reasoning event", async () => { + const events = await collect( + sseChunk({ role: "assistant", content: "" }), + sseChunk({ content: "hi" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + assert.deepEqual(reasoningDeltas(events), [], "no reasoning invented"); + assert.ok(events.some((e) => e.kind === "meta"), "the role-carrying frame still rides the meta path"); +}); + +// 5. Alias + content in ONE frame: still exactly one client-visible frame, and +// the reasoning event owns it (same allocation as the #1881 dual-field pin). +test("#7432 openai adapter: a frame with reasoning alias AND content forwards once", async () => { + const events = await collect( + sseChunk({ reasoning: "think", content: "say" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + const reasoning = events.find((e) => e.kind === "reasoning"); + const text = events.find((e) => e.kind === "text"); + assert.ok(reasoning && reasoning.kind === "reasoning", "reasoning event yielded"); + assert.ok(text && text.kind === "text", "text event yielded"); + assert.equal(text!.delta, "say"); + assert.equal(text!.raw, undefined, "text event must not duplicate the frame"); + const raw = reasoning!.raw!.toString("utf8"); + assert.ok(raw.includes('"reasoning_content":"think"'), `reasoning rides canonical, got: ${raw}`); + assert.ok(raw.includes('"content":"say"'), `sibling content rides along once, got: ${raw}`); +}); + +// 6. The tool-call replay path (#1881 withheld sibling prose) reads the alias +// too: a proxy-only tool frame must still hand its reasoning to the client. +test("#7432 openai adapter: proxy-only tool frame delivers its alias reasoning prose", async () => { + const events = await collect( + sseChunk({ tool_calls: [{ index: 0, id: "call_p", type: "function", function: { name: "bili_compress", arguments: "{}" } }] }), + sseChunk({ reasoning: "sibling thought", tool_calls: [{ index: 1, id: "call_p2", type: "function", function: { name: "bili_compress", arguments: "{}" } }] }), + sseChunk({}, "tool_calls"), + `data: [DONE]\n\n`, + ); + const out = clientBytes(events); + assert.ok(out.includes("sibling thought"), `the withheld reasoning reaches the client, got: ${out}`); + assert.ok(out.includes('"reasoning_content":"sibling thought"'), `canonical field, got: ${out}`); + assert.ok(!out.includes("bili_compress"), "the proxy call still never rides along"); +}); From 728097a7188edd076dfd5432e0494281bfde8f13 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 21 Nov 2026 12:31:23 +0800 Subject: [PATCH 2/2] test: #7432 pin the reasoning_text alias row (completes the alias-table pin) Review supplement to the original commit: the alias table has three entries (reasoning_content / reasoning / reasoning_text) but the suite only pinned two. Add the third row so the full pi-ai field set is regression-pinned. --- tests/issue7432-reasoning-alias.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/issue7432-reasoning-alias.test.ts b/tests/issue7432-reasoning-alias.test.ts index 1f044fe04b..63cb597f6c 100644 --- a/tests/issue7432-reasoning-alias.test.ts +++ b/tests/issue7432-reasoning-alias.test.ts @@ -141,3 +141,20 @@ test("#7432 openai adapter: proxy-only tool frame delivers its alias reasoning p assert.ok(out.includes('"reasoning_content":"sibling thought"'), `canonical field, got: ${out}`); assert.ok(!out.includes("bili_compress"), "the proxy call still never rides along"); }); + +// 7. The full alias table is pinned, not just `reasoning`: the third entry +// (`reasoning_text`, same set and order as pi-ai's reasoningFields) streams +// as reasoning and rides out canonical. +test("#7432 openai adapter: delta.reasoning_text is read and rewritten like the other aliases", async () => { + const events = await collect( + sseChunk({ reasoning_text: "third alias" }), + sseChunk({ content: "answer" }), + sseChunk({}, "stop"), + `data: [DONE]\n\n`, + ); + assert.deepEqual(reasoningDeltas(events), ["third alias"], "reasoning_text chunk streams as reasoning"); + const reasoning = events.find((e) => e.kind === "reasoning"); + const raw = reasoning!.raw!.toString("utf8"); + assert.ok(raw.includes('"reasoning_content":"third alias"'), `canonical field, got: ${raw}`); + assert.ok(!/"reasoning_text":/.test(raw), `alias key dropped, got: ${raw}`); +});