Skip to content

Commit 369be81

Browse files
authored
fix(responses): give every reasoning input item the summary the upstream requires (#4673)
* fix(responses): give every reasoning input item the summary the upstream requires A reasoning item forwarded without `summary` is refused with `Missing required parameter: 'input[N].summary'` before inference, while responsesRequestSchema marks the field optional so it passed every local gate. The chat ingress minted such an item for a replayed assistant turn, and the reasoning sanitizer passed any client-supplied one through untouched. The ingress now carries the replayed thinking as a summary_text part, mirroring src/claude/inbound.ts, and the sanitizer supplies an empty summary for any item that arrives without the key. * test(codex-integration): expect the sanitizer-supplied reasoning summary in issue-702 replay
1 parent 9b71107 commit 369be81

5 files changed

Lines changed: 79 additions & 3 deletions

File tree

‎src/adapters/openai-responses/reasoning.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ import { isPlainObject } from "./internal";
1212
* destinations, a present non-array `content` field is omitted. Otherwise non-empty array content
1313
* is blanked unless raw reasoning preservation is enabled; removing an `ocxr1:` envelope selects
1414
* the same blanking path when non-array omission is not active.
15+
*
16+
* A reasoning item that arrives with no `summary` key at all also gets an empty one. The field is
17+
* required on a reasoning input item by the Responses API — a missing one is refused with
18+
* `Missing required parameter: 'input[N].summary'` before inference — while
19+
* responsesRequestSchema marks it optional, so such an item passes every local gate and fails only
20+
* on the wire. This is not gated on the destination, because it reshapes nothing a canonical
21+
* backend issued: every reasoning item Codex and this proxy emit already carries `summary`, so an
22+
* item missing the key came from a translated ingress (`/v1/chat/completions`, `/v1/messages`) or
23+
* a third-party client, and injecting the empty array is the whole shape it was missing.
1524
*/
1625
export function sanitizeReasoningInputContent(
1726
body: unknown,
@@ -36,6 +45,7 @@ export function sanitizeReasoningInputContent(
3645
const hasOcxEnvelope = typeof rec.encrypted_content === "string" && rec.encrypted_content.startsWith(OCX_REASONING_PREFIX);
3746
const hasOutputStatus = Object.prototype.hasOwnProperty.call(rec, "status");
3847
const hasEncryptedContent = Object.prototype.hasOwnProperty.call(rec, "encrypted_content");
48+
const missingSummary = !Object.prototype.hasOwnProperty.call(rec, "summary");
3949
const stripEncryptedContent = hasOcxEnvelope
4050
|| (opts?.stripEncryptedContent === true && hasEncryptedContent);
4151
// Codex serializes an absent reasoning content channel as `"content": null`. The field is
@@ -59,11 +69,12 @@ export function sanitizeReasoningInputContent(
5969
const blankContent = !dropNullContentChannel
6070
&& !opts?.preserveRawReasoningContent
6171
&& (hasRawContent || hasOcxEnvelope);
62-
if (!blankContent && !stripOutputStatus && !stripEncryptedContent && !dropNullContentChannel) {
72+
if (!blankContent && !stripOutputStatus && !stripEncryptedContent && !dropNullContentChannel && !missingSummary) {
6373
return item;
6474
}
6575
changed = true;
6676
const next: Record<string, unknown> = { ...rec };
77+
if (missingSummary) next.summary = [];
6778
if (dropNullContentChannel) delete next.content;
6879
if (stripOutputStatus) delete next.status;
6980
if (stripEncryptedContent) delete next.encrypted_content;

‎src/chat/inbound.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,18 @@ export function chatCompletionsToResponsesBody(raw: unknown): Rec {
313313
// here keeps that adjacency intact.
314314
const reasoningText = assistantReasoningText(msg);
315315
if (reasoningText !== undefined) {
316-
input.push({ type: "reasoning", content: [{ type: "reasoning_text", text: reasoningText }] });
316+
// `summary` is required on a reasoning input item by the OpenAI Responses API, and our
317+
// own responsesRequestSchema marks it optional, so a summary-less item validated locally
318+
// and was refused upstream with `Missing required parameter: 'input[N].summary'`. It also
319+
// has to carry the text, not just satisfy the field: sanitizeReasoningInputContent blanks
320+
// `content` for every destination except a `preserveResponsesReasoningContent` provider,
321+
// so summary is the only channel that survives to a native backend. This mirrors the
322+
// Claude ingress (src/claude/inbound.ts), which has always minted both.
323+
input.push({
324+
type: "reasoning",
325+
summary: [{ type: "summary_text", text: reasoningText }],
326+
content: [{ type: "reasoning_text", text: reasoningText }],
327+
});
317328
}
318329
const blocks = assistantContentToBlocks(msg.content);
319330
if (blocks.length > 0) input.push({ type: "message", role: "assistant", content: blocks });

‎tests/codex-integration/issue-702-expired-replay-state.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,11 @@ describe("routed replay recovery", () => {
379379
expect(upstreamRequests).toHaveLength(1);
380380
expect(upstreamRequests[0]!.previous_response_id).toBeUndefined();
381381
expect(upstreamRequests[0]!.input).toEqual([
382-
history[0], reasoning,
382+
history[0],
383+
// The client replayed this reasoning item with no `summary`; the reasoning sanitizer
384+
// supplies the empty array the Responses API requires on a reasoning input item, so the
385+
// forwarded item is the replayed one plus that field.
386+
{ ...reasoning, summary: [] },
383387
{ type: "function_call", call_id: "call_replay", name: custom ? "exec" : "lookup",
384388
arguments: custom ? JSON.stringify({ input: "text(1)" }) : "{}", status: "completed" },
385389
{ ...toolResult, type: "function_call_output" },

‎tests/providers/deepseek-reasoning-replay.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,36 @@ describe("sanitizeReasoningInputContent scoping", () => {
3838
type: "reasoning",
3939
id: "rs_1",
4040
content: [],
41+
// `reasoningItem` omits `summary`, and the sanitizer now supplies the empty array the
42+
// Responses API requires on every reasoning input item.
43+
summary: [],
4144
encrypted_content: "native-blob",
4245
});
4346
});
4447

48+
// Regression: a reasoning item translated from `/v1/chat/completions` or `/v1/messages` carried
49+
// no `summary`, which responsesRequestSchema allows and the upstream does not — the request was
50+
// refused with `Missing required parameter: 'input[N].summary'` before inference.
51+
test("a summary-less reasoning item gains the required empty summary", () => {
52+
const out = inputOf(sanitizeReasoningInputContent({ model: "m", input: [reasoningItem()] }));
53+
expect(out[0]!.summary).toEqual([]);
54+
});
55+
56+
test("an existing summary is left exactly as it arrived", () => {
57+
const summary = [{ type: "summary_text", text: "chain" }];
58+
const out = inputOf(sanitizeReasoningInputContent({ model: "m", input: [reasoningItem({ summary })] }));
59+
expect(out[0]!.summary).toEqual(summary);
60+
});
61+
62+
test("a summary-less item is repaired even where content is preserved", () => {
63+
const out = inputOf(sanitizeReasoningInputContent(
64+
{ model: "m", input: [reasoningItem()] },
65+
{ preserveRawReasoningContent: true },
66+
));
67+
expect(out[0]!.summary).toEqual([]);
68+
expect(out[0]!.content).toEqual([{ type: "reasoning_text", text: "think step by step" }]);
69+
});
70+
4571
test("default behavior still blanks reasoning content (ChatGPT backend rule)", () => {
4672
const out = inputOf(sanitizeReasoningInputContent({ model: "m", input: [reasoningItem()] }));
4773
expect(out[0]!.content).toEqual([]);

‎tests/responses/chat-inbound-reasoning-replay.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
import { describe, expect, test } from "bun:test";
1717
import { chatCompletionsToResponsesBody } from "../../src/chat/inbound";
18+
import { sanitizeReasoningInputContent } from "../../src/adapters/openai-responses";
1819
import { responsesRequestSchema } from "../../src/responses/schema";
1920

2021
type Item = Record<string, unknown>;
@@ -41,6 +42,29 @@ describe("F6 assistant reasoning survives translation", () => {
4142
expect(out[idx + 1]).toMatchObject({ type: "message", role: "assistant" });
4243
});
4344

45+
// Regression: a Pi/Aside chat replay reached a Responses backend as
46+
// `{ type: "reasoning", content: [...] }` with no `summary`, and the upstream refused the
47+
// whole request with `Missing required parameter: 'input[2].summary'`. The field is optional
48+
// in responsesRequestSchema, so only the live call failed.
49+
test("the synthesized reasoning item carries the summary the Responses API requires", () => {
50+
const item = items(body([USER, { role: "assistant", content: "a", reasoning_content: "prior analysis" }]))
51+
.find(i => i.type === "reasoning")!;
52+
53+
expect(item.summary).toEqual([{ type: "summary_text", text: "prior analysis" }]);
54+
});
55+
56+
// sanitizeReasoningInputContent blanks `content` on every destination that does not opt into
57+
// plaintext replay, so the summary is what actually reaches a native backend.
58+
test("the replayed thinking survives reasoning-content sanitization", () => {
59+
const sanitized = sanitizeReasoningInputContent(
60+
body([USER, { role: "assistant", content: "a", reasoning_content: "prior analysis" }]),
61+
) as Record<string, unknown>;
62+
const item = (sanitized.input as Item[]).find(i => i.type === "reasoning")!;
63+
64+
expect(item.content).toEqual([]);
65+
expect(item.summary).toEqual([{ type: "summary_text", text: "prior analysis" }]);
66+
});
67+
4468
test("reasoning_details segments are joined in order", () => {
4569
const out = items(body([USER, {
4670
role: "assistant",

0 commit comments

Comments
 (0)