Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions open-sse/utils/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>;

// ── 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<string, unknown>;
if (b.type === "text") return typeof b.text === "string" && (b.text as string).length > 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In responseTranslator.ts, the function convertOpenAINonStreamingToClaude translates empty or missing content into a placeholder block with text: "(empty response)". Since detectMalformedNonStream runs after response translation, a translated empty response will contain text: "(empty response)". Currently, this block is treated as valid output because its length is greater than 0, which allows empty responses to bypass this malformed guard for Claude clients. We should explicitly exclude "(empty response)" from being considered as valid output.

Suggested change
if (b.type === "text") return typeof b.text === "string" && (b.text as string).length > 0;
if (b.type === "text") 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;
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Comment on lines +249 to +257

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add a unit test to verify that the "(empty response)" placeholder is correctly identified as empty_choices by detectMalformedNonStream.

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");
});

test("detectMalformedNonStream returns 'empty_choices' for Claude message with '(empty response)' placeholder", () => {
  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", () => {
Expand Down
Loading