refactor(backend): extract Gemini model fallback into reusable utility (DRY)#439
Merged
Conversation
Introduce utils/geminiHelper.js with two exported functions:
- generateWithFallback(apiKey, parts, options, retries, initialDelay)
Tries candidate models in order (env GEMINI_MODEL first, then the
hardcoded fallback list) and supports exponential-backoff retries for
transient errors (429 / 503 / timeout / network).
- generateChatWithFallback(apiKey, prompt, history, options)
Same model-fallback strategy but initialises a Gemini chat session
(startChat + sendMessage) for the conversational AI endpoint.
The duplicated ~15-line fallback loops that appeared in 6 locations
across 5 files are removed and replaced with single calls to the helper:
controllers/aiController.js — 3 loops → 3 helper calls
controllers/resumeController.js — 1 loop → 1 helper call
routes/aiRoutes.js — 1 loop → 1 helper call (chat)
routes/AptitudeQuestions.js — 1 loop + local isRetryableError
+ generateWithRetry → 1 helper call
Benefits:
* Single source of truth for model list and fallback order
* Retry logic (previously only in AptitudeQuestions) is now available
to all callers via the retries parameter
* ~90 lines of boilerplate removed
* Fallback helper is independently unit-testable
Closes #<issue-number>
Contributor
Author
|
Hello @KaranUnique, I have completed my assigned work so please review the changes and merge the PR under ECSoC. |
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.
Summary
The Gemini model fallback loop was duplicated across 6 locations in 5 files.
This PR extracts it into a single reusable utility at
utils/geminiHelper.js.Changes
New:
utils/geminiHelper.jsgenerateWithFallback(apiKey, parts, options?, retries?, initialDelay?)— triescandidate models in order (env var first), with optional exponential-backoff
retries for transient errors (429, 503, timeout, network)
generateChatWithFallback(apiKey, prompt, history?, options?)— same fallbackstrategy for chat-session endpoints using
startChat+sendMessageUpdated files
controllers/aiController.jscontrollers/resumeController.jsroutes/aiRoutes.jsroutes/AptitudeQuestions.jsBenefits
Testing
All existing behaviour is preserved. No API surface changed.
Closes #435