From c01679995dce17dca6cba639aa50842fd8a2835e Mon Sep 17 00:00:00 2001 From: NomenAK Date: Sat, 27 Jun 2026 10:18:59 +0000 Subject: [PATCH 1/3] fix(translator): accept Claude Messages shape in non-stream malformed-200 guard detectMalformedNonStream only recognized OpenAI Chat Completions (choices[]) and Responses API (object:"response") bodies. When a Claude client routes to a non-Claude provider, the translated non-streaming body is Claude Messages shape (type:"message", content[]), which has neither choices nor object. The guard then falsely returned empty_choices and the request became a 502, even though the upstream returned usable content. This surfaced as the mid-thread model switch 502 in Claude Code probes (non-stream, max_tokens=1) against kiro/openai/openrouter providers. Add a Claude Messages branch that treats text/thinking/tool_use blocks as usable output. Add unit coverage for usable and empty Claude bodies. --- open-sse/utils/diagnostics.ts | 22 ++++++++++++++++ tests/unit/diagnostics.test.ts | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/open-sse/utils/diagnostics.ts b/open-sse/utils/diagnostics.ts index 8c58358c80e3..167e0e9500de 100644 --- a/open-sse/utils/diagnostics.ts +++ b/open-sse/utils/diagnostics.ts @@ -168,12 +168,34 @@ export function synthResponsesFailure(reason?: MalformedReason): string { * - Tool-call responses (content=null + tool_calls=[…]) are also valid. * - Responses API function_call / other structural items count as output even * when they carry no user-visible text. + * - Claude Messages shape (type:"message" + content[]) is checked directly, + * since a Claude client receives the translated body in that shape (no + * `choices`/`object:"response"`), e.g. a non-Claude provider answering a + * Claude-format request. */ export function detectMalformedNonStream(resp: unknown): MalformedReason | null { if (!resp || typeof resp !== "object") return "empty_choices"; const body = resp as Record; + // ── Claude Messages shape ── + if (body.type === "message") { + const content = body.content; + const hasOutput = + Array.isArray(content) && + content.some((block) => { + if (!block || typeof block !== "object") return false; + const b = block as Record; + if (b.type === "text") return typeof b.text === "string" && (b.text as string).length > 0; + if (b.type === "thinking") + return typeof b.thinking === "string" && (b.thinking as string).length > 0; + if (b.type === "tool_use") + return typeof b.name === "string" && (b.name as string).length > 0; + return false; + }); + return hasOutput ? null : "empty_choices"; + } + // ── Responses API shape ── if (body.object === "response") { const output = body.output; diff --git a/tests/unit/diagnostics.test.ts b/tests/unit/diagnostics.test.ts index 82b202e385a2..079834654087 100644 --- a/tests/unit/diagnostics.test.ts +++ b/tests/unit/diagnostics.test.ts @@ -168,6 +168,53 @@ test("detectMalformedNonStream allows Responses API function_call items as valid assert.equal(detectMalformedNonStream(body), null); }); +// ── (c2) detectMalformedNonStream — Claude Messages shape ──────────────────── + +test("detectMalformedNonStream returns null for Claude message with text content", () => { + const body = { + type: "message", + role: "assistant", + content: [{ type: "text", text: "Hi!" }], + stop_reason: "end_turn", + }; + assert.equal(detectMalformedNonStream(body), null); +}); + +test("detectMalformedNonStream returns null for Claude message with tool_use block", () => { + const body = { + type: "message", + role: "assistant", + content: [{ type: "tool_use", id: "toolu_1", name: "search", input: {} }], + stop_reason: "tool_use", + }; + assert.equal(detectMalformedNonStream(body), null); +}); + +test("detectMalformedNonStream returns null for Claude message with thinking block", () => { + const body = { + type: "message", + role: "assistant", + content: [{ type: "thinking", thinking: "let me think" }], + stop_reason: "end_turn", + }; + assert.equal(detectMalformedNonStream(body), null); +}); + +test("detectMalformedNonStream returns 'empty_choices' for Claude message with empty content", () => { + const body = { type: "message", role: "assistant", content: [], stop_reason: "end_turn" }; + assert.equal(detectMalformedNonStream(body), "empty_choices"); +}); + +test("detectMalformedNonStream returns 'empty_choices' for Claude message with empty-text block", () => { + const body = { + type: "message", + role: "assistant", + content: [{ type: "text", text: "" }], + stop_reason: "end_turn", + }; + assert.equal(detectMalformedNonStream(body), "empty_choices"); +}); + // ── (d) no stack trace leakage ─────────────────────────────────────────────── test("synthOpenAIErrorChunk message does NOT contain stack trace path", () => { From f994548193faf61c9f9805d86892a3f271048a26 Mon Sep 17 00:00:00 2001 From: NomenAK Date: Sat, 27 Jun 2026 10:50:50 +0000 Subject: [PATCH 2/3] fix(translator): treat Claude "(empty response)" placeholder as malformed convertOpenAINonStreamingToClaude substitutes the literal "(empty response)" text when the upstream produced no content. The new Claude Messages branch in detectMalformedNonStream would otherwise accept that placeholder as usable output, letting a genuinely empty completion pass the guard for Claude clients while still 502-ing for OpenAI clients. Exclude the placeholder so empty completions trip empty_choices on both paths. Add unit coverage for the placeholder case. --- open-sse/utils/diagnostics.ts | 12 +++++++++++- tests/unit/diagnostics.test.ts | 12 ++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/open-sse/utils/diagnostics.ts b/open-sse/utils/diagnostics.ts index 167e0e9500de..9d3bb3d846f6 100644 --- a/open-sse/utils/diagnostics.ts +++ b/open-sse/utils/diagnostics.ts @@ -186,7 +186,17 @@ export function detectMalformedNonStream(resp: unknown): MalformedReason | null content.some((block) => { if (!block || typeof block !== "object") return false; const b = block as Record; - if (b.type === "text") return typeof b.text === "string" && (b.text as string).length > 0; + if (b.type === "text") { + // convertOpenAINonStreamingToClaude emits "(empty response)" as a + // placeholder when the upstream produced no content; treat it as + // empty so a genuinely empty completion still trips the guard + // (parity with the OpenAI `content:""` path). + return ( + typeof b.text === "string" && + (b.text as string).length > 0 && + b.text !== "(empty response)" + ); + } if (b.type === "thinking") return typeof b.thinking === "string" && (b.thinking as string).length > 0; if (b.type === "tool_use") diff --git a/tests/unit/diagnostics.test.ts b/tests/unit/diagnostics.test.ts index 079834654087..b2a2d49b03cd 100644 --- a/tests/unit/diagnostics.test.ts +++ b/tests/unit/diagnostics.test.ts @@ -215,6 +215,18 @@ test("detectMalformedNonStream returns 'empty_choices' for Claude message with e assert.equal(detectMalformedNonStream(body), "empty_choices"); }); +test("detectMalformedNonStream returns 'empty_choices' for Claude message with (empty response) placeholder", () => { + // convertOpenAINonStreamingToClaude substitutes this placeholder for empty + // upstream content; it must still be treated as malformed. + const body = { + type: "message", + role: "assistant", + content: [{ type: "text", text: "(empty response)" }], + stop_reason: "end_turn", + }; + assert.equal(detectMalformedNonStream(body), "empty_choices"); +}); + // ── (d) no stack trace leakage ─────────────────────────────────────────────── test("synthOpenAIErrorChunk message does NOT contain stack trace path", () => { From c47c8e6edb4392d4748861a5f2980e2ce575d6d2 Mon Sep 17 00:00:00 2001 From: Diego Rodrigues de Sa e Souza Date: Sat, 27 Jun 2026 11:59:58 -0300 Subject: [PATCH 3/3] fix(diagnostics): dedupe Claude Messages malformed-200 guard; keep signature/redacted_thinking valid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Merging release/v3.8.39 surfaced a duplicate `body.type === "message"` branch: this PR's new handler shadowed the canonical one added by #5116 (#5108), and its thinking check required non-empty `thinking` text — so an extended-thinking block that carries only a `signature` (thinking text "") was misclassified as `empty_choices`, regressing #5108 (false 502 on Claude thinking-only completions). Collapse to the single canonical handler and fold in this PR's intent: - text: valid when non-empty AND not the "(empty response)" placeholder - thinking: valid when it has visible text OR a non-empty `signature` - redacted_thinking / tool_use(id) remain valid structural output Tests: tests/unit/diagnostics-claude-thinking-5108.test.ts 6/6, tests/unit/diagnostics.test.ts 29/29 (incl. the "(empty response)" placeholder case). Co-authored-by: NomenAK <39598727+NomenAK@users.noreply.github.com> --- open-sse/utils/diagnostics.ts | 54 +++++++++++------------------------ 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/open-sse/utils/diagnostics.ts b/open-sse/utils/diagnostics.ts index e668b87d3bb7..d84eabdf7995 100644 --- a/open-sse/utils/diagnostics.ts +++ b/open-sse/utils/diagnostics.ts @@ -169,43 +169,14 @@ export function synthResponsesFailure(reason?: MalformedReason): string { * - Responses API function_call / other structural items count as output even * when they carry no user-visible text. * - Claude Messages shape (type:"message" + content[]) is checked directly, - * since a Claude client receives the translated body in that shape (no - * `choices`/`object:"response"`), e.g. a non-Claude provider answering a - * Claude-format request. + * since a Claude client receives the body in that shape (no + * `choices`/`object:"response"`). */ export function detectMalformedNonStream(resp: unknown): MalformedReason | null { if (!resp || typeof resp !== "object") return "empty_choices"; const body = resp as Record; - // ── Claude Messages shape ── - if (body.type === "message") { - const content = body.content; - const hasOutput = - Array.isArray(content) && - content.some((block) => { - if (!block || typeof block !== "object") return false; - const b = block as Record; - if (b.type === "text") { - // convertOpenAINonStreamingToClaude emits "(empty response)" as a - // placeholder when the upstream produced no content; treat it as - // empty so a genuinely empty completion still trips the guard - // (parity with the OpenAI `content:""` path). - return ( - typeof b.text === "string" && - (b.text as string).length > 0 && - b.text !== "(empty response)" - ); - } - if (b.type === "thinking") - return typeof b.thinking === "string" && (b.thinking as string).length > 0; - if (b.type === "tool_use") - return typeof b.name === "string" && (b.name as string).length > 0; - return false; - }); - return hasOutput ? null : "empty_choices"; - } - // ── Responses API shape ── if (body.object === "response") { const output = body.output; @@ -245,16 +216,25 @@ export function detectMalformedNonStream(resp: unknown): MalformedReason | null // throws on `null.type` (that would crash the whole non-stream classifier). if (block === null || typeof block !== "object") return false; const b = block as Record; - // Text block with visible text. - if (b.type === "text" && typeof b.text === "string" && (b.text as string).length > 0) { + // Text block with visible text. `convertOpenAINonStreamingToClaude` emits + // "(empty response)" as a placeholder when the upstream produced no content, + // so treat that sentinel as empty — a genuinely empty completion still trips + // the guard (parity with the OpenAI `content:""` path). + if ( + b.type === "text" && + typeof b.text === "string" && + (b.text as string).length > 0 && + b.text !== "(empty response)" + ) { return true; } - // Extended-thinking block: a non-empty `signature` is cryptographic proof the - // thinking step ran, so it is a valid completion even when the thinking text is "". + // Extended-thinking block: valid when it carries visible thinking text OR a + // non-empty `signature` (cryptographic proof the thinking step ran, so it is a + // valid completion even when the thinking text is ""). if ( b.type === "thinking" && - typeof b.signature === "string" && - (b.signature as string).length > 0 + ((typeof b.thinking === "string" && (b.thinking as string).length > 0) || + (typeof b.signature === "string" && (b.signature as string).length > 0)) ) { return true; }