diff --git a/changelog.d/fixes/7285-combo-finish-reason.md b/changelog.d/fixes/7285-combo-finish-reason.md new file mode 100644 index 000000000000..71515e48ca95 --- /dev/null +++ b/changelog.d/fixes/7285-combo-finish-reason.md @@ -0,0 +1 @@ +- fix(sse): combo failover now detects OpenAI-shape streams truncated without `finish_reason`/`[DONE]` (#7285) diff --git a/open-sse/services/combo/validateQuality.ts b/open-sse/services/combo/validateQuality.ts index 7f1a32b38a3a..99805eb35ca4 100644 --- a/open-sse/services/combo/validateQuality.ts +++ b/open-sse/services/combo/validateQuality.ts @@ -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"; @@ -98,6 +100,29 @@ function messageDeltaEndsLifecycle(parsed: Record): 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, + 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 @@ -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 = ""; @@ -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; try { @@ -268,6 +301,8 @@ export async function validateResponseQuality( continue; } + applyOpenAiLifecycleEvent(parsed, openAi); + const eventType = (typeof parsed.type === "string" ? parsed.type : null) || pendingEventType || ""; pendingEventType = ""; @@ -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. diff --git a/open-sse/utils/streamHelpers.ts b/open-sse/utils/streamHelpers.ts index 1f4b5f919820..2f25a13ab8c1 100644 --- a/open-sse/utils/streamHelpers.ts +++ b/open-sse/utils/streamHelpers.ts @@ -317,6 +317,22 @@ function hasGeminiCandidateStreamValue(parsed: Record): 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): boolean { + return Array.isArray(parsed.choices); +} + +export function hasOpenAIFinishReason(parsed: Record): boolean { + if (!Array.isArray(parsed.choices)) return false; + return parsed.choices.some((choice) => isRecord(choice) && choice.finish_reason != null); +} + export function isKnownNonClaudeStreamPayload( parsed: Record, eventType = "" diff --git a/tests/unit/combo-streaming-openai-no-finish-reason-7285.test.ts b/tests/unit/combo-streaming-openai-no-finish-reason-7285.test.ts new file mode 100644 index 000000000000..0f7c5cdf2e49 --- /dev/null +++ b/tests/unit/combo-streaming-openai-no-finish-reason-7285.test.ts @@ -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 { + return new ReadableStream({ + 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"); +});