Repository navigation
Description
Summary
OpenAI completions stream closed before a finish_reason was received is thrown by the openai-completions provider consumer when the SSE stream ends after already emitting assistant content, but never delivered a terminal finish_reason.
Where it comes from
packages/ai/src/providers/openai-completions.ts, in streamOpenAICompletionsOnce (the for await loop that drains the OpenAI-compatible chat-completions stream):
// Detect premature stream closure before the normal block-finalization
// sweep. Throwing after that sweep would make the error handler emit a
// second text_end/thinking_end for the same partial block.
if (streamFinishedAt === undefined && output.content.length > 0) {
throw new AIError.ProviderResponseError(
"OpenAI completions stream closed before a finish_reason was received",
{ provider: model.provider, kind: "incomplete-stream" },
);
}streamFinishedAt is only set when a chunk carries a truthy finish_reason:
if (choice.finish_reason) {
const finishReasonResult = mapStopReason(choice.finish_reason);
output.stopReason = finishReasonResult.stopReason;
if (finishReasonResult.errorMessage) {
output.errorMessage = finishReasonResult.errorMessage;
}
streamFinishedAt ??= Date.now();
}So the error fires when both:
- the consumer never observed a truthy
finish_reason(streamFinishedAt === undefined), and - at least one content block (text / thinking / tool call) was already pushed to
output.content.
It is deliberately excluded when no content was emitted — that path is left to withEmptyCompletionRetry.
The error is a ProviderResponseError with kind: "incomplete-stream", which is classified as Flag.Transient and therefore auto-retried by the session retry gate; it only surfaces as a user-visible stopReason: "error" after retries are exhausted.
Trigger conditions (all confirmed driving the exact message via a repro harness)
The stream loop can end with streamFinishedAt === undefined and content present in four distinct ways:
1. Mid-stream transport drop (the "real" truncation)
The HTTP body / SSE connection closes (EOF) after partial content but before any terminal chunk — upstream crash, network reset, proxy/gateway timeout, max_concurrency admission shedding, etc. Old behavior (before this guard was added) silently finalized the partial message as a clean stop, so the agent loop treated a truncated turn as complete.
2. Server sends [DONE] but never a finish_reason chunk
A non-compliant OpenAI-compatible host streamed content deltas and then emitted the [DONE] sentinel without a terminal finish_reason chunk. readSseJson returns on [DONE], ending the loop with streamFinishedAt still undefined → error.
3. Final chunk carries finish_reason: null (suspected consumer false-positive)
Many OpenAI-compatible servers emit "finish_reason": null on every content chunk (it is null while still streaming) and then terminate. If the last chunk is finish_reason: null and the stream then ends, the truthiness guard if (choice.finish_reason) treats null as absent, so streamFinishedAt never flips → error.
Notably, mapStopReason(null) explicitly returns { stopReason: "stop" } — but that branch is dead because the caller only invokes mapStopReason when finish_reason is truthy. This strongly suggests the ORIGINAL intent was that a null/omitted finish on an otherwise-complete stream should be a clean stop, not an incomplete-stream error.
4. Terminal chunk with empty choices: []
If the host ends with a {choices: []} chunk (no finish_reason) and content was already emitted, the !choice branch only breaks when streamFinishedAt !== undefined; with content present and no finish reason it falls through to the guard → error.
5. Model routed to the wrong transport (known case #8957)
A model that only supports the OpenAI Responses API but is resolved to openai-completions. The /chat/completions route closes the stream with no finish_reason on every tool-call turn. Fixed for the specific opencode-go muse-spark-1.2 / deepseek-v4-flash models by pinning them to openai-responses (OPENCODE_GO_API_ID_OVERRIDES) — but the underlying symptom (a wrong-transport stream closing without a finish reason) is the same.
Repro
Using streamOpenAICompletions with a mocked fetch, each of the following SSE payloads produces the exact error (content present, no truthy finish_reason):
// 1. EOF after content, no finish_reason
[{ choices: [{ delta: { content: "Hel" } }] },
{ choices: [{ delta: { content: "lo" } }] }]
// 2. [DONE] after content, no finish_reason chunk
[{ choices: [{ delta: { content: "Hel" } }] },
{ choices: [{ delta: { content: "lo" } }] },
"[DONE]"]
// 3. final chunk finish_reason:null
[{ choices: [{ delta: { content: "Hel" } }] },
{ choices: [{ delta: {}, finish_reason: null }] },
"[DONE]"]
// 4. final chunk empty choices:[]
[{ choices: [{ delta: { content: "Hel" } }] },
{ choices: [] },
"[DONE]"]A stream that genuinely finishes with finish_reason: "stop"/"length"/"tool_calls" does not error, and a stream with no content and no finish reason goes down the withEmptyCompletionRetry path instead (no error).
Why this is worth a follow-up
The guard was added to convert a silent mid-sentence halt into an error (so the agent loop doesn't treat a truncated turn as complete) — that's correct for the transport-drop case. But case 2 / 3 / 4 are non-compliant-but-complete streams: the server delivered a deliberate [DONE] (or a terminal chunk) and simply omitted (or nulled) the finish_reason. Treating those as an error currently surfaces a confusing stopReason: "error" + retry on every turn against such hosts, and mapStopReason(null) => "stop" being dead code indicates the consumer may be too strict.
Suggested direction
- When
[DONE]is observed, the stream ended by server agreement — a missing/nullfinish_reasonon an otherwise-complete stream should not be classified as truncated (could default tostop, or a distinct "missing finish reason" diagnostic). - Fix the truthiness guard so
finish_reason: nullreachesmapStopReason(null) => "stop"(or keep waiting for the real terminal reason) rather than being dropped. - Keep the strict error only for the genuinely-truncated case (EOF with no
[DONE]and no finish reason). - For the wrong-transport class (muse spark 1.2 from opencode go repeatedly throws errors #8957), the per-model routing overrides are a band-aid; the underlying issue is that a stream that closes without a finish reason gives no signal about which transport was correct.
Activity
roboomp commentedon Aug 23, 2026
Looking into this. Will build a repro harness for the
[DONE]-terminated-without-finish_reasoncases and report back.roboomp commentedon Aug 23, 2026
Reproduced. Harness feeds mocked SSE through
streamOpenAICompletionsand reads the resolvedstopReason:Cases 2/3/4 all reach the
[DONE]sentinel — the streaming protocol's terminal signal — yet were classified as truncated. Case 1 (transport EOF, no[DONE]) is the only genuine truncation. Fixing by making the guard[DONE]-aware: track whether[DONE]was observed and throw incomplete-stream only when it was not. Your suggested direction 2 (routingfinish_reason: nulltomapStopReason) is unsafe on its own since many hosts streamfinish_reason: nullon every content chunk;[DONE]is the reliable discriminator. PR incoming.roboomp commentedon Aug 23, 2026
Fix up in #9434. The guard now honors the
[DONE]sentinel:[DONE]-terminated streams finalize as a clean stop (tool-call turns promote totoolUse), and the incomplete-stream error fires only on a genuine transport EOF with no[DONE]and no finish reason.fix(ai): honor [DONE] sentinel as clean completion in openai-completions
Triumph1701 commentedon Aug 23, 2026
The distinction between a transport-truncated stream and a protocol-complete stream that omits finish_reason seems important. It may help to model stream termination as an evidence state (EOF, [DONE], terminal choice) and classify retries from that, rather than using truthiness of finish_reason as the sole signal. For the wrong-transport case, a provider capability/endpoint matrix could prevent routing a Responses-only model to Chat Completions before the request starts.
roboomp commentedon Aug 23, 2026
This issue is closed. If the bug is back, please reopen and I'll triage again from scratch.
fix(ai): honor [DONE] sentinel as clean completion in openai-completions