Skip to content

Commit 9824715

Browse files
committed
fix(responses): cover non-streaming passthrough and case-insensitive model gate
CodeRabbit follow-ups: - Apply the summary-channel rewrite to the bounded-JSON passthrough path too (plain JSON answers and forced JSON-to-SSE reframing both build from clientJson), handling both the SSE completed-event shape and the bare response document shape DeepSeek returns for stream:false. - Return the original reasoning item untouched when it carries no reasoning_text content, so summary-channel items are never cleared. - Normalize both sides of the preserveReasoningContentModels match so mixed-case configured ids still gate the rewrite.
1 parent f28c0e1 commit 9824715

3 files changed

Lines changed: 129 additions & 11 deletions

File tree

‎src/server/responses-reasoning-summary-rewrite.ts‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@ function reasoningTextOf(item: Record<string, unknown>): string {
3535
function reasoningItemToSummaryShape(item: Record<string, unknown>): Record<string, unknown> {
3636
if (item.type !== "reasoning") return item;
3737
const text = reasoningTextOf(item);
38+
// Items that already use the summary channel (or carry no content text at
39+
// all) are left untouched: rewriting them could clear a valid summary.
40+
if (text.length === 0) return item;
3841
const next: Record<string, unknown> = { ...item };
3942
delete next.content;
40-
// Preserve an existing summary when the item carries no content-channel text
41-
// (a future upstream may emit both channels); only synthesize the summary
42-
// from content when content is actually present.
43-
next.summary = text.length > 0
44-
? [{ type: "summary_text", text }]
45-
: (Array.isArray(next.summary) ? next.summary : []);
43+
next.summary = [{ type: "summary_text", text }];
4644
return next;
4745
}
4846

@@ -84,6 +82,7 @@ function rewritePayload(payload: Record<string, unknown>): Record<string, unknow
8482
changed = true;
8583
}
8684
}
85+
// SSE event shape: {type: "response.completed", response: {output}}.
8786
const response = isPlainObject(next.response) ? { ...next.response } : null;
8887
if (response && Array.isArray(response.output)) {
8988
const output = response.output.map(item => {
@@ -97,6 +96,17 @@ function rewritePayload(payload: Record<string, unknown>): Record<string, unknow
9796
next.response = response;
9897
}
9998
}
99+
// Bare response document shape (non-streaming passthrough):
100+
// {object: "response", output: [...]}.
101+
if (Array.isArray(next.output)) {
102+
const output = next.output.map(item => {
103+
if (!isPlainObject(item) || item.type !== "reasoning") return item;
104+
const rewritten = reasoningItemToSummaryShape(item);
105+
if (rewritten !== item) changed = true;
106+
return rewritten;
107+
});
108+
if (changed) next.output = output;
109+
}
100110
return changed ? next : null;
101111
}
102112
}
@@ -117,6 +127,31 @@ export function createReasoningSummaryChannelPayloadRewrite(): SsePayloadRewrite
117127
};
118128
}
119129

130+
/**
131+
* Object-level variant for the non-streaming passthrough: the bounded-JSON
132+
* relay bypasses the SSE payload rewrite, so reasoning items inside a full
133+
* Responses JSON document need the same normalization before plain JSON
134+
* serialization or forced JSON-to-SSE reframing. Returns the same reference
135+
* when nothing changed.
136+
*/
137+
export function rewriteReasoningSummaryInJson(value: unknown): unknown {
138+
if (!isPlainObject(value)) return value;
139+
const rewritten = rewritePayload(value);
140+
return rewritten !== null ? rewritten : value;
141+
}
142+
143+
/** String-level variant of {@link rewriteReasoningSummaryInJson}. */
144+
export function rewriteReasoningSummaryInJsonString(json: string): string {
145+
let parsed: unknown;
146+
try {
147+
parsed = JSON.parse(json);
148+
} catch {
149+
return json;
150+
}
151+
const rewritten = rewriteReasoningSummaryInJson(parsed);
152+
return rewritten === parsed ? json : JSON.stringify(rewritten);
153+
}
154+
120155
/**
121156
* True when a routed native-Responses provider emits content-channel reasoning
122157
* (raw `reasoning_text`) instead of the summary channel. DeepSeek's
@@ -130,5 +165,7 @@ export function routeUsesContentChannelReasoning(
130165
): boolean {
131166
if (provider.statelessResponses === true) return true;
132167
const preserved = provider.preserveReasoningContentModels;
133-
return Array.isArray(preserved) && preserved.some(id => id === modelId || id === modelId.toLowerCase());
168+
const normalizedModelId = modelId.toLowerCase();
169+
return Array.isArray(preserved)
170+
&& preserved.some(id => id.toLowerCase() === normalizedModelId);
134171
}

‎src/server/responses/core.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ import {
210210
} from "../responses-item-id-repair";
211211
import {
212212
createReasoningSummaryChannelPayloadRewrite,
213+
rewriteReasoningSummaryInJsonString,
213214
routeUsesContentChannelReasoning,
214215
} from "../responses-reasoning-summary-rewrite";
215216
import {
@@ -2999,9 +3000,15 @@ async function handleResponsesInner(
29993000
const repaired = hasResponsesSnapshotRepair(route.provider.responsesSnapshotRepair)
30003001
? repairResponsesSnapshotJson(restored, outboundRequestBody)
30013002
: restored;
3002-
return parsed._responseModelId !== undefined && parsed._responseModelId !== parsed.modelId
3003+
const modelRewritten = parsed._responseModelId !== undefined && parsed._responseModelId !== parsed.modelId
30033004
? rewriteResponsesModelJson(repaired, parsed._responseModelId)
30043005
: repaired;
3006+
// The bounded-JSON answer bypasses the SSE payload rewrite, so content-
3007+
// channel reasoning needs the same normalization here for the plain
3008+
// JSON answer and every reframed-SSE variant built from clientJson.
3009+
return routeUsesContentChannelReasoning(route.provider, route.modelId)
3010+
? rewriteReasoningSummaryInJsonString(modelRewritten)
3011+
: modelRewritten;
30053012
})();
30063013
// #1700: same fail-closed policy as the SSE relay above. Both the plain JSON answer and
30073014
// the reframed-SSE branch below are built from this body, so one check covers them. This

‎tests/responses-reasoning-summary-rewrite.test.ts‎

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { describe, expect, test } from "bun:test";
22
import {
33
createReasoningSummaryChannelPayloadRewrite,
44
routeUsesContentChannelReasoning,
5+
rewriteReasoningSummaryInJson,
6+
rewriteReasoningSummaryInJsonString,
57
} from "../src/server/responses-reasoning-summary-rewrite";
68

79
const rewrite = createReasoningSummaryChannelPayloadRewrite();
@@ -107,19 +109,19 @@ describe("responses reasoning summary channel rewrite", () => {
107109
}
108110
});
109111

110-
test("keeps an empty reasoning item without inventing a summary", () => {
112+
test("leaves a reasoning item without content text untouched", () => {
111113
expect(apply({
112114
type: "response.output_item.done",
113115
output_index: 0,
114116
item: { type: "reasoning", id: "rs_1", status: "completed", content: [], summary: [] },
115117
})).toEqual({
116118
type: "response.output_item.done",
117119
output_index: 0,
118-
item: { type: "reasoning", id: "rs_1", status: "completed", summary: [] },
120+
item: { type: "reasoning", id: "rs_1", status: "completed", content: [], summary: [] },
119121
});
120122
});
121123

122-
test("preserves an existing summary when content is empty", () => {
124+
test("preserves a summary-channel reasoning item as-is", () => {
123125
expect(apply({
124126
type: "response.output_item.done",
125127
output_index: 0,
@@ -137,11 +139,72 @@ describe("responses reasoning summary channel rewrite", () => {
137139
type: "reasoning",
138140
id: "rs_1",
139141
status: "completed",
142+
content: [],
140143
summary: [{ type: "summary_text", text: "already summarized" }],
141144
},
142145
});
143146
});
144147

148+
test("rewrites reasoning items inside a bare completed response document", () => {
149+
const doc = {
150+
id: "resp_1",
151+
object: "response",
152+
status: "completed",
153+
output: [
154+
{
155+
type: "reasoning",
156+
id: "rs_1",
157+
status: "completed",
158+
content: [{ type: "reasoning_text", text: "thinking" }],
159+
summary: [],
160+
},
161+
{ type: "message", id: "msg_1", status: "completed", content: [{ type: "output_text", text: "OK" }] },
162+
],
163+
};
164+
const result = rewriteReasoningSummaryInJson(doc) as { output: Record<string, unknown>[] };
165+
expect(result.output[0]).toEqual({
166+
type: "reasoning",
167+
id: "rs_1",
168+
status: "completed",
169+
summary: [{ type: "summary_text", text: "thinking" }],
170+
});
171+
expect(result.output[1]).toEqual(doc.output[1]);
172+
});
173+
174+
test("rewrites reasoning items inside an SSE completed event document", () => {
175+
const doc = {
176+
type: "response.completed",
177+
response: {
178+
id: "resp_1",
179+
status: "completed",
180+
output: [
181+
{
182+
type: "reasoning",
183+
id: "rs_1",
184+
status: "completed",
185+
content: [{ type: "reasoning_text", text: "thinking" }],
186+
summary: [],
187+
},
188+
],
189+
},
190+
};
191+
const result = rewriteReasoningSummaryInJson(doc) as { response: { output: Record<string, unknown>[] } };
192+
expect(result.response.output[0]).toEqual({
193+
type: "reasoning",
194+
id: "rs_1",
195+
status: "completed",
196+
summary: [{ type: "summary_text", text: "thinking" }],
197+
});
198+
});
199+
200+
test("string-level rewrite leaves summary-channel documents untouched", () => {
201+
const doc = JSON.stringify({
202+
id: "resp_1",
203+
output: [{ type: "reasoning", id: "rs_1", summary: [{ type: "summary_text", text: "already summarized" }] }],
204+
});
205+
expect(rewriteReasoningSummaryInJsonString(doc)).toBe(doc);
206+
});
207+
145208
test("malformed payloads pass through unchanged", () => {
146209
expect(rewrite("not json")).toBe("not json");
147210
expect(rewrite("[1,2]")).toBe("[1,2]");
@@ -160,6 +223,17 @@ describe("routeUsesContentChannelReasoning", () => {
160223
)).toBe(true);
161224
});
162225

226+
test("model matching is case-insensitive on both sides", () => {
227+
expect(routeUsesContentChannelReasoning(
228+
{ preserveReasoningContentModels: ["DeepSeek-V4-Flash"] },
229+
"deepseek-v4-flash",
230+
)).toBe(true);
231+
expect(routeUsesContentChannelReasoning(
232+
{ preserveReasoningContentModels: ["deepseek-v4-flash"] },
233+
"DeepSeek-V4-Flash",
234+
)).toBe(true);
235+
});
236+
163237
test("other providers do not", () => {
164238
expect(routeUsesContentChannelReasoning({}, "gpt-5.5")).toBe(false);
165239
});

0 commit comments

Comments
 (0)