Repository navigation
fix(sse): combo failover for OpenAI streams truncated without finish_reason #7568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
tests/unit/combo-streaming-openai-no-finish-reason-7285.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent potential runtime
TypeErrorcrashes when parsing malformed or unexpected SSE payloads (such asdata: nullwhich parses tonull), we should defensively check thatparsedis a non-null object before accessing its properties.