Skip to content

✨ feat: Discover Google Models from the Gemini API - #18588

Open
owahltinez wants to merge 4 commits into
LibreChat-AI:devfrom
owahltinez:google-fetch-models
Open

owahltinez wants to merge 4 commits into
LibreChat-AI:devfrom
owahltinez:google-fetch-models

Conversation

@owahltinez

@owahltinez owahltinez commented Nov 20, 2026 •

Copy link
Copy Markdown

Pull Request

Summary

The built-in Google endpoint never asks the Gemini API which models exist. getGoogleModels() returns GOOGLE_MODELS or the hardcoded defaultModels.google, so every new Gemini release stays invisible until an admin edits GOOGLE_MODELS (or endpoints.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 server GOOGLE_KEY, the Google endpoint lists the models the key can access through the Gemini API's OpenAI-compatible /v1beta/openai/models, using the shared fetchModels helper (two-minute MODEL_QUERIES cache, 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 as gemini-flash-latest miss the version-based thinkingLevel rule in google/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, since validateAgentModel checks a vertexai agent against the google catalog and the Developer API listing says nothing about Vertex. A configured models list or GOOGLE_MODELS still wins, and a user-provided key, GOOGLE_REVERSE_PROXY, no server GOOGLE_KEY, a failed request or a listing with no new chat model all return the defaults unchanged.

How it works

Precedence in getGoogleModels:

endpoints.google.models     -> returned as configured
GOOGLE_MODELS               -> returned as listed
!fetch | no/user key | proxy -> defaultModels.google
fetchModels(/v1beta/openai/models)
  strip "models/", keep /^gemini-\d/ minus non-chat tokens
                            -> unique([...defaultModels.google, ...listed chat models])

Both default-model loaders pass the endpoint config through, so /api only gains wiring:

 [EModelEndpoint.google]: () =>
-  loaders.getGoogleModels(),
+  loaders.getGoogleModels({
+    configuredModels: configuredModelList(appConfig, EModelEndpoint.google),
+    fetch: appConfig.endpoints?.google?.fetch,
+  }),

Type of change

  • Feature

Testing

  1. Set GOOGLE_KEY to a Gemini Developer API key and leave GOOGLE_MODELS unset.
  2. Without fetch, getGoogleModels() returns the 15 defaults and makes no request.
  3. With endpoints.google.fetch: true, it returned the 15 defaults followed by gemini-3.1-flash-lite, gemini-4-carbon and gemini-4-neon, with no aliases, TTS, image, live, embedding or Gemma entries.

Tested environments/configuration:

  • Provider/model: Gemini Developer API (API key), live /v1beta/openai/models listing
  • OS: macOS, Node 26

Automated tests:

  • packages/api/src/endpoints/models.spec.ts: opt-in default, GOOGLE_MODELS and 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 fallbacks
  • packages/api/src/endpoints/config/defaults.spec.ts and api/server/services/Config/loadDefaultModels.spec.js: both loaders forward configuredModels and fetch
  • npm run static-checks -- --against upstream/dev --full passes (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

  • Opt-in: deployments without endpoints.google.fetch see no change.
  • With it enabled, loading Google models adds one GET to generativelanguage.googleapis.com per two-minute cache window (5 s timeout, shared in flight). Not measured beyond that.
  • Discovered models outside the defaults rely on existing prefix-based context and thinking rules (gemini-4 in utils/tokens.ts, the version check in google/llm.ts); a future model that needs bespoke request handling still needs a code change, as today.
  • Defaults the key cannot access stay listed, as today; discovery never narrows the catalog.
  • Gemma models are not surfaced, matching the defaults.
  • npm run lighthouse was not run.

Checklist

  • I reviewed my own changes
  • Relevant tests have been added or updated
  • Existing relevant tests pass
  • The change does not introduce new warnings or errors
  • User-facing or complex behavior is documented where necessary
  • Required dependency changes have been merged/published
  • Required documentation PR: the fetch field is documented in librechat.example.yaml; a librechat.ai docs PR is not opened yet

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.
Copilot AI balanced review requested due to automatic review settings November 20, 2026 13:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.fetch and 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

Comment thread packages/api/src/endpoints/models.ts Outdated
if (chat.size === 0) {
return models;
}
return [...new Set([...models.filter((id) => chat.has(id)), ...chat])];

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants