From 8656838a5b8f7d9066856e8860bad470063ee307 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Tue, 22 Sep 2026 21:23:09 -0500 Subject: [PATCH] fix(ai): ignore bare null SSE frames (#50793) --- packages/ai/src/protocols/shared.ts | 14 ++++++---- .../provider/openai-compatible-chat.test.ts | 21 +++++++++++++++ .../openai-compatible-responses.test.ts | 27 +++++++++++++++++++ packages/ai/test/schema.test.ts | 14 ++++++++++ 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/packages/ai/src/protocols/shared.ts b/packages/ai/src/protocols/shared.ts index 61c2130e75a..b96333fb096 100644 --- a/packages/ai/src/protocols/shared.ts +++ b/packages/ai/src/protocols/shared.ts @@ -231,11 +231,11 @@ export const errorText = (error: unknown) => { /** * `framing` step for Server-Sent Events. Decodes UTF-8, runs the SSE channel - * decoder, optionally filters named events, and drops empty events. `[DONE]` - * is dropped by default or retained for protocols that use it as their stream - * boundary. Retry control events are ignored without interrupting the stream. - * Decoder failures become provider output errors so the public error channel - * stays `AIError`. + * decoder, optionally filters named events, and drops empty and bare `null` + * events. `[DONE]` is dropped by default or retained for protocols that use it + * as their stream boundary. Retry control events are ignored without + * interrupting the stream. Decoder failures become provider output errors so + * the public error channel stays `AIError`. */ export const sseFraming = ( bytes: Stream.Stream, @@ -265,6 +265,10 @@ export const sseFraming = ( (event) => (events === undefined || events.has(event.event)) && event.data.length > 0 && + // Some OpenAI-compatible proxies serialize an empty flush as a bare + // `data: null`, between events or after `[DONE]`. No protocol has a + // null event, so it carries nothing and must not abort the stream. + event.data !== "null" && (event.data !== "[DONE]" || includeDone || (events !== undefined && event.event !== "message")), ), Stream.map((event) => event.data), diff --git a/packages/ai/test/provider/openai-compatible-chat.test.ts b/packages/ai/test/provider/openai-compatible-chat.test.ts index 8e604aaba7a..32cabda1f7c 100644 --- a/packages/ai/test/provider/openai-compatible-chat.test.ts +++ b/packages/ai/test/provider/openai-compatible-chat.test.ts @@ -375,6 +375,27 @@ describe("OpenAI-compatible Chat route", () => { }), ) + it.effect("ignores bare null frames between deltas", () => + Effect.gen(function* () { + const response = yield* LLMClient.generate(request).pipe( + Effect.provide( + fixedResponse( + sseEvents( + deltaChunk({ role: "assistant", content: "Hello" }), + "null", + deltaChunk({ content: "!" }), + "null", + deltaChunk({}, "stop"), + ), + ), + ), + ) + + expect(response.text).toBe("Hello!") + expect(response.finishReason).toEqual({ normalized: "stop", raw: "stop" }) + }), + ) + it.effect("accepts nullable usage and preserves provider fields", () => Effect.gen(function* () { const response = yield* LLMClient.generate(request).pipe( diff --git a/packages/ai/test/provider/openai-compatible-responses.test.ts b/packages/ai/test/provider/openai-compatible-responses.test.ts index 4c41adc0c51..2c6cbaa9ea7 100644 --- a/packages/ai/test/provider/openai-compatible-responses.test.ts +++ b/packages/ai/test/provider/openai-compatible-responses.test.ts @@ -393,6 +393,33 @@ describe("Open Responses-compatible route", () => { }), ) + it.effect("ignores bare null frames between events", () => + Effect.gen(function* () { + const model = configure({ + apiKey: "test-key", + baseURL: "https://responses.example.test/v1", + }).model("example-model") + const response = yield* LLMClient.generate(LLM.request({ model, prompt: "Say hello." })).pipe( + Effect.provide( + fixedResponse( + sseEvents( + { type: "response.output_item.added", output_index: 0, item: { type: "message", id: "msg_1" } }, + "null", + { type: "response.output_text.delta", output_index: 0, item_id: "msg_1", delta: "Hello" }, + "null", + { type: "response.output_item.done", output_index: 0, item: { type: "message", id: "msg_1" } }, + { type: "response.completed", response: { id: "resp_1" } }, + "null", + ), + ), + ), + ) + + expect(response.text).toBe("Hello") + expect(response.events.at(-1)).toMatchObject({ type: "finish" }) + }), + ) + describe("stream validation", () => { const request = LLM.request({ model: configure({ apiKey: "test-key", baseURL: "https://responses.example.test/v1" }).model("example-model"), diff --git a/packages/ai/test/schema.test.ts b/packages/ai/test/schema.test.ts index 10a45d01237..4a025f6c535 100644 --- a/packages/ai/test/schema.test.ts +++ b/packages/ai/test/schema.test.ts @@ -178,6 +178,20 @@ describe("AI.Usage", () => { }), ) + it.effect("sseFraming drops bare null frames and keeps other payloads", () => + Effect.gen(function* () { + const frames = yield* ProviderShared.sseFraming( + Stream.make( + new TextEncoder().encode( + 'data: {"first":true}\n\ndata: null\n\nevent: update\ndata: null\n\ndata: "null"\n\ndata: 0\n\ndata: {"second":true}\n\ndata: [DONE]\n\ndata: null\n\n', + ), + ), + ).pipe(Stream.runCollect) + + expect(Array.from(frames)).toEqual(['{"first":true}', '"null"', "0", '{"second":true}']) + }), + ) + test("visibleOutputTokens clamps reasoning > output to zero", () => { expect(new Usage({ outputTokens: 10, reasoningTokens: 4 }).visibleOutputTokens).toBe(6) expect(new Usage({ outputTokens: 10 }).visibleOutputTokens).toBe(10)