-
Notifications
You must be signed in to change notification settings - Fork 5.1k
feat: add InsufficientQuotaError for insufficient_quota 429 responses #3507
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
2a8433e
036eb86
63a1951
1376bd6
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 |
|---|---|---|
|
|
@@ -845,6 +845,18 @@ def _should_retry(self, response: httpx2.Response) -> bool: | |
|
|
||
| # Retry on rate limits. | ||
| if response.status_code == 429: | ||
| # An `insufficient_quota` error means the account has run out of | ||
| # quota; retrying will not help, so don't burn the retry budget | ||
| # on a request that is guaranteed to fail again. | ||
| try: | ||
| body = response.json() | ||
| except Exception: | ||
| body = None | ||
| data = body.get("error", body) if is_mapping(body) else body | ||
| if is_mapping(data) and data.get("code") == "insufficient_quota": | ||
| log.debug("Not retrying as the error code is `insufficient_quota`") | ||
| return False | ||
|
|
||
| log.debug("Retrying due to status code %i", response.status_code) | ||
| return True | ||
|
|
||
|
|
@@ -1122,6 +1134,19 @@ def request( | |
| except status_exceptions() as err: # thrown on 4xx and 5xx status code | ||
| log.debug("Encountered an HTTP status error: %i", response.status_code) | ||
|
|
||
| # The retry classification below may inspect the response body (e.g. to | ||
| # detect `insufficient_quota`), so make sure it has been read first. For | ||
| # `stream=True` requests the body is not read automatically and accessing | ||
| # it before this point raises `httpx.ResponseNotRead`. Reading can itself | ||
| # fail (e.g. a dropped connection mid-stream) for errors that are still | ||
| # retriable on status/headers alone, so a read failure here must not stop | ||
| # `_should_retry` from running. | ||
| if not err.response.is_closed: | ||
| try: | ||
| err.response.read() | ||
|
Comment on lines
+1144
to
+1146
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.
Fresh evidence after the catch fix is that this unconditional read still runs before retry classification for every streamed HTTP error, although only 429 quota classification needs the body. A streamed 408, 409, or 5xx response whose body stalls now waits for the read timeout—600 seconds by default, potentially once per attempt—before Useful? React with 👍 / 👎. |
||
| except Exception: | ||
| pass | ||
|
|
||
| if remaining_retries > 0 and self._should_retry(err.response): | ||
| err.response.close() | ||
| self._sleep_for_retry( | ||
|
|
@@ -1745,6 +1770,19 @@ async def request( | |
| except status_exceptions() as err: # thrown on 4xx and 5xx status code | ||
| log.debug("Encountered an HTTP status error: %i", response.status_code) | ||
|
|
||
| # The retry classification below may inspect the response body (e.g. to | ||
| # detect `insufficient_quota`), so make sure it has been read first. For | ||
| # `stream=True` requests the body is not read automatically and accessing | ||
| # it before this point raises `httpx.ResponseNotRead`. Reading can itself | ||
| # fail (e.g. a dropped connection mid-stream) for errors that are still | ||
| # retriable on status/headers alone, so a read failure here must not stop | ||
| # `_should_retry` from running. | ||
| if not err.response.is_closed: | ||
| try: | ||
| await err.response.aread() | ||
| except Exception: | ||
| pass | ||
|
|
||
| if remaining_retries > 0 and self._should_retry(err.response): | ||
| await err.response.aclose() | ||
| await self._sleep_for_retry( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -792,6 +792,8 @@ def _make_status_error( | |
| return _exceptions.UnprocessableEntityError(err_msg, response=response, body=data) | ||
|
|
||
| if response.status_code == 429: | ||
| if is_mapping(data) and data.get("code") == "insufficient_quota": | ||
| return _exceptions.InsufficientQuotaError(err_msg, response=response, body=data) | ||
|
Comment on lines
794
to
+796
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.
With the default Useful? React with 👍 / 👎. |
||
| return _exceptions.RateLimitError(err_msg, response=response, body=data) | ||
|
|
||
| if response.status_code >= 500: | ||
|
|
@@ -1488,6 +1490,8 @@ def _make_status_error( | |
| return _exceptions.UnprocessableEntityError(err_msg, response=response, body=data) | ||
|
|
||
| if response.status_code == 429: | ||
| if is_mapping(data) and data.get("code") == "insufficient_quota": | ||
| return _exceptions.InsufficientQuotaError(err_msg, response=response, body=data) | ||
| return _exceptions.RateLimitError(err_msg, response=response, body=data) | ||
|
|
||
| if response.status_code >= 500: | ||
|
|
||
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.
Fresh evidence after the retry fix: for
stream=True/.with_streaming_responsecalls,_base_client.request()sends the request with streaming enabled, so the 429 body has not been read when this line callsresponse.json(); httpx raisesResponseNotRead, the broadexceptturns the body intoNone, and the code falls through to retry. Streaming chat/responses requests that receivecode="insufficient_quota"will therefore still burnmax_retriesbefore raising, unlike the non-streaming path covered by the new tests.Useful? React with 👍 / 👎.