Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions changelog.d/fixes/7285-combo-finish-reason.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- fix(sse): combo failover now detects OpenAI-shape streams truncated without `finish_reason`/`[DONE]` (#7285)
54 changes: 53 additions & 1 deletion open-sse/services/combo/validateQuality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import {
createSSEDataLineNormalizer,
hasOpenAIFinishReason,
isKnownNonClaudeStreamPayload,
isOpenAIChoicesPayload,
} from "../../utils/streamHelpers.ts";
import { evaluateResponseValidation, type ResponseValidationConfig } from "./responseValidation.ts";
import { getReasoningTokens } from "../../../src/lib/usage/tokenAccounting.ts";
Expand Down Expand Up @@ -98,6 +100,29 @@ function messageDeltaEndsLifecycle(parsed: Record<string, unknown>): boolean {
return asObject(parsed, "delta")?.stop_reason != null;
}

/**
* Mutable OpenAI-shape lifecycle flags (#7285) — tracked independently of
* {@link SseLifecycleFlags} because the truncation signal here (a stream that
* closes without ever carrying `finish_reason` or a `[DONE]` sentinel) is
* orthogonal to the Claude event switch and must fire even when
* `hasOpenAICompatibleStreamValue()` never sees real content (e.g. a
* role-only delta).
*/
interface OpenAiLifecycleFlags {
hasChoicePayload: boolean;
hasTerminalMarker: boolean;
}

/** Update `flags` in place from one parsed OpenAI-shape SSE `data:` payload. */
function applyOpenAiLifecycleEvent(
parsed: Record<string, unknown>,
flags: OpenAiLifecycleFlags
): void {
if (!isOpenAIChoicesPayload(parsed)) return;
flags.hasChoicePayload = true;
if (hasOpenAIFinishReason(parsed)) flags.hasTerminalMarker = true;
}

/**
* Apply a single parsed Claude SSE event to the peeked lifecycle `flags`
* (mutated in place). Extracted from `parseAccumulatedSse`'s inline switch to
Expand Down Expand Up @@ -228,6 +253,8 @@ export async function validateResponseQuality(
};
let anyContentFound = false;
let sawAnyBytes = false;
// #7285: OpenAI-shape lifecycle tracking, parallel to `sse` above.
const openAi: OpenAiLifecycleFlags = { hasChoicePayload: false, hasTerminalMarker: false };
const sseLineNormalizer = createSSEDataLineNormalizer();
let pendingEventType = "";

Expand Down Expand Up @@ -259,7 +286,13 @@ export async function validateResponseQuality(
}

const data = trimmed.slice(5).trim();
if (!data || data === "[DONE]") continue;
if (!data) continue;
if (data === "[DONE]") {
// #7285: `[DONE]` is itself a terminal marker for OpenAI-shape
// streams, even when no earlier chunk carried `finish_reason`.
openAi.hasTerminalMarker = true;
continue;
}

let parsed: Record<string, unknown>;
try {
Expand All @@ -268,6 +301,8 @@ export async function validateResponseQuality(
continue;
}

applyOpenAiLifecycleEvent(parsed, openAi);

const eventType =
(typeof parsed.type === "string" ? parsed.type : null) || pendingEventType || "";
pendingEventType = "";
Expand Down Expand Up @@ -362,6 +397,23 @@ export async function validateResponseQuality(
return { valid: false, reason: "streaming no recognized content" };
}

// Issue #7285: an OpenAI-shape stream (`choices[]` chunks) that
// closes without ever carrying `finish_reason` or a `[DONE]`
// sentinel, and without producing recognized content, is a
// truncated response — failover to a sibling combo target rather
// than forwarding the incomplete stream as a success. Does not
// affect Claude-shape streams (`openAi.hasChoicePayload` stays
// false for those) and does not regress the #3399/#3685
// pass-through contract: a healthy stream exits the peek loop
// early via the `foundContent` branch above and never reaches here.
if (openAi.hasChoicePayload && !openAi.hasTerminalMarker && !anyContentFound) {
log.warn?.(
"COMBO",
"Streaming OpenAI-shape response ended with no finish_reason or [DONE] — marking as invalid for combo failover"
);
return { valid: false, reason: "streaming openai truncated without finish_reason" };
}

// Incomplete lifecycle or non-Claude stream — replay all buffered
// bytes. The reader is exhausted so the forwarding reader will
// immediately signal done.
Expand Down
16 changes: 16 additions & 0 deletions open-sse/utils/streamHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,22 @@ function hasGeminiCandidateStreamValue(parsed: Record<string, unknown>): boolean
});
}

// Issue #7285: an OpenAI-shape SSE stream that closes without ever emitting a
// chunk carrying `finish_reason` (and without a `data: [DONE]` sentinel) is a
// truncated response — combo failover needs to detect that shape independently
// of `hasOpenAICompatibleStreamValue()` (which only looks for *content*, not
// the terminal marker). Kept alongside the other shape-detection helpers so
// callers can distinguish "OpenAI-shape chunk seen" from "OpenAI-shape stream
// reached its terminal marker".
export function isOpenAIChoicesPayload(parsed: Record<string, unknown>): boolean {
return Array.isArray(parsed.choices);
}

export function hasOpenAIFinishReason(parsed: Record<string, unknown>): boolean {
if (!Array.isArray(parsed.choices)) return false;
return parsed.choices.some((choice) => isRecord(choice) && choice.finish_reason != null);
}
Comment on lines +327 to +334

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

To prevent potential runtime TypeError crashes when parsing malformed or unexpected SSE payloads (such as data: null which parses to null), we should defensively check that parsed is a non-null object before accessing its properties.

Suggested change
export function isOpenAIChoicesPayload(parsed: Record<string, unknown>): boolean {
return Array.isArray(parsed.choices);
}
export function hasOpenAIFinishReason(parsed: Record<string, unknown>): boolean {
if (!Array.isArray(parsed.choices)) return false;
return parsed.choices.some((choice) => isRecord(choice) && choice.finish_reason != null);
}
export function isOpenAIChoicesPayload(parsed: Record<string, unknown>): boolean {
return !!parsed && Array.isArray(parsed.choices);
}
export function hasOpenAIFinishReason(parsed: Record<string, unknown>): boolean {
if (!parsed || !Array.isArray(parsed.choices)) return false;
return parsed.choices.some((choice) => isRecord(choice) && choice.finish_reason != null);
}


export function isKnownNonClaudeStreamPayload(
parsed: Record<string, unknown>,
eventType = ""
Expand Down
65 changes: 65 additions & 0 deletions tests/unit/combo-streaming-openai-no-finish-reason-7285.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import test from "node:test";
import assert from "node:assert/strict";

const { validateResponseQuality } = await import("../../open-sse/services/combo.ts");

const encoder = new TextEncoder();
const silentLog = { warn: () => {} };

function sseStream(body: string): ReadableStream<Uint8Array> {
return new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(encoder.encode(body));
controller.close();
},
});
}

// OpenAI-shape stream: single role-only delta chunk, then the connection
// closes. No finish_reason anywhere, no `data: [DONE]` sentinel.
function makeTruncatedOpenAiStream(): Response {
const body =
`data: ${JSON.stringify({
id: "chatcmpl-test-truncated",
object: "chat.completion.chunk",
choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }],
})}\n\n`;
return new Response(sseStream(body), {
status: 200,
headers: { "content-type": "text/event-stream" },
});
}

// Healthy OpenAI-shape stream: content delta + a chunk carrying
// finish_reason: "stop" — must keep passing through (#3399/#3685 contract).
function makeHealthyOpenAiStream(): Response {
const chunks = [
JSON.stringify({
id: "chatcmpl-test-healthy",
object: "chat.completion.chunk",
choices: [{ index: 0, delta: { role: "assistant", content: "Hello" }, finish_reason: null }],
}),
JSON.stringify({
id: "chatcmpl-test-healthy",
object: "chat.completion.chunk",
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
}),
];
const body = chunks.map((c) => `data: ${c}\n\n`).join("") + "data: [DONE]\n\n";
return new Response(sseStream(body), {
status: 200,
headers: { "content-type": "text/event-stream" },
});
}

test("#7285 RED: OpenAI-shape stream with role-only delta and NO finish_reason should fail over but currently passes as valid", async () => {
const res = makeTruncatedOpenAiStream();
const out = await validateResponseQuality(res, true, silentLog);
assert.equal(out.valid, false, "expected failover (valid:false)");
});

test("#7285 control: a healthy OpenAI stream ending with finish_reason still passes through (#3399/#3685 no-regression)", async () => {
const res = makeHealthyOpenAiStream();
const out = await validateResponseQuality(res, true, silentLog);
assert.equal(out.valid, true, "expected valid:true for a properly terminated stream");
});
Loading