Repository navigation
fix(ai): honor [DONE] sentinel as clean completion in openai-completions - #9434
Conversation
OpenAI-compatible hosts that stream content then terminate with the `[DONE]` sentinel while omitting or nulling `finish_reason` were misclassified as truncated streams, failing every turn with an incomplete-stream error that only surfaces after retry exhaustion. Track the `[DONE]` sentinel via an always-installed SSE observer and raise the incomplete-stream error only on a genuine transport EOF (no `[DONE]` and no finish reason). `[DONE]` is the streaming protocol's terminal signal, so a stream ending with it completed by server agreement and now finalizes as the default stop (mapStopReason(null)). Fixes #9433
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5e59c4fb3c
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const result = await run(frames); | ||
| expect(result.stopReason).toBe("stop"); | ||
| expect(result.errorMessage).toBeUndefined(); | ||
| expect(result.text.length).toBeGreaterThan(0); |
There was a problem hiding this comment.
Assert the preserved completion text
The assertion result.text.length > 0 does not verify the test's stated content-preservation contract: a regression that truncates "Hello" to a single character would still pass. Include the expected reconstructed text in each fixture and assert it, or remove the preservation claim; repository guidance explicitly prohibits non-empty-output assertions without a consumer contract.
AGENTS.md reference: AGENTS.md:L270-L270
Useful? React with 👍 / 👎.
|
Repro
A mocked-
fetchharness feeds SSE payloads throughstreamOpenAICompletionsand reads the resolvedstopReason. Any OpenAI-compatible host that streams content and then terminates withdata: [DONE]while omitting ornullingfinish_reasonresolves tostopReason: "error"withOpenAI completions stream closed before a finish_reason was received; only a genuine transport EOF (no[DONE]) is a real truncation.Cause
streamOpenAICompletionsOnceinpackages/ai/src/providers/openai-completions.tsflipsstreamFinishedAtonly on a truthychoice.finish_reason. The post-loop guard then threwProviderResponseError { kind: "incomplete-stream" }wheneverstreamFinishedAt === undefined && output.content.length > 0, without distinguishing a[DONE]-terminated stream (server-agreed completion) from a real transport EOF.readSseJsonreturns on both[DONE]and EOF, so the consumer had no completion signal — and the existingmapStopReason(null) => "stop"branch was dead code, confirming anull/omitted finish on an otherwise-complete stream was meant to be a clean stop.Fix
[DONE]sentinel in an always-installed SSE observer (sawDoneSentinel), independent of the caller'sonSseEvent.[DONE]was NOT observed (genuine EOF); a[DONE]-terminated stream falls through to normal finalization and keeps the defaultstop. Tool-call turns still promote totoolUse.packages/ai/test/issue-9433-repro.test.tscovering the three[DONE]-terminated wire shapes (no finish chunk / trailingfinish_reason: null/ trailing emptychoices: []), the tool-call promotion, and the retained genuine-EOF error; changelog entry.Verification
bun test packages/ai/test/issue-9433-repro.test.ts→ 5 pass.bun run --filter '@oh-my-pi/pi-ai' check→ clean (tsgo types + biome). Fullbun testinpackages/ai: only pre-existing unrelated failures (proxy.test.tsenv-ordering,issue-6276Bedrock 5s timeout), both pass in isolation. Skipped pre-publish gate (skip_checks=true):bun check/bun run fixfail oncheck:rs/fix:rsbecause thecmakebinary is absent from this environment (failed to execute command: No such file or directory ... is cmake not installed?) — a branch-independent toolchain gap, and this diff is TypeScript-only. Fixes #9433