Skip to content

fix(OpenAI): handle rate limit and server errors on Guzzle stream requests - #816

Merged
iBotPeaches merged 1 commit into
openai-php:mainfrom
APX403:stream-bad-response-errors
Oct 20, 2026
Merged

iBotPeaches merged 1 commit into
openai-php:mainfrom
APX403:stream-bad-response-errors

Conversation

@APX403

@APX403 APX403 commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

What:

  • Bug Fix
  • New Feature

Description:

With Guzzle, a streamed request that gets a 429 from the API throws ErrorException and one that gets a 5xx throws TransporterException, while the same requests without streaming throw RateLimitException and ServerException (#652, #757). For example, createStreamed() on a 503:

OpenAI\Exceptions\TransporterException: Server error: `POST https://api.openai.com/v1/chat/completions` resulted in a `503 Service Unavailable` response

The default stream handler calls Guzzle's send(), which throws ClientException or ServerException for 4xx and 5xx responses because http_errors defaults to true. The PSR-18 sendRequest() used for the other requests turns it off, so only streams are affected. The catch in HttpTransporter::sendRequest() only looks for a JSON error inside a ClientException, so the rate limit and server error checks in requestStream() never run.

The catch now checks for Guzzle's BadResponseException, the parent of both exceptions, and runs the same checks in the same order as for a returned response. Doing it there rather than turning off http_errors in the factory also covers custom stream handlers like the one in the README. Code that catches ErrorException or TransporterException for a streamed 429 or 5xx will now get RateLimitException or ServerException, as it already does without streaming.

@iBotPeaches
iBotPeaches merged commit f9857a8 into openai-php:main Oct 20, 2026
14 checks passed
@iBotPeaches iBotPeaches added this to the v0.22.0 milestone Oct 20, 2026
@iBotPeaches

Copy link
Copy Markdown
Collaborator

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants