fix(openai): require terminal stream events before finishing#246
Draft
ethanndickson wants to merge 1 commit into
Draft
fix(openai): require terminal stream events before finishing#246ethanndickson wants to merge 1 commit into
ethanndickson wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
#232 gated the Chat Completions text
Streamand the Responses API textStreamonfantasy.NewIncompleteStreamError(). This PR closes two remaining gaps that those guards don't cover, plus a related misclassification.What this PR fixes
1. Chat Completions object stream — silent truncation.
streamObjectWithJSONModehas no terminal-event check today. A truncated stream where any prior chunk parsed cleanly emitsFinishwith the partiallastParsedObject— the caller receives a JSON object missing fields, reported as success. Fix: tracksawFinishReason; otherwise yieldfantasy.NewIncompleteStreamError()(retryable*fantasy.ProviderErrorwithCause: io.ErrUnexpectedEOF) withctx.Err()precedence.2. Responses API object stream — misclassified truncation + silent-finish risk. Post-loop wraps
io.EOFfromstream.Err()viatoProviderErr, which produces a non-retryable error (IsRetryable()matchesio.ErrUnexpectedEOF, not bareio.EOF), so transient truncation surfaces as a fatal provider error and retries never engage. And with no terminal-event check beyond theerrorevent, an SDK that returnsnilon a clean mid-message close flows straight toFinishwith a partial parsed object. Fix: tracksawTerminalEvent(set onresponse.completed/response.incomplete), treatio.EOFfromstream.Err()as a fall-through to that check, and emitfantasy.NewIncompleteStreamError()when neither arrived.3.
response.failedmisclassified as transport truncation on both Responses streams. Today the event falls through, fails the terminal-event check, and gets retried as if it were a network truncation. Fix: handleresponse.failedexplicitly, yielding a non-retryable*fantasy.Error{Title: "response failed"}with the provider's error message and code. The pre-existing top-levelerrorevent is normalized through the same helper as*fantasy.Error{Title: "response error"}, so only synthetic stream truncation is wrapped withio.ErrUnexpectedEOF.Two smaller refinements ride along: the existing Chat Completions text
Streamguard now respectsctx.Err()precedence, and the Responses API textStream's existingFinishReasonUnknown-sentinel guard is renamed to an explicitsawTerminalEventfor clarity (semantics unchanged).Tests
Three new table-driven tests cover happy path + each missing terminal signal + (where applicable)
response.failed/errorevents:TestChatCompletionsStreamObject_RequiresFinishReasonBeforeFinishTestResponsesStream_RequiresTerminalEventBeforeFinishTestResponsesStreamObject_RequiresTerminalEventBeforeFinishThe object-stream truncation subtests would emit
Finishonmaintoday.Prior art
OpenAI's own
openai/codex(Apache-2.0) implements the same gate for the Responses API:codex-rs/codex-api/src/sse/responses.rshandlesresponse.completed,response.incomplete, andresponse.failedas distinct terminal events, and emitsApiError::Stream("stream closed before response.completed")on early EOF. Regression teststream_no_completed.rs::retries_on_early_closeasserts the retry path fires; an inlineerror_when_error_eventtest coversresponse.failedend-to-end.vercel/ai(Apache-2.0) makes the sameisResponseCompletedChunk/isResponseFailedChunkdistinction in its OpenAI Responses adapter.Follow-ups
Similar terminal-event gaps exist in a few of the other providers in this repo; those will land in separate PRs so each can be reviewed in isolation.