-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(responses): reject with OpenAIError when the stream emits an error event #2021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ import { | |
| } from '../../resources/responses/responses'; | ||
| import { RequestOptions } from '../../internal/request-options'; | ||
| import { type ReadableStream } from '../../internal/shim-types'; | ||
| import { APIUserAbortError, OpenAIError } from '../../error'; | ||
| import { APIError, APIUserAbortError, OpenAIError } from '../../error'; | ||
| import OpenAI from '../../index'; | ||
| import { type BaseEvents, EventStream } from '../EventStream'; | ||
| import { type ResponseFunctionCallArgumentsDeltaEvent, type ResponseTextDeltaEvent } from './EventTypes'; | ||
|
|
@@ -54,7 +54,7 @@ type ResponseEvents = BaseEvents & | |
| { | ||
| [K in ResponseStreamEvent['type']]: (event: Extract<ResponseStreamEvent, { type: K }>) => void; | ||
| }, | ||
| 'response.output_text.delta' | 'response.function_call_arguments.delta' | ||
| 'response.output_text.delta' | 'response.function_call_arguments.delta' | 'error' | ||
| > & { | ||
| event: (event: ResponseStreamEvent) => void; | ||
| 'response.output_text.delta': (event: ResponseTextDeltaEvent) => void; | ||
|
|
@@ -152,6 +152,12 @@ export class ResponseStream<ParsedT = null> | |
| } | ||
| break; | ||
| } | ||
| case 'error': { | ||
| // The API reports failures with an `error` event instead of a non-2xx response, so | ||
| // convert it here. Throwing hands it to `EventStream`'s error path, which rejects | ||
| // `finalResponse()` and notifies `error` listeners with the converted error. | ||
| throw new APIError(undefined, event, event.message, undefined); | ||
|
Comment on lines
+155
to
+159
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a Useful? React with 👍 / 👎. |
||
| } | ||
| default: | ||
| maybeEmit(event.type, event); | ||
| break; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the server sends an
errorframe beforeresponse.created, this branch is never reached becauseaccumulateResponse()first throws its generic “expected 'response.created'”OpenAIError. ConsequentlyfinalResponse()and error listeners lose the server's message, code, and parameter and do not receive the promisedAPIError. Handleevent.type === 'error'before calling the accumulator.Useful? React with 👍 / 👎.