Fix Gemini driver retry mechanism#2331
Merged
danielaskdd merged 1 commit intoHKUDS:mainfrom Nov 8, 2025
Merged
Conversation
• Add google-api-core dependency • Add specific exception handling • Create InvalidResponseError class • Update retry decorators • Fix empty response handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔧 Fix Gemini driver retry mechanism
Problem
The Gemini LLM driver had two critical issues:
Missing Retry Mechanism: When concurrent requests reached the 8th request, the system would fail with
RuntimeError: Gemini response did not contain any text contentwithout any retry attempts, causing document processing to crash.Missing Dependency: The code imported
google.api_core.exceptionsbutgoogle-api-corewas not listed as a dependency, causingModuleNotFoundErroron fresh installations.Overly Broad Retry Logic: The initial fix used a too-broad
Exceptionretry that would retry non-recoverable errors like authentication failures and configuration errors, wasting time and resources.Changes Made
1. Enhanced Retry Logic (
lightrag/llm/gemini.py)InvalidResponseErrorexception class for empty response scenariosgoogle.api_core.exceptions:InternalServerError(500) - Server errorsServiceUnavailable(503) - Service unavailableResourceExhausted(429) - Rate limitingGatewayTimeout(504) - Gateway timeoutBadGateway(502) - Bad gatewayDeadlineExceeded- Request timeoutAborted(409) - Concurrency conflictsUnknown(500) - Unknown server errorsInvalidResponseError- Empty response errorsgoogle-api-coregemini_complete_if_cacheandgemini_embedfunctions2. Dependency Management
pyproject.toml:
google-api-core>=2.0.0,<3.0.0to core dependenciesapioptional dependenciesoffline-llmoptional dependenciesrequirements-offline-llm.txt:
google-api-core>=2.0.0,<3.0.0Benefits
✅ Fixes concurrent request failures - Properly handles transient errors on the 8th concurrent request
✅ Smart retry strategy - Only retries recoverable errors, fails fast on permanent errors
✅ Prevents dependency issues - Automatic installation ensures
google-api-coreis always available✅ Handles rate limiting - Exponential backoff for 429 errors
✅ Production-ready - Enterprise-grade error handling matching industry best practices
✅ Consistent with OpenAI driver - Similar retry logic across LLM bindings
Errors That Will Be Retried (Max 3 attempts)
Errors That Will Fail Immediately (No retry)
ValueError)TypeError,AttributeError)Testing Recommendations
google-api-coreauto-installsFiles Changed
lightrag/llm/gemini.py- Enhanced retry logic and dependency installationpyproject.toml- Addedgoogle-api-coredependency (3 locations)requirements-offline-llm.txt- Addedgoogle-api-corefor offline installsBreaking Changes
None. This is a backward-compatible improvement.
Related Issues
Fixes:
ModuleNotFoundError: No module named 'google.api_core'