Skip to content

Commit 6e86b18

Browse files
olddonkeyclaude
andcommitted
fix(responses): scope the null-content strip to routed destinations
The first version stripped `"content": null` from every reasoning item, which broke OpenAI. Caught in live traffic minutes after deploying it locally: 400 invalid_request_error The encrypted content k7pQ...Px7D could not be verified. Reason: Encrypted content could not be decrypted or parsed. An OpenAI-operated backend binds the blob to the item's exact shape, so removing a field invalidates it. The two requirements are exactly opposed: xAI refuses the null key, OpenAI needs it kept — so the strip has to follow the destination. The predicate is deliberately not `authMode === "forward"`. A noncanonical forward provider never receives the caller's credentials, so forward auth says nothing about which backend answers; only the canonical ChatGPT surface and the official OpenAI API are treated as OpenAI-operated, and a self-hosted relay is routed like any other gateway. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent d8426a6 commit 6e86b18

3 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎src/adapters/openai-responses.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { COMPACT_PROMPT, decodeCompactionSummary, SUMMARY_PREFIX } from "../resp
66
import { collectResponsesToolGroups } from "../responses/tool-groups";
77
import { isHostedToolUnsupportedForModel } from "../responses/hosted-tool-policy";
88
import { decodeServerSentEvents } from "../lib/sse-decoder";
9-
import { CODEX_FORWARD_BASE_URL, isCanonicalOpenAiForwardProvider } from "../providers/openai-tiers";
9+
import { CODEX_FORWARD_BASE_URL, isCanonicalOpenAiForwardProvider, isOpenAiOperatedResponsesDestination } from "../providers/openai-tiers";
1010
import { OCX_REASONING_PREFIX } from "../responses/reasoning-envelope";
1111
import { modelRecordValue } from "../reasoning-effort";
1212
import type { TranslatorBudget } from "../lib/translator-budget";
@@ -41,7 +41,7 @@ export const FORWARD_HEADERS = [
4141

4242
export function sanitizeReasoningInputContent(
4343
body: unknown,
44-
opts?: { preserveRawReasoningContent?: boolean },
44+
opts?: { preserveRawReasoningContent?: boolean; dropNullContentChannel?: boolean },
4545
): unknown {
4646
if (!body || typeof body !== "object" || Array.isArray(body)) return body;
4747
const raw = body as Record<string, unknown>;
@@ -61,7 +61,11 @@ export function sanitizeReasoningInputContent(
6161
// — xAI answers `Could not decode the compaction blob`, naming the sibling `encrypted_content`
6262
// rather than the field it actually refused, which is why this reads as a blob failure. Drop the
6363
// key so the item matches the shape the upstream issued.
64-
if ("content" in rec && !Array.isArray(rec.content)) {
64+
//
65+
// Gated to routed destinations. An OpenAI-operated backend binds the blob to the item's exact
66+
// shape, so deleting a field there invalidates it (`The encrypted content ... could not be
67+
// verified`); the two requirements are exactly opposed, and a live regression proved it.
68+
if (opts?.dropNullContentChannel === true && "content" in rec && !Array.isArray(rec.content)) {
6569
changed = true;
6670
const next: Record<string, unknown> = { ...rec };
6771
delete next.content;
@@ -1584,7 +1588,10 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
15841588
outBody = rewritten.body;
15851589
convertedRoutedToolSearchNames = rewritten.names;
15861590
}
1587-
const sanitizedBody = normalizeToolSchemas(stripSparkCompatibility(stripUnsupportedReasoningParams(stripItemIdsWhenUnstored(stripInvalidItemIds(stripUnsupportedHostedTools(sanitizeReasoningInputContent(scrubOcxCompactionItems(outBody), { preserveRawReasoningContent: provider.preserveResponsesReasoningContent === true })))))));
1591+
const sanitizedBody = normalizeToolSchemas(stripSparkCompatibility(stripUnsupportedReasoningParams(stripItemIdsWhenUnstored(stripInvalidItemIds(stripUnsupportedHostedTools(sanitizeReasoningInputContent(scrubOcxCompactionItems(outBody), {
1592+
preserveRawReasoningContent: provider.preserveResponsesReasoningContent === true,
1593+
dropNullContentChannel: !isOpenAiOperatedResponsesDestination(provider),
1594+
})))))));
15881595
const finalBody = stripDisabledReasoningSummaries(
15891596
normalizeConfiguredReasoningSummaryDelivery(sanitizedBody, provider, parsed.modelId),
15901597
provider,

‎src/providers/openai-tiers.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ export function supportsNativeResponsesCompactEndpoint(
5454
&& normalizedBaseUrl(provider.baseUrl) === OPENAI_API_BASE_URL;
5555
}
5656

57+
/**
58+
* Whether this destination is an OpenAI-operated Responses backend — the canonical ChatGPT Codex
59+
* surface or the official OpenAI API.
60+
*
61+
* Deliberately not keyed on `authMode === "forward"`: a noncanonical forward provider does not
62+
* receive the caller's credentials (see the forward-header gate in the Responses adapter), so
63+
* forward auth says nothing about which backend is on the other end.
64+
*/
65+
export function isOpenAiOperatedResponsesDestination(provider: OcxProviderConfig): boolean {
66+
if (isCanonicalOpenAiForwardProvider(provider)) return true;
67+
return provider.adapter === "openai-responses"
68+
&& normalizedBaseUrl(provider.baseUrl) === OPENAI_API_BASE_URL;
69+
}
70+
5771
export interface OpenAiTierMigrationProjection {
5872
config: OcxConfig;
5973
changed: boolean;

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,6 +2181,52 @@ describe("reasoning input content channel", () => {
21812181
expect(out.summary).toEqual([{ type: "summary_text", text: "thinking" }]);
21822182
});
21832183

2184+
// An OpenAI-operated backend binds the blob to the item's exact shape, so deleting a field there
2185+
// invalidates it: `The encrypted content ... could not be verified`. Caught in live traffic after
2186+
// an ungated first version of this fix shipped locally — the two backends want opposite things.
2187+
test("keeps a null content channel on OpenAI-operated destinations", () => {
2188+
const item = {
2189+
type: "reasoning",
2190+
content: null,
2191+
summary: [],
2192+
encrypted_content: "openai-issued-blob",
2193+
};
2194+
for (const target of [
2195+
{ adapter: "openai-responses", baseUrl: "https://chatgpt.com/backend-api/codex", authMode: "forward" as const },
2196+
{ adapter: "openai-responses", baseUrl: "https://api.openai.com/v1", authMode: "key" as const, apiKey: "sk-t" },
2197+
]) {
2198+
const request = createResponsesPassthroughAdapter(target).buildRequest({
2199+
modelId: "gpt-5.6-sol",
2200+
context: { messages: [] },
2201+
stream: true,
2202+
options: {},
2203+
_rawBody: { model: "gpt-5.6-sol", store: false, input: [item] },
2204+
}, { headers: new Headers({ authorization: "Bearer token" }) });
2205+
const out = (JSON.parse(request.body) as { input: Record<string, unknown>[] }).input[0];
2206+
expect(out).toHaveProperty("content");
2207+
expect(out.content).toBeNull();
2208+
expect(out.encrypted_content).toBe("openai-issued-blob");
2209+
}
2210+
});
2211+
2212+
// A noncanonical forward gateway does not receive the caller's credentials, so forward auth says
2213+
// nothing about which backend answers; it is routed and must get the strip.
2214+
test("strips a null content channel on a noncanonical forward relay", () => {
2215+
const request = createResponsesPassthroughAdapter({
2216+
adapter: "openai-responses",
2217+
baseUrl: "https://relay.example/backend-api/codex",
2218+
authMode: "forward",
2219+
}).buildRequest({
2220+
modelId: "grok-4.6",
2221+
context: { messages: [] },
2222+
stream: true,
2223+
options: {},
2224+
_rawBody: { model: "grok-4.6", store: false, input: [{ type: "reasoning", content: null, encrypted_content: "b" }] },
2225+
}, { headers: new Headers() });
2226+
const out = (JSON.parse(request.body) as { input: Record<string, unknown>[] }).input[0];
2227+
expect(out).not.toHaveProperty("content");
2228+
});
2229+
21842230
test("leaves an array content channel to the existing sanitizer", () => {
21852231
const out = forwarded({
21862232
type: "reasoning",

0 commit comments

Comments
 (0)