Skip to content

Commit 5718d44

Browse files
authored
Merge pull request #773 from Wibias/fix/735-openai-chat-eof
fix(openai-chat): complete streams that end after output without terminal
2 parents be177ea + 38173bf commit 5718d44

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
660660
const pendingToolCalls: PendingToolCall[] = [];
661661
let toolCallSeq = 0;
662662
const flushToolCalls = function* (): Generator<AdapterEvent> {
663+
if (pendingToolCalls.length > 0) sawOutput = true;
663664
for (const call of pendingToolCalls) {
664665
if (!call.id) call.id = `call_${++toolCallSeq}`;
665666
yield { type: "tool_call_start", id: call.id, name: call.name };
@@ -674,6 +675,7 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
674675
// explicit `[DONE]` sentinel OR a chunk carrying a non-null `finish_reason` (some
675676
// OpenAI-compatible providers omit `[DONE]` but do send finish_reason).
676677
let finishReason: string | undefined;
678+
let sawOutput = false;
677679

678680
// Single per-line handler shared by the streaming loop and the EOF residual-frame flush, so
679681
// a final frame is parsed identically wherever it lands (no duplicated, drift-prone parsing).
@@ -740,9 +742,11 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
740742
const delta = choices[0].delta;
741743
if (delta) {
742744
if (typeof delta.reasoning_content === "string" && delta.reasoning_content.length > 0) {
745+
sawOutput = true;
743746
yield { type: "reasoning_raw_delta", text: delta.reasoning_content };
744747
}
745748
if (typeof delta.content === "string" && delta.content.length > 0) {
749+
sawOutput = true;
746750
yield { type: "text_delta", text: delta.content };
747751
}
748752

@@ -800,13 +804,23 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
800804
if (buffer.length > 0) {
801805
if ((yield* handleDataLine(buffer)) === "terminate") return;
802806
}
807+
const hadPendingToolCallsAtEof = pendingToolCalls.length > 0;
803808
yield* flushToolCalls();
804809
// Reader EOF. A graceful close shows at least one terminal signal: `[DONE]` (returns above),
805810
// a non-null finish_reason (sawFinish), or a trailing usage chunk (providers emit usage only
806811
// at end-of-generation). If NONE of those were seen, the stream was cut mid-flight — fail
807812
// closed so the bridge emits a classified response.failed rather than a silent truncation.
808813
const sawFinish = finishReason !== undefined;
809-
if (!sawFinish && pendingUsage === undefined) {
814+
if (!sawFinish && pendingUsage === undefined && hadPendingToolCallsAtEof) {
815+
debugProviderDiagnostic("openai-chat", "stream-truncated", {
816+
finishReason: finishReason ?? null,
817+
hadUsage: false,
818+
pendingToolCalls: true,
819+
});
820+
yield { type: "error", message: "upstream stream ended without a terminal signal ([DONE] or finish_reason) - possible truncation" };
821+
return;
822+
}
823+
if (!sawFinish && pendingUsage === undefined && !sawOutput) {
810824
debugProviderDiagnostic("openai-chat", "stream-truncated", {
811825
finishReason: finishReason ?? null,
812826
hadUsage: false,

‎tests/openai-chat-eof.test.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ async function collect(gen: AsyncGenerator<AdapterEvent>): Promise<AdapterEvent[
1212
}
1313

1414
describe("openai-chat stream EOF fail-closed", () => {
15-
test("truncated stream (no [DONE], no finish_reason) yields a terminal error, not a clean done", async () => {
15+
test("truncated stream (no [DONE], no finish_reason) yields done when content was emitted", async () => {
1616
const response = new Response('data: {"choices":[{"delta":{"content":"par"}}]}\n\n');
1717
const events = await collect(createOpenAIChatAdapter(provider).parseStream(response));
1818
const last = events[events.length - 1];
19-
expect(last.type).toBe("error");
19+
expect(last.type).toBe("done");
20+
expect(events.some(e => e.type === "error")).toBe(false);
21+
});
22+
23+
test("empty EOF without content still errors", async () => {
24+
const response = new Response("");
25+
const events = await collect(createOpenAIChatAdapter(provider).parseStream(response));
26+
expect(events.at(-1)?.type).toBe("error");
2027
expect(events.some(e => e.type === "done")).toBe(false);
2128
});
2229

@@ -128,11 +135,11 @@ describe("openai-chat stream EOF fail-closed", () => {
128135
expect(events.some(e => e.type === "error")).toBe(false);
129136
});
130137

131-
test("genuinely truncated stream WITHOUT a trailing newline still fails closed", async () => {
132-
// Mid-content frame, no terminator, no newline — must remain a terminal error.
138+
test("genuinely truncated stream WITHOUT a trailing newline completes when content was emitted", async () => {
139+
// Mid-content frame, no terminator, no newline — content was yielded, so accept done.
133140
const response = new Response('data: {"choices":[{"delta":{"content":"par"}}]}');
134141
const events = await collect(createOpenAIChatAdapter(provider).parseStream(response));
135-
expect(events.at(-1)?.type).toBe("error");
136-
expect(events.some(e => e.type === "done")).toBe(false);
142+
expect(events.at(-1)?.type).toBe("done");
143+
expect(events.some(e => e.type === "error")).toBe(false);
137144
});
138145
});

0 commit comments

Comments
 (0)