Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

roomote[bot]
Copy link

@roomote roomote bot commented Aug 13, 2025

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

  • Added tracking to detect when Gemini API returns chunks without any text content
  • Throw a specific error with clear messaging when empty responses are detected
  • Added translation keys for the error message in both English and Chinese
  • Added test coverage for the empty response scenario

Testing

  • Added unit test to verify empty response handling
  • All existing tests pass
  • Linting and type checking pass

Fixes #7046


Important

Fixes issue with Gemini API returning empty responses by detecting and handling them with specific error messages and adds test coverage.

  • Behavior:
    • Detects empty responses from Gemini API in createMessage() in gemini.ts and throws a specific error.
    • Logs a warning when empty responses are detected.
    • Adds English and Chinese translations for the new error message in common.json.
  • Testing:
    • Adds a unit test in gemini.spec.ts to verify handling of empty responses.
    • Ensures all existing tests pass.

This description was created by Ellipsis for 351dc5f. You can customize this summary. It will automatically update as commits are pushed.

- 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
Copy link
Author

@roomote roomote bot left a 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(
Copy link
Author

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"))
Copy link
Author

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")
Copy link
Author

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.",
Copy link
Author

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?

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:M This PR changes 30-99 lines, ignoring generated files.
Projects
Status: Triage
2 participants