fix(ai): openai-completions - throw error on missing finish-reason

- require \ before treating \ streams as successful
- add regression coverage for truncated streams without \
- closes #4345
This commit is contained in:
Ramiz Wachtler
2026-05-15 15:11:43 +02:00
parent a8af0b5e99
commit 98ffad0437
3 changed files with 38 additions and 0 deletions

View File

@@ -165,6 +165,7 @@ export const streamOpenAICompletions: StreamFunction<"openai-completions", OpenA
let textBlock: TextContent | null = null;
let thinkingBlock: ThinkingContent | null = null;
let hasFinishReason = false;
const toolCallBlocksByIndex = new Map<number, StreamingToolCallBlock>();
const toolCallBlocksById = new Map<string, StreamingToolCallBlock>();
const blocks = output.content as StreamingBlock[];
@@ -288,6 +289,7 @@ export const streamOpenAICompletions: StreamFunction<"openai-completions", OpenA
if (finishReasonResult.errorMessage) {
output.errorMessage = finishReasonResult.errorMessage;
}
hasFinishReason = true;
}
if (choice.delta) {
@@ -390,6 +392,9 @@ export const streamOpenAICompletions: StreamFunction<"openai-completions", OpenA
if (output.stopReason === "error") {
throw new Error(output.errorMessage || "Provider returned an error stop reason");
}
if (!hasFinishReason) {
throw new Error("Stream ended without finish_reason");
}
stream.push({ type: "done", reason: output.stopReason, message: output });
stream.end();