Skip to content

fix(core): sanitize SSE-corrupted JSON and domain strings in error classification#21702

Merged
gsquared94 merged 2 commits intogoogle-gemini:mainfrom
gsquared94:fix/sse-json-corruption-error-classification
Mar 9, 2026
Merged

fix(core): sanitize SSE-corrupted JSON and domain strings in error classification#21702
gsquared94 merged 2 commits intogoogle-gemini:mainfrom
gsquared94:fix/sse-json-corruption-error-classification

Conversation

@gsquared94
Copy link
Contributor

@gsquared94 gsquared94 commented Mar 9, 2026

Summary

Mitigate SSE stream corruption that causes 429 QUOTA_EXHAUSTED errors to be incorrectly classified as retryable, leading to unnecessary retry loops instead of triggering the AI credits fallback flow.

This is a client-side mitigation until the root cause — either in server-side SSE chunking or client-side SSE stream parsing — can be diagnosed and fixed.

Details

When the API returns a 429 error with alt=sse, the JSON error body can arrive with a stray comma injected at a line boundary. The observed corruption pattern is:

"domain": "cloudcode-pa.googleapis.com",
 ,       "metadata": {

This comma-whitespace-comma pattern causes two cascading failures:

  1. JSON parsing failure: JSON.parse() fails on the corrupted body, so parseGoogleApiError returns null. Without structured error details, classifyGoogleError falls through to treating the 429 as a generic RetryableQuotaError.

  2. Domain validation failure: Even when parsing eventually succeeds through an alternate code path, the extracted domain string contains a trailing comma ("cloudcode-pa.googleapis.com,"), which fails the CLOUDCODE_DOMAINS.includes() exact-match check.

The combined effect: TerminalQuotaError is never thrown, the AI credits fallback UI never triggers, and the client enters a retry loop.

Changes

  • googleErrors.ts: Add sanitizeJsonString() that collapses comma-whitespace-comma patterns (regex /,(\s*),/g) before all JSON.parse() calls in the error parsing pipeline.
  • googleQuotaErrors.ts: Add isCloudCodeDomain() that strips non-alphanumeric characters (except . and -) from domain strings before comparing against CLOUDCODE_DOMAINS.

Related Issues

Related: #21704

How to Validate

  1. Run the new test cases:
    npm test -w @google/gemini-cli-core -- src/utils/googleErrors.test.ts src/utils/googleQuotaErrors.test.ts
  2. Verify the SSE-corrupted JSON tests pass:
    • should parse a gaxios error with SSE-corrupted JSON containing stray commas
    • should parse a gaxios error with SSE-corrupted JSON in response.data
  3. Verify the corrupted domain tests pass:
    • should return TerminalQuotaError for Cloud Code QUOTA_EXHAUSTED with SSE-corrupted domain
    • should return ValidationRequiredError with SSE-corrupted domain

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

…assification

SSE serialization can inject stray commas into the JSON error response body,
causing JSON.parse() to fail and domain validation to miss corrupted domain
strings. This is a mitigation until the root cause in server-side SSE chunking
or client-side SSE parsing can be diagnosed and fixed.

- Add sanitizeJsonString() to handle comma-whitespace-comma patterns in JSON
- Add isCloudCodeDomain() to strip non-alphanumeric chars from domain strings
- Add test cases for both SSE-corrupted JSON parsing and domain sanitization
@gsquared94 gsquared94 requested a review from a team as a code owner March 9, 2026 13:32
@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 introduces a client-side mitigation to address issues arising from SSE stream corruption, which was causing Google API 429 errors to be misclassified. By sanitizing both JSON error bodies and domain strings before processing, the system can now correctly identify quota exhaustion and validation requirements, ensuring the appropriate fallback mechanisms (like AI credits) are triggered rather than entering infinite retry loops. This significantly improves the robustness of error handling for API responses.

Highlights

  • SSE Corruption Mitigation: Implemented client-side sanitization for JSON and domain strings to address corruption caused by Server-Sent Events (SSE) stream parsing, preventing incorrect error classification.
  • JSON String Sanitization: Added a utility function, sanitizeJsonString(), to collapse stray comma patterns (e.g., , ,) in JSON strings before parsing, ensuring JSON.parse() succeeds even with corrupted input.
  • Domain String Sanitization: Introduced isCloudCodeDomain() to strip non-alphanumeric characters from domain strings before validation, correctly identifying Cloud Code domains despite SSE-induced corruption.
  • Improved Error Classification: Ensured that 429 QUOTA_EXHAUSTED errors with corrupted JSON or domain strings are correctly classified as TerminalQuotaError or ValidationRequiredError, triggering the AI credits fallback instead of unnecessary retries.
  • New Test Cases: Added comprehensive test cases to validate the new sanitization logic for both SSE-corrupted JSON and domain strings, covering various error parsing scenarios.
Changelog
  • packages/core/src/utils/googleErrors.test.ts
    • Added a test case to verify parsing of SSE-corrupted JSON containing stray commas when the error is in the message path.
    • Added a test case to verify parsing of SSE-corrupted JSON when the error is in response.data.
  • packages/core/src/utils/googleErrors.ts
    • Introduced sanitizeJsonString function to remove duplicate commas in JSON strings.
    • Applied sanitizeJsonString to JSON.parse calls when parsing errorObj if it's a string.
    • Applied sanitizeJsonString to JSON.parse calls when parsing currentError.message.
    • Applied sanitizeJsonString to JSON.parse calls within fromGaxiosError when processing response.data.
    • Applied sanitizeJsonString to JSON.parse calls within fromApiError when processing response.data.
    • Applied sanitizeJsonString to JSON.parse calls within fromApiError when extracting JSON from substrings.
  • packages/core/src/utils/googleQuotaErrors.test.ts
    • Added a test case to ensure TerminalQuotaError is returned for Cloud Code QUOTA_EXHAUSTED errors with SSE-corrupted domain strings.
    • Added a test case to ensure ValidationRequiredError is returned for errors with SSE-corrupted domain strings.
  • packages/core/src/utils/googleQuotaErrors.ts
    • Introduced isCloudCodeDomain function to sanitize domain strings by removing non-alphanumeric characters (except . and -) before checking against CLOUDCODE_DOMAINS.
    • Replaced direct CLOUDCODE_DOMAINS.includes(errorInfo.domain) with isCloudCodeDomain(errorInfo.domain) in classifyValidationRequiredError.
    • Replaced direct validDomains.includes(errorInfo.domain) with isCloudCodeDomain(errorInfo.domain) in classifyGoogleError.
Activity
  • The author, gsquared94, has implemented a client-side mitigation for SSE stream corruption.
  • New test cases have been added to validate the JSON and domain string sanitization logic.
  • The pull request includes updates for MacOS and Linux platforms, with npm run validation completed.
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 introduces a client-side mitigation for SSE stream corruption that can lead to incorrect error classification. The changes involve sanitizing corrupted JSON strings by removing duplicate commas and cleaning up domain strings before validation. New tests are added to cover these sanitization cases. My main feedback is to improve the robustness of the JSON sanitization logic to handle more complex corruption patterns, such as multiple consecutive commas.

@gemini-cli gemini-cli bot added the status/need-issue Pull requests that need to have an associated issue. label Mar 9, 2026
Apply the regex replacement in a loop so that patterns like ',,,'' are
fully collapsed, not just reduced to ',,' on a single pass.
@gsquared94 gsquared94 enabled auto-merge March 9, 2026 14:05
@gsquared94 gsquared94 added this pull request to the merge queue Mar 9, 2026
Merged via the queue into google-gemini:main with commit 936f624 Mar 9, 2026
28 checks passed
@gsquared94 gsquared94 deleted the fix/sse-json-corruption-error-classification branch March 9, 2026 14:20
@github-actions
Copy link

github-actions bot commented Mar 9, 2026

Patch workflow(s) dispatched successfully!

📋 Details:

  • Channels: preview
  • Commit: 936f6240dd9821c380f628943dea20f87da365fe
  • Workflows Created: 1

🔗 Track Progress:

github-actions bot pushed a commit that referenced this pull request Mar 9, 2026
@github-actions
Copy link

github-actions bot commented Mar 9, 2026

🚀 Patch PR Created!

📋 Patch Details:

📝 Next Steps:

  1. Review and approve the hotfix PR: #21742
  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

github-actions bot commented Mar 9, 2026

🚀 Patch Release Started!

📋 Release Details:

  • Environment: prod
  • Channel: preview → publishing to npm tag preview
  • Version: v0.33.0-preview.5
  • Hotfix PR: Merged ✅
  • Release Branch: release/v0.33.0-preview.5-pr-21702

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

🔗 Track Progress:

@jerop jerop added the release/patch-to-preview PR needs to be patch to preview release label Mar 9, 2026
@galz10
Copy link
Collaborator

galz10 commented Mar 10, 2026

/patch preview

@github-actions
Copy link

Patch workflow(s) dispatched successfully!

📋 Details:

  • Channels: preview
  • Commit: 936f6240dd9821c380f628943dea20f87da365fe
  • Workflows Created: 1

🔗 Track Progress:

github-actions bot pushed a commit that referenced this pull request Mar 10, 2026
@github-actions
Copy link

🚀 Patch PR Created!

📋 Patch Details:

📝 Next Steps:

  1. Review and approve the hotfix PR: #21800
  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

🚀 Patch Release Started!

📋 Release Details:

  • Environment: prod
  • Channel: preview → publishing to npm tag preview
  • Version: v0.33.0-preview.9
  • Hotfix PR: Merged ✅
  • Release Branch: release/v0.33.0-preview.9-pr-21702

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

🔗 Track Progress:

@github-actions
Copy link

Patch Release Complete!

📦 Release Details:

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

📝 What's Available:

🔗 Links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release/patch-to-preview PR needs to be patch to preview release status/need-issue Pull requests that need to have an associated issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants