Skip to content

Commit 92ce4f3

Browse files
fix: preserve streaming tool_calls when provider omits finish_reason
Some OpenAI-compatible providers (e.g., Evolink deepseek-v4-pro) send valid delta.tool_calls chunks but end the stream with data: [DONE] without a final finish_reason chunk. Previously, this caused an error that discarded all accumulated tool call blocks. Now we infer the stop reason from the accumulated content: toolUse if tool calls were parsed, stop otherwise. This matches the existing fallback logic that already promotes stop -> toolUse when tool calls are present. Fixes #97994
1 parent 4391041 commit 92ce4f3

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎src/llm/providers/openai-completions.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,29 @@ describe("openai-completions stop-reason tool-call guard", () => {
11561156
expect(toolCalls).toHaveLength(1);
11571157
});
11581158

1159+
it("preserves toolCall blocks when stream ends without finish_reason (data: [DONE])", async () => {
1160+
// Some OpenAI-compatible providers (e.g., Evolink deepseek-v4-pro) send
1161+
// valid delta.tool_calls chunks but end with `data: [DONE]` without a
1162+
// final `finish_reason` chunk. The tool calls should still be preserved.
1163+
mockChunksRef.chunks = [
1164+
makeToolCallChunk("call_1", "read", '{"path":"/tmp/foo.txt"}'),
1165+
// No finishChunk — stream ends naturally like `data: [DONE]`
1166+
];
1167+
1168+
const stream = streamOpenAICompletions(model, context, {
1169+
apiKey: "***",
1170+
});
1171+
const result = await stream.result();
1172+
1173+
expect(result.stopReason).toBe("toolUse");
1174+
const toolCalls = result.content.filter((b) => b.type === "toolCall");
1175+
expect(toolCalls).toHaveLength(1);
1176+
expect(toolCalls[0]).toMatchObject({
1177+
id: "call_1",
1178+
name: "read",
1179+
});
1180+
});
1181+
11591182
it("keeps buffered visible text before following tool calls", async () => {
11601183
mockChunksRef.chunks = [
11611184
makeTextChunk("Use <"),

‎src/llm/providers/openai-completions.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,25 @@ export const streamOpenAICompletions: StreamFunction<
499499
if (output.stopReason === "error") {
500500
throw new Error(output.errorMessage || "Provider returned an error stop reason");
501501
}
502-
if (!hasFinishReason) {
503-
throw new Error("Stream ended without finish_reason");
504-
}
505-
506502
const hasToolCalls = output.content.some((block) => block.type === "toolCall");
507503
const hasVisibleText = output.content.some(
508504
(block) => block.type === "text" && block.text.trim().length > 0,
509505
);
506+
507+
// Some OpenAI-compatible providers end the stream with `data: [DONE]`
508+
// without emitting a final `finish_reason`. When that happens we still
509+
// have the accumulated content blocks, so we can infer the stop reason
510+
// instead of discarding everything with an error.
511+
if (!hasFinishReason) {
512+
if (hasToolCalls) {
513+
output.stopReason = "toolUse";
514+
} else if (hasVisibleText) {
515+
output.stopReason = "stop";
516+
} else {
517+
output.stopReason = "stop";
518+
}
519+
}
520+
510521
if (output.stopReason === "toolUse" && !hasToolCalls) {
511522
output.stopReason = "stop";
512523
}

0 commit comments

Comments
 (0)