-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: handle empty responses from Gemini API #7047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Add tracking for whether any text content was yielded - Throw specific error when Gemini API returns empty response - Add translation keys for empty response error in English and Chinese - Add test coverage for empty response scenario Fixes #7046
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewing my own code because apparently I trust no one, not even myself.
console.warn("Gemini API returned empty response, no text content was generated") | ||
|
||
// Throw a specific error that can be caught and retried | ||
throw new Error( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I notice we're throwing an error with t("common:errors.gemini.empty_response")
here, but then it gets wrapped again with t("common:errors.gemini.generate_stream")
in the catch block at line 174. This could lead to nested/confusing error messages. Should we consider throwing this error in a way that bypasses the outer catch, or perhaps use a custom error type that the catch block can handle differently?
for await (const _chunk of stream) { | ||
// Should throw before yielding any chunks | ||
} | ||
}).rejects.toThrow(t("common:errors.gemini.generate_stream")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test expects t("common:errors.gemini.generate_stream")
but the actual error thrown is t("common:errors.gemini.empty_response")
. While this works due to the error wrapping in the catch block, would it be clearer to test for the specific empty response error message to make the test's intent more obvious?
// Check if we got an empty response | ||
if (!hasYieldedContent) { | ||
// Log the issue for debugging | ||
console.warn("Gemini API returned empty response, no text content was generated") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is console.warn()
intentional for production? Should we consider using a debug logger that can be toggled based on environment settings instead?
// Throw a specific error that can be caught and retried | ||
throw new Error( | ||
t("common:errors.gemini.empty_response", { | ||
error: "The Gemini API did not return any text content. This may be a temporary issue.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message includes both a translated message and a hardcoded English fallback. Could we simplify this by ensuring all necessary context is in the translation files themselves, avoiding the redundancy?
Description
This PR fixes an issue where the Gemini API returns empty responses, causing the error "The language model did not provide any assistant messages".
Problem
Users were experiencing complete failure when using Google Gemini API (gemini-2.5-pro model) with the error message indicating no assistant messages were provided. This made the application completely unusable for affected users.
Solution
Testing
Fixes #7046
Important
Fixes issue with Gemini API returning empty responses by detecting and handling them with specific error messages and adds test coverage.
createMessage()
ingemini.ts
and throws a specific error.common.json
.gemini.spec.ts
to verify handling of empty responses.This description was created by
for 351dc5f. You can customize this summary. It will automatically update as commits are pushed.