Skip to content

Commit 112b8c6

Browse files
committed
fix(responses): strip max_output_tokens and metadata in forward mode
The ChatGPT backend (authMode: "forward") enforces a strict parameter allowlist and rejects unknown top-level fields with HTTP 400 {"detail":"Unsupported parameter: …"}. The stripPreviousResponseId doc-comment already documents that the backend rejects metadata and max_output_tokens, but only previous_response_id was actually stripped. Third-party Responses API clients (GJC, SDK wrappers) send max_output_tokens per the public OpenAI spec, causing every request through a forward-mode provider to fail with 400. Codex CLI never sends these fields (it uses reasoning.effort for output control), so the strip is a no-op for the primary client. Add stripUnsupportedForwardParams() inside the existing forward block, removing both max_output_tokens and metadata before the wire request is built. Key-auth mode is unaffected. Verified live: the exact request that returned 400 (max_output_tokens: 32000 + reasoning.effort: low) now returns 200 with response.completed.
1 parent 6d8f05f commit 112b8c6

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

‎src/adapters/openai-responses.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,23 @@ function stripPreviousResponseId(body: unknown, strip: boolean): unknown {
461461
return rest;
462462
}
463463

464+
/**
465+
* Remove top-level parameters the ChatGPT backend (`authMode: "forward"`) rejects
466+
* with `{"detail":"Unsupported parameter: …"}` (strict allowlist). Codex CLI never
467+
* sends these — it controls output length via `reasoning.effort` — but third-party
468+
* Responses API clients (GJC, SDK wrappers) include `max_output_tokens` per the
469+
* public spec. `metadata` is likewise absent from the allowlist. No-op when the
470+
* body carries neither field, keeping the common Codex path allocation-free.
471+
*/
472+
function stripUnsupportedForwardParams(body: unknown): unknown {
473+
if (!isPlainObject(body)) return body;
474+
const hasMot = Object.prototype.hasOwnProperty.call(body, "max_output_tokens");
475+
const hasMeta = Object.prototype.hasOwnProperty.call(body, "metadata");
476+
if (!hasMot && !hasMeta) return body;
477+
const { max_output_tokens: _mot, metadata: _meta, ...rest } = body;
478+
return rest;
479+
}
480+
464481
/**
465482
* Hosted tool types whose server-side function names collide with the client tools Codex
466483
* declares for the matching app skill. Codex sends BOTH (e.g. hosted `image_generation` plus a
@@ -644,6 +661,7 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
644661
);
645662
if (forward) {
646663
outBody = repairOrphanedInputItems(outBody, unexpandedMiss);
664+
outBody = stripUnsupportedForwardParams(outBody);
647665
}
648666
else outBody = stripConflictingHostedTools(outBody);
649667
if (forward || parsed._previousResponseInputExpanded === true) {

‎tests/openai-responses-passthrough.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,3 +667,69 @@ describe("OpenAI Responses hosted-tool name conflicts", () => {
667667
expect(body.tools.some(t => t.type === "function" && t.name === "image_gen.imagegen")).toBe(true);
668668
});
669669
});
670+
671+
describe("OpenAI Responses forward-mode unsupported param stripping", () => {
672+
const meta = { headers: new Headers({ authorization: "Bearer token" }) };
673+
const rawBody = {
674+
model: "gpt-5.6-sol",
675+
input: [{ role: "user", content: [{ type: "input_text", text: "ping" }] }],
676+
stream: true,
677+
store: false,
678+
max_output_tokens: 32000,
679+
metadata: { user_id: "u-1" },
680+
reasoning: { effort: "low" },
681+
};
682+
683+
test("forward mode strips max_output_tokens and metadata", () => {
684+
const adapter = createResponsesPassthroughAdapter(provider);
685+
const request = adapter.buildRequest({
686+
modelId: "gpt-5.6-sol",
687+
context: { messages: [] },
688+
stream: true,
689+
options: {},
690+
_rawBody: { ...rawBody },
691+
}, meta);
692+
const body = JSON.parse(request.body) as Record<string, unknown>;
693+
694+
expect(body).not.toHaveProperty("max_output_tokens");
695+
expect(body).not.toHaveProperty("metadata");
696+
expect(body.reasoning).toEqual({ effort: "low" });
697+
expect(body.model).toBe("gpt-5.6-sol");
698+
});
699+
700+
test("forward mode is a no-op when neither field is present", () => {
701+
const adapter = createResponsesPassthroughAdapter(provider);
702+
const { max_output_tokens: _m, metadata: _d, ...codexBody } = rawBody;
703+
const request = adapter.buildRequest({
704+
modelId: "gpt-5.6-sol",
705+
context: { messages: [] },
706+
stream: true,
707+
options: {},
708+
_rawBody: { ...codexBody },
709+
}, meta);
710+
const body = JSON.parse(request.body) as Record<string, unknown>;
711+
712+
expect(body.reasoning).toEqual({ effort: "low" });
713+
expect(body.store).toBe(false);
714+
});
715+
716+
test("key-auth mode preserves max_output_tokens and metadata", () => {
717+
const adapter = createResponsesPassthroughAdapter({
718+
adapter: "openai-responses",
719+
baseUrl: "https://api.openai.example/v1",
720+
authMode: "key",
721+
apiKey: "sk-test",
722+
});
723+
const request = adapter.buildRequest({
724+
modelId: "gpt-5.6-sol",
725+
context: { messages: [] },
726+
stream: true,
727+
options: {},
728+
_rawBody: { ...rawBody },
729+
}, { headers: new Headers() });
730+
const body = JSON.parse(request.body) as Record<string, unknown>;
731+
732+
expect(body.max_output_tokens).toBe(32000);
733+
expect(body.metadata).toEqual({ user_id: "u-1" });
734+
});
735+
});

0 commit comments

Comments
 (0)