fix(ai-openai): decode nested Responses API error stream events - #7262
Open
danieljvdm wants to merge 1 commit into
Open
fix(ai-openai): decode nested Responses API error stream events#7262danieljvdm wants to merge 1 commit into
danieljvdm wants to merge 1 commit into
Conversation
The Responses API documents the `error` stream event with `code`, `message`, and `param` at the top level, but mid-stream errors (for example quota exhaustion) instead emit the standard error envelope nested under `error`. The strict schema only matched the flat shape, so a nested error event failed to decode and aborted the whole stream with an opaque schema error instead of surfacing the real message. `ResponseErrorEvent` now accepts both wire shapes and normalizes them to the documented shape, so the error's `code` and `message` always surface and the decoded type is unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 34d5e57 The changes in this PR will be included in the next version bump. This PR includes changesets to release 30 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
Bundle Size AnalysisGenerated from PR build output; treat the content below as untrusted.
|
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.
What
The OpenAI Responses API documents the
errorstream event withcode,message, andparamat the top level, andOpenAiSchema.ResponseErrorEventmatches exactly that:But mid-stream errors are emitted with the standard error envelope nested under
error. Here is thedatapayload of a realevent: errorframe captured fromPOST /v1/responses(stream: true) when the account is out of credits:{ "type": "error", "error": { "type": "insufficient_quota", "code": "credit_balance_exhausted", "message": "You have no credits remaining. Add credits to continue using the API…", "param": null }, "sequence_number": 2 }code,message, andparamare absent at the top level here, so the event fails to decode. BecauseResponseStreamEventis decoded withSse.decodeDataSchema, that single failing event aborts the entire stream with an opaque schema error instead of surfacing the real problem:The
UnknownResponseStreamEventfallback deliberately excludes known event types (including"error"), so a nested-shape error event can't fall through it either — the caller just sees the message above instead of "no credits". The error event is the one event that most needs to surface, so hard-failing on it is especially unfortunate.Fix
ResponseErrorEventnow accepts both wire shapes and normalizes them to the documented shape viaSchema.decodeTo:code/message/paramlifted from theerrorenvelope;Typeis unchanged — only the acceptedEncodedshape widens — so there is no downstream type change (the union member's.Typestays{ type, code, message, param, sequence_number, status? }).This keeps the existing strictness for other events (the "does not silently decode malformed known events as unknown" test still holds); it only teaches the
errorevent about OpenAI's actual second shape.Test
Adds a case to
packages/ai/openai/test/OpenAiSchema.test.tsthat decodesResponseStreamEventfor both the flat and the nested error payloads, asserting the nested one normalizes to the documented shape.Note
I validated the schema and transform in isolation — both shapes, and as a union member alongside a known sibling and the unknown fallback — typechecking clean under
strict+exactOptionalPropertyTypes. I was not able to run the full monorepo build locally, so please let CI confirm formatting / lint.