Add Gemini model fallback for overload resilience#3
Merged
Conversation
Gemini returns 503 UNAVAILABLE when a model is overloaded (observed live on gemini-3.5-flash). generateJson now tries the primary model (GEMINI_MODEL) then falls through a fallback chain (GEMINI_FALLBACK_MODELS, default gemini-3-flash-preview,gemini-flash-latest,gemini-2.0-flash) on transient codes (503/500/429); non-retryable errors (bad key, invalid request) still fail fast. Client is injectable for tests. +7 unit tests (69 total).
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.
Why
Live judging exposed that
gemini-3.5-flashintermittently returns503 UNAVAILABLE("high demand"). A single-model call surfaces that as a failed AI action.What
generateJson()now tries the primary model (GEMINI_MODEL) and, on a transient/overload code (503/500/429), falls through a fallback chain (GEMINI_FALLBACK_MODELS, defaultgemini-3-flash-preview,gemini-flash-latest,gemini-2.0-flash). Non-retryable errors (bad key, invalid request) still fail fast. The client is injectable so the fallback logic is unit-tested without network.Tests
+7 unit tests (69 total):
isRetryablecode classification,modelChainordering/dedup, and the fallback loop (primary-overloaded→fallback, fail-fast on non-retryable, all-overloaded→throw, primary-success).Verified: typecheck clean,
next buildsucceeds, deployed to the live service.