BerriAI/litellm · error · ValueError

GEMINI_API_BASE or GEMINI_API_KEY/GOOGLE_API_KEY is not set.

Error message

GEMINI_API_BASE or GEMINI_API_KEY/GOOGLE_API_KEY is not set. Please set the environment variable, to query Gemini's `/models` endpoint.

What it means

GeminiModelInfo.get_models() needs both an API base (GEMINI_API_BASE, defaulting to Google's generativelanguage.googleapis.com) and an API key (explicit, GOOGLE_API_KEY, or GEMINI_API_KEY) before it can call GET /{version}/models. If either resolves to None it raises this ValueError instead of making an unauthenticated request.

Source

Thrown at litellm/llms/gemini/common_utils.py:387

    @staticmethod
    def get_base_model(model: str) -> str | None:
        return model.replace("gemini/", "")

    def process_model_name(self, models: list[dict[str, str]]) -> list[str]:
        litellm_model_names: Final = []
        for model in models:
            stripped_model_name = model["name"].replace("models/", "")
            litellm_model_name = "gemini/" + stripped_model_name
            litellm_model_names.append(litellm_model_name)
        return litellm_model_names

    def get_models(self, api_key: str | None = None, api_base: str | None = None) -> list[str]:
        api_base = GeminiModelInfo.get_api_base(api_base)
        api_key = GeminiModelInfo.get_api_key(api_key)
        endpoint: Final = f"/{self.api_version}/models"
        if api_base is None or api_key is None:
            raise ValueError(
                "GEMINI_API_BASE or GEMINI_API_KEY/GOOGLE_API_KEY is not set. Please set the environment variable, to query Gemini's `/models` endpoint."
            )

        response: Final = litellm.module_level_client.get(
            url=f"{api_base}{endpoint}",
            headers={"x-goog-api-key": api_key},
        )

        if response.status_code != 200:
            raise ValueError(
                f"Failed to fetch models from Gemini. Status code: {response.status_code}, Response: {response.json()}"
            )

        models: Final = response.json()["models"]

        litellm_model_names: Final = self.process_model_name(models)
        return litellm_model_names

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Export GOOGLE_API_KEY or GEMINI_API_KEY (AI Studio key) before listing models.
  2. Or pass the key programmatically: litellm.get_model_list('gemini', api_key='AIza...').
  3. Ensure GEMINI_API_BASE, if set, is a non-empty valid URL (or unset it to use the default endpoint).

Example fix

# before
models = litellm.get_model_list("gemini")  # no key in env -> ValueError

# after
import os
os.environ["GOOGLE_API_KEY"] = "AIza..."
models = litellm.get_model_list("gemini")
Defensive patterns

Strategy: validation

Validate before calling

import os

def can_list_gemini_models() -> bool:
    return bool(os.getenv("GOOGLE_API_KEY") or os.getenv("GEMINI_API_KEY")) and bool(
        os.getenv("GEMINI_API_BASE") or True
    )

Try / catch

try:
    models = litellm.get_model_list("gemini")
except ValueError as e:
    if "GEMINI_API_KEY/GOOGLE_API_KEY is not set" in str(e):
        raise RuntimeError("Gemini credentials missing; cannot discover models") from e
    raise

Prevention

When it happens

Trigger: litellm.get_model_list('gemini') (or another path into this get_models) with no Google/Gemini key in the environment and no key passed, or with GEMINI_API_BASE explicitly set to an empty value.

Common situations: Startup code that auto-discovers models before credentials are loaded; env vars defined in a .env that was never sourced in the deployed process; key present but empty string, which get_api_key treats as unset.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/d8fa7233a57dd2fd. Report an issue: GitHub.