BerriAI/litellm · error · ValueError
Failed to fetch models from Gemini. Status code
Error message
Failed to fetch models from Gemini. Status code: {response.status_code}, Response: {response.json()} What it means
Error "Failed to fetch models from Gemini. Status code: {response.status_code}, Response: {response.json()}" thrown in BerriAI/litellm.
Solutions
- Read the status code and response body in the error; the /models request was rejected.
- Verify the API key is valid and has access to the Generative Language API.
When it happens
Trigger: Thrown at litellm/llms/gemini/common_utils.py:397 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/9c1dafde919d9914.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/llms/gemini/common_utils.py:397
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
def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BaseLLMException:
return GeminiError(status_code=status_code, message=error_message, headers=headers)
def get_token_counter(self) -> BaseTokenCounter | None:
"""
Factory method to create a token counter for this provider.
Returns:
Optional TokenCounterInterface implementation for this provider,
or None if token counting is not supported.View on GitHub (pinned to 77b7c6c40c)