Repository navigation
fix(llm/google): retry SDK-raised errors with retryable status codes - #5651
shoutoutuoadi325 wants to merge 1 commit into
Conversation
ClientError/ServerError from google-genai never matched the ModelProviderError branch, so a single 429 or 503 killed the call on the first attempt even though the generic except branch already extracts the status code. Use it for the same backoff check before wrapping.
There was a problem hiding this comment.
🟢 Approval recommended
The change is narrowly scoped, preserves prior behavior for non-retryable/unknown statuses, and adds targeted tests covering the new retry path.
Pull request overview
This PR fixes ChatGoogle’s retry loop so that SDK-raised google-genai exceptions (e.g., 429 RESOURCE_EXHAUSTED / 503) are retried using the same retryable_status_codes + exponential backoff logic that previously applied only to ModelProviderError.
Changes:
- Add retry/backoff handling for non-
ModelProviderErrorexceptions when a retryable HTTP-ish status code can be derived. - Add CI tests covering SDK
ClientErrorwith a retryable status (429) and a non-retryable status (400).
File summaries
| File | Description |
|---|---|
browser_use/llm/google/chat.py |
Extends the existing retry loop to apply backoff/retry to SDK-raised errors when the extracted status code is retryable. |
tests/ci/test_llm_retries.py |
Adds regression tests ensuring SDK ClientError 429 is retried and SDK ClientError 400 is not retried. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b0ba09eb48
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| # SDK errors (google.genai ClientError/ServerError) also land here: give them | ||
| # the same backoff as the ModelProviderError branch above | ||
| if status_code is not None and status_code in self.retryable_status_codes and attempt < self.max_retries - 1: |
There was a problem hiding this comment.
Preserve the exact SDK status before retrying
When retryable_status_codes is narrowed to [500] or [502], a genuine SDK error with that response status is not retried: the status is first extracted correctly, but the message heuristics above see 500/502 in the SDK exception string and overwrite it with 503, so this membership test rejects it. Since this constructor option promises retries for the specified HTTP codes, retain an extracted response status and use message heuristics only when no authoritative status is available.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
2 issues found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="browser_use/llm/google/chat.py">
<violation number="1" location="browser_use/llm/google/chat.py:574">
P3: This branch duplicates the complete delay, jitter, warning, and sleep sequence already used for `ModelProviderError` above. Extract that sequence into one helper so future backoff changes cannot diverge between exception paths.</violation>
<violation number="2" location="browser_use/llm/google/chat.py:576">
P2: When a caller configures only the SDK response code, a real 500/502 SDK error can be rewritten to 503 by the message heuristics before this check, so `retryable_status_codes=[500]` or `[502]` does not retry it. Restore the SDK exception's `code` before testing the retry list, and only infer a status from the message when no response code exists.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Re-trigger cubic
|
|
||
| # SDK errors (google.genai ClientError/ServerError) also land here: give them | ||
| # the same backoff as the ModelProviderError branch above | ||
| if status_code is not None and status_code in self.retryable_status_codes and attempt < self.max_retries - 1: |
There was a problem hiding this comment.
P2: When a caller configures only the SDK response code, a real 500/502 SDK error can be rewritten to 503 by the message heuristics before this check, so retryable_status_codes=[500] or [502] does not retry it. Restore the SDK exception's code before testing the retry list, and only infer a status from the message when no response code exists.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At browser_use/llm/google/chat.py, line 576:
<comment>When a caller configures only the SDK response code, a real 500/502 SDK error can be rewritten to 503 by the message heuristics before this check, so `retryable_status_codes=[500]` or `[502]` does not retry it. Restore the SDK exception's `code` before testing the retry list, and only infer a status from the message when no response code exists.</comment>
<file context>
@@ -571,6 +571,18 @@ async def _make_api_call():
+ # SDK errors (google.genai ClientError/ServerError) also land here: give them
+ # the same backoff as the ModelProviderError branch above
+ if status_code is not None and status_code in self.retryable_status_codes and attempt < self.max_retries - 1:
+ delay = min(self.retry_base_delay * (2**attempt), self.retry_max_delay)
+ jitter = random.uniform(0, delay * 0.1) # 10% jitter
</file context>
| if status_code is not None and status_code in self.retryable_status_codes and attempt < self.max_retries - 1: | |
| status_code = getattr(e, 'code', None) or status_code | |
| if status_code is not None and status_code in self.retryable_status_codes and attempt < self.max_retries - 1: |
| ): | ||
| status_code = 503 | ||
|
|
||
| # SDK errors (google.genai ClientError/ServerError) also land here: give them |
There was a problem hiding this comment.
P3: This branch duplicates the complete delay, jitter, warning, and sleep sequence already used for ModelProviderError above. Extract that sequence into one helper so future backoff changes cannot diverge between exception paths.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At browser_use/llm/google/chat.py, line 574:
<comment>This branch duplicates the complete delay, jitter, warning, and sleep sequence already used for `ModelProviderError` above. Extract that sequence into one helper so future backoff changes cannot diverge between exception paths.</comment>
<file context>
@@ -571,6 +571,18 @@ async def _make_api_call():
):
status_code = 503
+ # SDK errors (google.genai ClientError/ServerError) also land here: give them
+ # the same backoff as the ModelProviderError branch above
+ if status_code is not None and status_code in self.retryable_status_codes and attempt < self.max_retries - 1:
</file context>
|
We hit this in production with I checked this branch against main (c75e847) with a stub client that answers (The ERROR line It still applies cleanly to main. Could a maintainer take a look? |
Ran into this while running longer Gemini tasks: one rate-limit hit aborts the step, because the retry loop in ChatGoogle only ever retried ModelProviderError. Real errors raised by google-genai (ClientError/ServerError for 429 RESOURCE_EXHAUSTED, 503, etc.) fall into the generic
except Exceptionbranch, where the code already goes to the trouble of pulling out a status code frome.response.status_codeor the message text, and then raises without ever consultingretryable_status_codes.So
max_retries/retryable_status_codesonly applied to errors we raise ourselves (e.g. the JSON-parse 500), which is backwards.This runs the extracted status code through the same backoff check the ModelProviderError branch uses - nothing else changes. Errors without a recognizable status (or with a non-retryable one like 400) still fail exactly as before, and ChatBrowserUse already retries real transport errors the same way.
Tests: added two cases to TestChatGoogleRetries that raise actual
google.genai.errors.ClientErrorinstead of ModelProviderError - one 429 that now succeeds on the third attempt, one 400 that still fails after a single attempt.uv run pytest tests/ci/test_llm_retries.py tests/ci/models/test_llm_google.pypasses locally.Summary by cubic
Retries SDK-raised errors from
google-genai(429, 503, etc.) so a single rate-limit hit no longer aborts a Gemini step. Previously the retry loop only handledModelProviderError; SDK errors fell into the generic exception branch and failed immediately despite the status code already being extracted. Now that status code is checked againstretryable_status_codesand gets the same backoff before failing.Written for commit b0ba09e. Summary will update on new commits.