fix(core): decode byte-array encoded API error messages#19855
fix(core): decode byte-array encoded API error messages#19855Rohan-Mohite14 wants to merge 6 commits intogoogle-gemini:mainfrom
Conversation
Summary of ChangesHello @Rohan-Mohite14, 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 addresses an issue where API error messages from the Gemini API were sometimes displayed as unreadable byte-array encoded strings. By adding decoding logic, the change ensures that all error messages are converted into human-readable UTF-8 text, significantly enhancing the clarity and understandability of error feedback within the CLI. Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces logic to decode byte-array encoded error messages from the Gemini API, which significantly improves the readability of errors in the CLI. No specific security vulnerabilities were identified in this change. General feedback focuses on ensuring the decoding process is robust against non-ASCII characters and large inputs by using TextDecoder instead of String.fromCharCode, and adjusting the new code to follow the established coding style of using braces for all conditional blocks.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
| @@ -40,6 +52,16 @@ export function parseAndFormatApiError( | |||
| fallbackModel?: string, | |||
| ): string { | |||
| if (isStructuredError(error)) { | |||
| const decoded = tryDecodeByteArray(error.message); | |||
| if (decoded) { | |||
| return parseAndFormatApiError( | |||
There was a problem hiding this comment.
I noticed an edge case here.
When the code successfully decodes error.message and recursively calls
parseAndFormatApiError(decoded, ...), it passes only the new string and
leaves behind the original outer object, dropping error.status.
Timeline of what happens if the API returns a 429 Error:
- Input at line 47:
{ message: "78,111...", status: 429 } - Line 55:
tryDecodeByteArraydecodes the message into the string
"No capacity available". - Line 57: The recursive call triggers, passing only the new string:
parseAndFormatApiError("No capacity available", ...) - The function restarts at line 47. Because the input is now a plain string
instead of an object,statusno longer exists. - Line 66 (
if (error.status === 429)) is never reached.
Actual Output: [API Error: No capacity available]
Expected Output:
[API Error: No capacity available]
Please wait and try again later. To increase your limits...
We should clone the error object here so we can preserve the original HTTP
status before recursing:
if (decoded) {
return parseAndFormatApiError(
{ ...error, message: decoded },
authType,
userTier,
currentModel,
fallbackModel,
);|
adding this decoding logic — it makes the CLI error messages much more readable when the API returns byte-array formatted messages. While reviewing the call chain, I noticed that the CLI itself doesn’t directly perform the HTTP request. The request flow appears to be: This suggests the byte-array formatted error may originate upstream in the @google/genai SDK. It’s possible the SDK is converting a Uint8Array (or ArrayBuffer) to a string without decoding it as UTF-8, which would produce the numeric byte sequence we’re seeing. The current workaround here works well for the CLI, but it might also be worth checking whether the SDK is returning the error body via something like response.arrayBuffer() rather than response.text(). If that’s the case, decoding earlier in the SDK could eliminate the need for this workaround downstream. |
|
@Rohan-Mohite14 The existing This change strips brackets and whitespace before decoding so both formats 91,123,10 |
Summary
API error messages from the Gemini API are sometimes displayed as raw ASCII byte codes (e.g.,
91,123,10,32,34,101,...) instead of readable text. This PR adds decoding logic to convert these byte-array encoded error messages into readable UTF-8 text before formatting and displaying them in the CLI. This improves usability and ensures error messages are understandable.Details
The
@google/genaiSDK occasionally returns HTTP error response bodies as aUint8Array. When this gets stringified into anError.message, it produces comma-separated ASCII byte values instead of readable JSON error text.The
parseAndFormatApiError()function inpackages/core/src/utils/errorParsing.tsdid not previously detect or decode this format.This PR introduces a helper function
tryDecodeByteArray()that:This decoding is applied in
parseAndFormatApiError()in two scenarios:StructuredErrorobjects (decoding.message)This ensures all API error messages are properly decoded and formatted before being displayed in the CLI.
Related Issues
Fixes #19851
How to Validate
Run the unit tests:
npm test -w @google/gemini-cli-core -- src/utils/errorParsing.test.tsVerify that:
Manual validation:
Expected result:
Pre-Merge Checklist
Linked PR to existing issue (API error messages displayed as raw byte codes instead of decoded text when model capacity is exhausted #19851)
Added/updated tests
Noted breaking changes (none expected)
Ran preflight checks:
Validated on required platforms/methods:
macOS
Windows
Linux