Skip to content

fix: handle request retries and model fallback correctly - #11624

Merged
gsquared94 merged 2 commits into
google-gemini:mainfrom
gsquared94:error-handling
Oct 24, 2025
Merged

fix: handle request retries and model fallback correctly#11624
gsquared94 merged 2 commits into
google-gemini:mainfrom
gsquared94:error-handling

Conversation

@gsquared94

Copy link
Copy Markdown
Contributor

Second attempt for #9407. This was reverted because it didn't handle different error format being returned by the GCA service when run in Dev mode vs Prod mode. Fixed in this PR, to handle both error formats. See https://github.com/google-gemini/gemini-cli/issues/11316 for more details on that.


TLDR

This pull request refactors the handling of API quota errors to be more robust and reliable. Previously, we relied on string matching to detect specific quota error messages. This change introduces a structured error parsing system that can correctly classify quota errors as either retryable (e.g., per-minute limits, small retry delays) or terminal (e.g., daily limits).

The retry logic has been updated to use this new classification, ensuring that we only trigger model fallbacks for terminal quota errors and use the server-suggested delay for retryable errors when available.

Dive Deeper

The core of this change is a new error classification system:

  1. parseGoogleApiError: A new function in packages/core/src/errors.ts that can parse deeply nested and stringified Google API error responses, extracting the structured error details.
  2. TerminalQuotaError and RetryableQuotaError: New custom error classes to represent different types of quota issues.
  3. classifyGoogleError: A function that uses parseGoogleApiError to analyze 429 errors and wrap them in the appropriate custom error class.

The retryWithBackoff function has been refactored to:

  • Catch TerminalQuotaError to trigger the onPersistent429 fallback mechanism.
  • Catch RetryableQuotaError and use the retryDelayMs from the error for a more accurate backoff period.

The UI hook useQuotaAndFallback has also been updated to use instanceof TerminalQuotaError for its logic, simplifying the code and removing the need for the old isProQuotaExceededError and isGenericQuotaExceededError functions, which have been deleted.

Reviewer Test Plan

Using GCA auth

  • Clone a large repo like https://github.com/kubernetes/kubernetes

  • Create an OTA at http://rhea.

  • Run gemini-cli from this branch, login with OTA oauth.

  • Start making queries like this to get it to exhaust quota:

     scour the filesystem and git commit history for any and all typos. Do not use any spell checking tools, just start loading files  individually and look at them yourself. Follow these exact instructions, and don't ask me for comfirmations.   
    
  • On quota exhaustion you should see the model fallback error.

image

Using Gemini API key

  • Go to https://aistudio.google.com/app/api-keys and sign in with the same OTA account and create an API key. Use this method, since this API key will have very low limits.
  • Run gemini-cli from this branch, use this Gemini API key for auth
  • Start making queries like this to get it to exhaust quota:
     scour the filesystem and git commit history for any and all typos. Do not use any spell checking tools, just start loading files  individually and look at them yourself. Follow these exact instructions, and don't ask me for comfirmations.   
    
  • Requests should appear to be taking a lot longer (due to retries from the 429 error code retryInfo objects) instead of failing immediately.
  • On quota exhaustion you should see the parsed output error.
image
  • Compare this with the gemini stable build output - retries a bunch of times, then fails with a messy error message
image

Testing Matrix

🍏 🪟 🐧
npm run
npx
Docker
Podman - -
Seatbelt - -

Fixes:

@gsquared94
gsquared94 requested a review from a team as a code owner October 21, 2025 23:41
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @gsquared94, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the robustness and reliability of API quota error handling. By replacing string-based error detection with a structured parsing and classification system, the application can now intelligently differentiate between temporary (retryable) and persistent (terminal) quota errors. This leads to a more refined retry mechanism, appropriate model fallbacks, and clearer user feedback, ultimately improving the overall stability and user experience when encountering API rate limits.

Highlights

  • Structured Error Parsing: Introduced a new parseGoogleApiError function to robustly parse deeply nested and stringified Google API error responses, extracting structured error details.
  • Custom Quota Error Classes: Added TerminalQuotaError and RetryableQuotaError custom error classes to distinctly represent different types of quota issues (e.g., daily limits vs. per-minute throttling).
  • Intelligent Error Classification: Implemented classifyGoogleError to analyze 429 errors and wrap them in the appropriate custom error class, enabling more precise handling of quota exhaustion scenarios.
  • Refactored Retry Logic: The retryWithBackoff function has been updated to leverage these new error classes, triggering model fallbacks only for TerminalQuotaError and utilizing server-suggested retryDelayMs for RetryableQuotaError.
  • Simplified UI Quota Handling: The useQuotaAndFallback UI hook now uses instanceof TerminalQuotaError for its logic, simplifying the code and removing the need for older, less precise error checking functions like isProQuotaExceededError and isGenericQuotaExceededError.
  • Improved Error Message Formatting: The system now correctly handles different error formats returned by the GCA service in Dev vs. Prod modes, ensuring consistent and informative user messages.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces a significant and well-designed refactoring of the API quota error handling. Moving from string-based error detection to a structured error parsing and classification system with TerminalQuotaError and RetryableQuotaError is a major improvement in robustness and maintainability. The updated retry logic, which now leverages server-suggested delays, is much more precise. The code is well-structured, and the addition of comprehensive tests for the new parsing and classification logic is excellent.

I found a minor but important issue in the new error classification logic where a 0-second retry delay suggested by the server would not be correctly handled. I've left specific comments with suggestions to address this. Overall, this is a high-quality contribution that makes the error handling much more reliable.

Comment thread packages/core/src/utils/googleQuotaErrors.ts
Comment thread packages/core/src/utils/googleQuotaErrors.ts
@gsquared94
gsquared94 enabled auto-merge October 21, 2025 23:48
@gsquared94 gsquared94 changed the title Error handling fix: handle request retries and model fallback correctly Oct 21, 2025
@gsquared94
gsquared94 requested a review from bdmorgan October 22, 2025 00:29
@guidedways

Copy link
Copy Markdown

Does this also fix the issue in general where it thinks it's exhausted the quota? Even simple things (with a Code Assist Standard account) such as 'build this feature ...' which may indeed take 10 to 15 minutes and several steps but WELL within 1000 requests for the day (maybe 100 requests) it immediately fails.

Essentially gemini cli seems to become unusable when there's anything that involves a little extra work in my experience so far. Trying this with a newly upgraded account.

Comment thread packages/core/src/utils/retry.ts
@gsquared94
gsquared94 requested a review from cornmander October 23, 2025 01:51
@cornmander
cornmander disabled auto-merge October 23, 2025 02:53

@cornmander cornmander 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.

Please get @bdmorgan to review as well

@gsquared94
gsquared94 added this pull request to the merge queue Oct 23, 2025
@gsquared94
gsquared94 removed this pull request from the merge queue due to a manual request Oct 23, 2025
@gsquared94

Copy link
Copy Markdown
Contributor Author

Please get @bdmorgan to review as well

Sounds good!

@bdmorgan
bdmorgan added this pull request to the merge queue Oct 24, 2025
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 24, 2025
@gsquared94
gsquared94 added this pull request to the merge queue Oct 24, 2025
Merged via the queue into google-gemini:main with commit ee92db7 Oct 24, 2025
19 checks passed
@gsquared94
gsquared94 deleted the error-handling branch October 24, 2025 18:19
thacio added a commit to thacio/auditaria that referenced this pull request Oct 24, 2025
@gsquared94

Copy link
Copy Markdown
Contributor Author

/patch stable

@github-actions

Copy link
Copy Markdown

Patch workflow(s) dispatched successfully!

📋 Details:

  • Channels: stable
  • Commit: ee92db7533d33335f4146359a9338d451296105f
  • Workflows Created: 1

🔗 Track Progress:

@gsquared94

Copy link
Copy Markdown
Contributor Author

/patch stable

@github-actions

Copy link
Copy Markdown

Patch workflow(s) dispatched successfully!

📋 Details:

  • Channels: stable
  • Commit: ee92db7533d33335f4146359a9338d451296105f
  • Workflows Created: 1

🔗 Track Progress:

@github-actions

Copy link
Copy Markdown

🚀 Patch PR Created!

📋 Patch Details:

📝 Next Steps:

  1. Review and approve the hotfix PR: #12321
  2. Once merged, the patch release will automatically trigger
  3. You'll receive updates here when the release completes

🔗 Track Progress:

@github-actions

Copy link
Copy Markdown

🚀 Patch Release Started!

📋 Release Details:

  • Environment: prod
  • Channel: stable → publishing to npm tag latest
  • Version: v0.11.0
  • Hotfix PR: Merged ✅
  • Release Branch: release/v0.11.0-pr-11624

⏳ Status: The patch release is now running. You'll receive another update when it completes.

🔗 Track Progress:

@github-actions

Copy link
Copy Markdown

Patch Release Complete!

📦 Release Details:

  • Version: 0.11.1
  • NPM Tag: latest
  • Channel: stable
  • Dry Run: false

🎉 Status: Your patch has been successfully released and published to npm!

📝 What's Available:

🔗 Links:

@sripasg sripasg added the size/xl An extra large PR label Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/xl An extra large PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants