Skip to content

fix(llm/google): retry SDK-raised errors with retryable status codes - #5651

Open
shoutoutuoadi325 wants to merge 1 commit into
browser-use:mainfrom
shoutoutuoadi325:fix/google-sdk-error-retries
Open

shoutoutuoadi325 wants to merge 1 commit into
browser-use:mainfrom
shoutoutuoadi325:fix/google-sdk-error-retries

Conversation

@shoutoutuoadi325

@shoutoutuoadi325 shoutoutuoadi325 commented Sep 2, 2026 •

Copy link
Copy Markdown

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 Exception branch, where the code already goes to the trouble of pulling out a status code from e.response.status_code or the message text, and then raises without ever consulting retryable_status_codes.

So max_retries/retryable_status_codes only 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.ClientError instead 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.py passes 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 handled ModelProviderError; SDK errors fell into the generic exception branch and failed immediately despite the status code already being extracted. Now that status code is checked against retryable_status_codes and gets the same backoff before failing.

  • Non-retryable statuses like 400 still fail on the first attempt, unchanged.
  • Adds tests covering a 429 that succeeds on the third attempt and a 400 that fails immediately.

Written for commit b0ba09e. Summary will update on new commits.

Review in cubic

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.
Copilot AI lite review requested due to automatic review settings September 2, 2026 04:24
@CLAassistant

CLAassistant commented Sep 2, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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-ModelProviderError exceptions when a retryable HTTP-ish status code can be derived.
  • Add CI tests covering SDK ClientError with 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

@cubic-dev-ai cubic-dev-ai Bot Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Suggested change
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:
Fix with cubic

):
status_code = 503

# SDK errors (google.genai ClientError/ServerError) also land here: give them

@cubic-dev-ai cubic-dev-ai Bot Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with cubic

@HardMax71

Copy link
Copy Markdown

We hit this in production with ChatGoogle on the defaults (max_retries=5, 503 in retryable_status_codes). Over two weeks we logged 13 ServerError: 503 UNAVAILABLE ("This model is currently experiencing high demand") and not a single Got 503 error, retrying line. On one day four 503s hit the same run within 16 seconds, and each one counted as a failed agent step instead of backing off.

I checked this branch against main (c75e847) with a stub client that answers ServerError(503) twice and then succeeds:

main:
raised ModelProviderError (status 503) after 1 call(s)

#5651:
WARNING  [gemini-3-flash-preview] ⚠️ Got 503 error, retrying in 0.0s... (attempt 1/5)
WARNING  [gemini-3-flash-preview] ⚠️ Got 503 error, retrying in 0.0s... (attempt 2/5)
result: 'ok' after 3 calls

(The ERROR line 💥 API call failed before each retry is left out.)

It still applies cleanly to main. Could a maintainer take a look?

This branch has not been deployed

No deployments
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.

4 participants