Skip to content

Commit 2d2ef85

Browse files
fix(adapters): preserve upstream finish_reason as stopReason in openai-chat adapter
The openai-chat adapter collapsed all non-empty finish_reason values into a boolean sawFinish, discarding the original reason. The bridge already maps stopReason=max_tokens to response.incomplete (issue #246), but the adapter never provided the value. This means providers like ark-coding/glm-5.2 that return finish_reason=length when the model hits its 4096 output token cap were silently treated as normal completions - Codex received task_complete on truncated responses. - Replace sawFinish boolean with finishReason string in streaming path - Propagate finish_reason as stopReason on all done events (streaming + non-streaming) - Preserve [DONE] without finish_reason -> stopReason=undefined (backward compat) - Preserve EOF fail-closed behavior: no finish_reason + no usage -> error - 15 tests covering stop/length/max_tokens/tool_calls/content_filter/EOF scenarios
1 parent 9eb3df5 commit 2d2ef85

2 files changed

Lines changed: 174 additions & 7 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
604604
// being reported as a clean completion (silent truncation). A graceful close is either an
605605
// explicit `[DONE]` sentinel OR a chunk carrying a non-null `finish_reason` (some
606606
// OpenAI-compatible providers omit `[DONE]` but do send finish_reason).
607-
let sawFinish = false;
607+
let finishReason: string | undefined;
608608

609609
// Single per-line handler shared by the streaming loop and the EOF residual-frame flush, so
610610
// a final frame is parsed identically wherever it lands (no duplicated, drift-prone parsing).
@@ -615,7 +615,7 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
615615
const payload = line.slice(6).trim();
616616
if (payload === "[DONE]") {
617617
yield* flushToolCalls();
618-
yield { type: "done", usage: pendingUsage };
618+
yield { type: "done", usage: pendingUsage, ...(finishReason ? { stopReason: finishReason } : {}) };
619619
return "terminate";
620620
}
621621

@@ -649,7 +649,7 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
649649
// Observe the terminator BEFORE the delta guard: a finish-only chunk (finish_reason set,
650650
// no delta) is a graceful close and must mark sawFinish even though we skip it below.
651651
if (typeof choices[0].finish_reason === "string" && choices[0].finish_reason) {
652-
sawFinish = true;
652+
finishReason = choices[0].finish_reason;
653653
}
654654
const delta = choices[0].delta;
655655
if (delta) {
@@ -717,15 +717,15 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
717717
}
718718
yield* flushToolCalls();
719719
// Reader EOF. A graceful close shows at least one terminal signal: `[DONE]` (returns above),
720-
// a non-null finish_reason (sawFinish), or a trailing usage chunk (providers emit usage only
720+
// a non-null finish_reason (finishReason), or a trailing usage chunk (providers emit usage only
721721
// at end-of-generation). If NONE of those were seen, the stream was cut mid-flight — fail
722722
// closed so the bridge emits a classified response.failed rather than a silent truncation.
723-
if (!sawFinish && pendingUsage === undefined) {
723+
if (!finishReason && pendingUsage === undefined) {
724724
yield { type: "error", message: "upstream stream ended without a terminal signal ([DONE] or finish_reason) — possible truncation" };
725725
return;
726726
}
727727
// Graceful close that omitted [DONE] but delivered finish_reason and/or final usage.
728-
yield { type: "done", usage: pendingUsage };
728+
yield { type: "done", usage: pendingUsage, ...(finishReason ? { stopReason: finishReason } : {}) };
729729
} finally {
730730
reader.releaseLock();
731731
}
@@ -742,7 +742,7 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
742742
}
743743

744744
const events: AdapterEvent[] = [];
745-
const choices = json.choices as { message?: Record<string, unknown> }[] | undefined;
745+
const choices = json.choices as { message?: Record<string, unknown>; finish_reason?: string }[] | undefined;
746746
if (!Array.isArray(choices) || choices.length === 0 || !choices[0].message) {
747747
return [{ type: "error", message: "upstream response contained no choices" }];
748748
}
@@ -766,6 +766,7 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
766766
events.push({
767767
type: "done",
768768
usage: usageFromOpenAIChat(usage),
769+
...(choices[0].finish_reason ? { stopReason: choices[0].finish_reason } : {}),
769770
});
770771
return events;
771772
},
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { createOpenAIChatAdapter } from "../src/adapters/openai-chat";
3+
import type { AdapterEvent } from "../src/types";
4+
5+
const provider = { adapter: "openai-chat", baseUrl: "https://example.test/v1", apiKey: "key" };
6+
7+
async function collect(gen: AsyncGenerator<AdapterEvent>): Promise<AdapterEvent[]> {
8+
const out: AdapterEvent[] = [];
9+
for await (const e of gen) out.push(e);
10+
return out;
11+
}
12+
13+
function lastDone(events: AdapterEvent[]): { type: "done"; stopReason?: string } | undefined {
14+
return events.find(e => e.type === "done") as { type: "done"; stopReason?: string } | undefined;
15+
}
16+
17+
describe("openai-chat streaming finish_reason -> stopReason propagation", () => {
18+
test("finish_reason=stop -> done with stopReason=stop", async () => {
19+
const response = new Response('data: {"choices":[{"delta":{"content":"hi"},"finish_reason":"stop"}]}\n\n');
20+
const events = await collect(createOpenAIChatAdapter(provider).parseStream(response));
21+
const done = lastDone(events);
22+
expect(done).toBeDefined();
23+
expect(done!.stopReason).toBe("stop");
24+
});
25+
26+
test("finish_reason=length -> done with stopReason=length", async () => {
27+
const response = new Response('data: {"choices":[{"delta":{"content":"truncated"},"finish_reason":"length"}]}\n\n');
28+
const events = await collect(createOpenAIChatAdapter(provider).parseStream(response));
29+
const done = lastDone(events);
30+
expect(done).toBeDefined();
31+
expect(done!.stopReason).toBe("length");
32+
});
33+
34+
test("finish_reason=max_tokens -> done with stopReason=max_tokens", async () => {
35+
const response = new Response('data: {"choices":[{"delta":{"content":"x"},"finish_reason":"max_tokens"}]}\n\n');
36+
const events = await collect(createOpenAIChatAdapter(provider).parseStream(response));
37+
const done = lastDone(events);
38+
expect(done).toBeDefined();
39+
expect(done!.stopReason).toBe("max_tokens");
40+
});
41+
42+
test("finish_reason=tool_calls -> done with stopReason=tool_calls", async () => {
43+
const response = new Response('data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","function":{"name":"f","arguments":"{}"}}]},"finish_reason":"tool_calls"}]}\n\n');
44+
const events = await collect(createOpenAIChatAdapter(provider).parseStream(response));
45+
const done = lastDone(events);
46+
expect(done).toBeDefined();
47+
expect(done!.stopReason).toBe("tool_calls");
48+
});
49+
50+
test("finish_reason=content_filter -> done with stopReason=content_filter", async () => {
51+
const response = new Response('data: {"choices":[{"delta":{"content":"filtered"},"finish_reason":"content_filter"}]}\n\n');
52+
const events = await collect(createOpenAIChatAdapter(provider).parseStream(response));
53+
const done = lastDone(events);
54+
expect(done).toBeDefined();
55+
expect(done!.stopReason).toBe("content_filter");
56+
});
57+
58+
test("[DONE] after finish_reason=length preserves the captured stopReason", async () => {
59+
const response = new Response([
60+
'data: {"choices":[{"delta":{"content":"hi"},"finish_reason":"length"}]}\n\n',
61+
"data: [DONE]\n\n",
62+
].join(""));
63+
const events = await collect(createOpenAIChatAdapter(provider).parseStream(response));
64+
const done = lastDone(events);
65+
expect(done).toBeDefined();
66+
expect(done!.stopReason).toBe("length");
67+
});
68+
69+
test("[DONE] without any finish_reason -> done with stopReason=undefined", async () => {
70+
const response = new Response([
71+
'data: {"choices":[{"delta":{"content":"hi"}}]}\n\n',
72+
"data: [DONE]\n\n",
73+
].join(""));
74+
const events = await collect(createOpenAIChatAdapter(provider).parseStream(response));
75+
const done = lastDone(events);
76+
expect(done).toBeDefined();
77+
expect(done!.stopReason).toBeUndefined();
78+
});
79+
80+
test("EOF without finish_reason and without [DONE] -> error (not done)", async () => {
81+
const response = new Response('data: {"choices":[{"delta":{"content":"par"}}]}\n\n');
82+
const events = await collect(createOpenAIChatAdapter(provider).parseStream(response));
83+
const done = events.find(e => e.type === "done");
84+
expect(done).toBeUndefined();
85+
expect(events.at(-1)?.type).toBe("error");
86+
});
87+
88+
test("usage-only final frame without finish_reason -> done with stopReason=undefined", async () => {
89+
const response = new Response(
90+
'data: {"choices":[{"delta":{"content":"hi"}}]}\n\n' +
91+
'data: {"choices":[],"usage":{"prompt_tokens":1,"completion_tokens":2,"total_tokens":3}}'
92+
);
93+
const events = await collect(createOpenAIChatAdapter(provider).parseStream(response));
94+
const done = lastDone(events);
95+
expect(done).toBeDefined();
96+
expect(done!.stopReason).toBeUndefined();
97+
});
98+
});
99+
100+
describe("openai-chat non-streaming finish_reason -> stopReason propagation", () => {
101+
test("finish_reason=stop -> done with stopReason=stop", async () => {
102+
const json = JSON.stringify({
103+
choices: [{ message: { content: "hi" }, finish_reason: "stop" }],
104+
usage: { prompt_tokens: 5, completion_tokens: 2, total_tokens: 7 },
105+
});
106+
const events = await createOpenAIChatAdapter(provider).parseResponse(new Response(json));
107+
const done = lastDone(events);
108+
expect(done).toBeDefined();
109+
expect(done!.stopReason).toBe("stop");
110+
});
111+
112+
test("finish_reason=length -> done with stopReason=length", async () => {
113+
const json = JSON.stringify({
114+
choices: [{ message: { content: "truncated" }, finish_reason: "length" }],
115+
usage: { prompt_tokens: 5, completion_tokens: 4096, total_tokens: 4101 },
116+
});
117+
const events = await createOpenAIChatAdapter(provider).parseResponse(new Response(json));
118+
const done = lastDone(events);
119+
expect(done).toBeDefined();
120+
expect(done!.stopReason).toBe("length");
121+
});
122+
123+
test("finish_reason=max_tokens -> done with stopReason=max_tokens", async () => {
124+
const json = JSON.stringify({
125+
choices: [{ message: { content: "truncated" }, finish_reason: "max_tokens" }],
126+
usage: { prompt_tokens: 5, completion_tokens: 4096, total_tokens: 4101 },
127+
});
128+
const events = await createOpenAIChatAdapter(provider).parseResponse(new Response(json));
129+
const done = lastDone(events);
130+
expect(done).toBeDefined();
131+
expect(done!.stopReason).toBe("max_tokens");
132+
});
133+
134+
test("finish_reason=content_filter -> done with stopReason=content_filter", async () => {
135+
const json = JSON.stringify({
136+
choices: [{ message: { content: "" }, finish_reason: "content_filter" }],
137+
usage: { prompt_tokens: 5, completion_tokens: 0, total_tokens: 5 },
138+
});
139+
const events = await createOpenAIChatAdapter(provider).parseResponse(new Response(json));
140+
const done = lastDone(events);
141+
expect(done).toBeDefined();
142+
expect(done!.stopReason).toBe("content_filter");
143+
});
144+
145+
test("finish_reason=tool_calls -> done with stopReason=tool_calls", async () => {
146+
const json = JSON.stringify({
147+
choices: [{ message: { tool_calls: [{ id: "c1", function: { name: "f", arguments: "{}" } }] }, finish_reason: "tool_calls" }],
148+
usage: { prompt_tokens: 5, completion_tokens: 10, total_tokens: 15 },
149+
});
150+
const events = await createOpenAIChatAdapter(provider).parseResponse(new Response(json));
151+
const done = lastDone(events);
152+
expect(done).toBeDefined();
153+
expect(done!.stopReason).toBe("tool_calls");
154+
});
155+
156+
test("missing finish_reason -> done with stopReason=undefined", async () => {
157+
const json = JSON.stringify({
158+
choices: [{ message: { content: "no finish reason" } }],
159+
usage: { prompt_tokens: 5, completion_tokens: 3, total_tokens: 8 },
160+
});
161+
const events = await createOpenAIChatAdapter(provider).parseResponse(new Response(json));
162+
const done = lastDone(events);
163+
expect(done).toBeDefined();
164+
expect(done!.stopReason).toBeUndefined();
165+
});
166+
});

0 commit comments

Comments
 (0)