Repository navigation
✨ feat: Discover Google Models from the Gemini API - #18588
owahltinez wants to merge 4 commits into
Conversation
With a server GOOGLE_KEY and no GOOGLE_MODELS or configured list, list the models the key can access instead of the static defaults. Known defaults keep their order and newer Gemini chat models follow, so new releases appear without a config change. User-provided keys, reverse proxies and Vertex keep the defaults, as does a failed request.
Gate discovery behind endpoints.google.fetch so existing deployments keep the default list. Accept only versioned gemini-<n> IDs, since aliases such as gemini-flash-latest miss the version-based thinking rules, match non-chat tokens on hyphen boundaries, and fall back to defaults when nothing listed can chat.
Compose the discovered list as a Set union so duplicate listings collapse, and assert both default-model loaders forward the configured Google list and the fetch opt-in.
There was a problem hiding this comment.
🟡 Changes recommended
A critical validation bug can reject valid Vertex agents when discovery is enabled alongside both Google API key and Vertex credentials, because Vertex models are incorrectly checked against the Google Developer API catalog.
1 open issue
What changed in this PR
Adds opt-in Gemini API model discovery to LibreChat’s built-in Google endpoint, allowing new chat models to appear without manually updating model lists.
Changes:
- Adds
endpoints.google.fetchand documents it in the example configuration. - Reuses shared model fetching, filters results to versioned Gemini chat models, and preserves configured-list precedence and fallback behavior.
- Updates both model loaders and adds discovery, filtering, fallback, and configuration-forwarding tests.
| File | Description |
|---|---|
packages/data-provider/src/config.ts |
Adds the optional Google discovery flag. |
packages/api/src/endpoints/models.ts |
Implements discovery, filtering, ordering, and fallbacks. |
packages/api/src/endpoints/models.spec.ts |
Tests discovery behavior and configured-list precedence. |
packages/api/src/endpoints/config/defaults.ts |
Forwards Google configuration to the scoped loader. |
packages/api/src/endpoints/config/defaults.spec.ts |
Tests scoped-loader configuration forwarding. |
librechat.example.yaml |
Documents the discovery option. |
api/server/services/Config/loadDefaultModels.spec.js |
Tests legacy-loader forwarding and async behavior. |
api/server/services/Config/loadDefaultModels.js |
Wires Google discovery options into the legacy loader. |
Review effort: Balanced
| if (chat.size === 0) { | ||
| return models; | ||
| } | ||
| return [...new Set([...models.filter((id) => chat.has(id)), ...chat])]; |
There was a problem hiding this comment.
Confirmed: validateAgentModel resolves a vertexai agent to the google catalog, so narrowing it to the Developer API listing could reject a valid Vertex model. Fixed in 1dff98b: discovery now only appends listed chat models the defaults lack ([...new Set([...defaults, ...listed])]), so no default is ever removed and Vertex validation sees a superset of today's catalog. The discovery test now asserts the full defaults come first with gemini-4-neon appended; against the live API the result is the 15 defaults plus gemini-3.1-flash-lite, gemini-4-carbon and gemini-4-neon.
Vertex agents validate against the google catalog, so narrowing it to the Developer API listing could reject a valid Vertex model. Discovery now only appends listed chat models the defaults lack.

Pull Request
Summary
The built-in Google endpoint never asks the Gemini API which models exist.
getGoogleModels()returnsGOOGLE_MODELSor the hardcodeddefaultModels.google, so every new Gemini release stays invisible until an admin editsGOOGLE_MODELS(orendpoints.google.models) by hand, or a LibreChat release adds it to the defaults. OpenAI and Anthropic already discover their models from the provider; Google is the odd one out.This PR adds an opt-in
endpoints.google.fetch: true. With it set and a serverGOOGLE_KEY, the Google endpoint lists the models the key can access through the Gemini API's OpenAI-compatible/v1beta/openai/models, using the sharedfetchModelshelper (two-minuteMODEL_QUERIEScache, in-flight sharing, proxy support, error logging). Without the flag, behavior is unchanged.Discovered IDs are reduced to chat models: versioned
gemini-<n>IDs only, since aliases such asgemini-flash-latestmiss the version-basedthinkingLevelrule ingoogle/llm.ts, minus hyphen-delimited non-chat variants (tts,image,live,audio,transcribe,translate,computer-use). Discovery only appends: listed chat models the defaults lack follow the defaults, and no default is removed. That keeps the default model unchanged and keeps Vertex agents valid, sincevalidateAgentModelchecks avertexaiagent against thegooglecatalog and the Developer API listing says nothing about Vertex. A configuredmodelslist orGOOGLE_MODELSstill wins, and a user-provided key,GOOGLE_REVERSE_PROXY, no serverGOOGLE_KEY, a failed request or a listing with no new chat model all return the defaults unchanged.How it works
Precedence in
getGoogleModels:Both default-model loaders pass the endpoint config through, so
/apionly gains wiring:Type of change
Testing
GOOGLE_KEYto a Gemini Developer API key and leaveGOOGLE_MODELSunset.fetch,getGoogleModels()returns the 15 defaults and makes no request.endpoints.google.fetch: true, it returned the 15 defaults followed bygemini-3.1-flash-lite,gemini-4-carbonandgemini-4-neon, with no aliases, TTS, image, live, embedding or Gemma entries.Tested environments/configuration:
/v1beta/openai/modelslistingAutomated tests:
packages/api/src/endpoints/models.spec.ts: opt-in default,GOOGLE_MODELSand configured-list precedence, no-key / user-provided / reverse-proxy skips, filtering against real listing IDs, defaults kept ahead of appended models, duplicate collapse, failure and empty-listing fallbackspackages/api/src/endpoints/config/defaults.spec.tsandapi/server/services/Config/loadDefaultModels.spec.js: both loaders forwardconfiguredModelsandfetchnpm run static-checks -- --against upstream/dev --fullpasses (TypeScript and config migration tests included)Screenshots / recordings
No visual change. With
fetch: true, the existing Google model picker lists the discovered models.Risk / compatibility
endpoints.google.fetchsee no change.generativelanguage.googleapis.comper two-minute cache window (5 s timeout, shared in flight). Not measured beyond that.gemini-4inutils/tokens.ts, the version check ingoogle/llm.ts); a future model that needs bespoke request handling still needs a code change, as today.npm run lighthousewas not run.Checklist
fetchfield is documented inlibrechat.example.yaml; a librechat.ai docs PR is not opened yet