Skip to content

refactor(core): improve API response error logging when retry#21784

Merged
yunaseoul merged 2 commits intomainfrom
yunaseol/gca-parsing
Mar 10, 2026
Merged

refactor(core): improve API response error logging when retry#21784
yunaseoul merged 2 commits intomainfrom
yunaseol/gca-parsing

Conversation

@yunaseoul
Copy link
Contributor

@yunaseoul yunaseoul commented Mar 9, 2026

Summary

This PR refactors the API response error handling and retry logic to improve the accuracy of our telemetry and error logging.

Details

  • Enhanced Failure Logging: Previously, logContentRetryFailure only captured InvalidStreamError for Gemini 2 models, missing terminal network failures. Now, it consistently logs ContentRetryFailureEvent for all exhausted retryable errors (content and network), with the correct attempt number.
  • Refactored Retry Logic: Simplifies the stream retry loop in GeminiChat by removing the lastError tracking variable in favor of immediate throwing within the catch block.

Related Issues

Fixes #20189

How to Validate

Review the code changes and run npm run test -w @google/gemini-cli-core. The tests will ensure that retry counts are accurately reported and the logging behaves as expected for both eligible and non-eligible models.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

…lure logic

- Simplifies the stream retry loop in GeminiChat by removing the 'lastError' tracking variable in favor of immediate throwing within the catch block. This ensures that logContentRetryFailure is correctly triggered exactly once on the final failure attempt (or initial failure if no retries occur) for Gemini 2 models.
- Adds explicit test expectations to verify logContentRetryFailure is not called for non-eligible models or errors
- Updated `streamWithRetries` to track the actual number of `attemptsMade` instead of blindly reporting `maxAttempts` when an `InvalidStreamError` occurs and a retry is not performed (e.g., for non-Gemini-2 models).
- Ensure network errors are logged as ContentRetryFailureEvent as well.
@yunaseoul yunaseoul requested a review from a team as a code owner March 9, 2026 23:41
@gemini-cli
Copy link
Contributor

gemini-cli bot commented Mar 9, 2026

Hi @yunaseoul, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this.

We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines.

Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed.

Thank you for your understanding and for being a part of our community!

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 refines the content retry mechanism within the GeminiChat component, focusing on accuracy and clarity in error reporting. The changes ensure that retry attempts are precisely counted and logged, providing a more reliable understanding of content delivery failures. By simplifying the retry loop and consolidating error handling, the system becomes more robust and easier to maintain.

Highlights

  • Improved Content Retry Tracking: The system now accurately tracks and reports the number of content retry attempts, including logging network errors as ContentRetryFailureEvent.
  • Simplified GeminiChat Retry Logic: The internal retry loop in GeminiChat has been streamlined by removing the lastError tracking variable and immediately throwing errors within the catch block.
  • Precise Failure Logging: logContentRetryFailure is now guaranteed to be called exactly once upon the final failure attempt (or initial failure if no retries occur), and not for non-eligible models or errors.
  • Enhanced Test Coverage: Explicit test expectations were added to verify the correct behavior of logContentRetryFailure for both eligible and non-eligible scenarios.
Changelog
  • packages/core/src/core/geminiChat.test.ts
    • Added a test expectation to verify logContentRetryFailure is called exactly once when a retry fails.
  • packages/core/src/core/geminiChat.ts
    • Removed the lastError tracking variable, simplified the stream retry loop by throwing errors immediately, introduced an errorType variable for consistent logging, and updated logContentRetry and logContentRetryFailure calls to use accurate attempt counts and error types.
  • packages/core/src/core/geminiChat_network_retry.test.ts
    • Added a test expectation to confirm logContentRetryFailure is not called when no retries occur.
Activity
  • Updated tests were added.
  • Validation was performed on MacOS.
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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 refactors the retry logic in GeminiChat to simplify the implementation and improve the accuracy of failure reporting. The changes remove the lastError tracking variable in favor of a more direct control flow, ensure logContentRetryFailure is called for final failures, and correctly report the number of attempts made. The accompanying test updates correctly validate this new behavior.

My main feedback concerns a subtle inconsistency in the failure logging. For non-retryable errors during the initial connection phase, the failure is not logged, which seems to contradict one of the goals of this PR. I've left a specific comment with more details.

@github-actions
Copy link

github-actions bot commented Mar 9, 2026

Size Change: -2.24 kB (-0.01%)

Total Size: 26.1 MB

Filename Size Change
./bundle/gemini.js 25.7 MB -2.24 kB (-0.01%)
ℹ️ View Unchanged
Filename Size
./bundle/node_modules/@google/gemini-cli-devtools/dist/client/main.js 221 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/_client-assets.js 227 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/index.js 11.5 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/types.js 132 B
./bundle/sandbox-macos-permissive-open.sb 890 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB
./bundle/sandbox-macos-strict-open.sb 4.82 kB
./bundle/sandbox-macos-strict-proxied.sb 5.02 kB

compressed-size-action

@gemini-cli gemini-cli bot added area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. labels Mar 9, 2026
@yunaseoul yunaseoul changed the title fix(core): accurately track and report content retry attempts refactor(core): improve API response error logging and retry logic Mar 9, 2026
@yunaseoul yunaseoul changed the title refactor(core): improve API response error logging and retry logic refactor(core): improve API response error logging when retry Mar 10, 2026
Copy link
Collaborator

@abhipatel12 abhipatel12 left a comment

Choose a reason for hiding this comment

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

LGTM!

@yunaseoul yunaseoul added this pull request to the merge queue Mar 10, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 10, 2026
@sehoon38 sehoon38 added this pull request to the merge queue Mar 10, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 10, 2026
@yunaseoul yunaseoul added this pull request to the merge queue Mar 10, 2026
Merged via the queue into main with commit 0486a16 Mar 10, 2026
35 checks passed
@yunaseoul yunaseoul deleted the yunaseol/gca-parsing branch March 10, 2026 14:41
PrasannaPal21 pushed a commit to PrasannaPal21/gemini-cli that referenced this pull request Mar 10, 2026
kunal-10-cloud pushed a commit to kunal-10-cloud/gemini-cli that referenced this pull request Mar 12, 2026
yashodipmore pushed a commit to yashodipmore/geemi-cli that referenced this pull request Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhance observability and alerting for API parsing and stream errors

2 participants