BerriAI/litellm · error · Exception

Failed to get models: {response.text}

Error message

Failed to get models: {response.text}

What it means

The OpenAI transformation's model-listing helper GETs {base_url}/v1/models with a Bearer token. Any non-200 raises a generic Exception with the response body. This surfaces auth failures, wrong URLs, and servers that don't implement /v1/models.

Source

Thrown at litellm/llms/openai/chat/gpt_transformation.py:711

        if api_base is None:
            api_base = "https://api.openai.com"
        if api_key is None:
            api_key = get_secret_str("OPENAI_API_KEY")

        # Strip api_base to just the base URL (scheme + host + port)
        parsed_url: Final = httpx.URL(api_base)
        base_url = f"{parsed_url.scheme}://{parsed_url.host}"
        if parsed_url.port:
            base_url += f":{parsed_url.port}"

        response: Final = litellm.module_level_client.get(
            url=f"{base_url}/v1/models",
            headers={"Authorization": f"Bearer {api_key}"},
        )

        if response.status_code != 200:
            raise Exception(f"Failed to get models: {response.text}")

        models: Final = response.json()["data"]
        return [model["id"] for model in models]

    @staticmethod
    def get_api_key(api_key: str | None = None) -> str | None:
        return api_key or litellm.api_key or litellm.openai_key or get_secret_str("OPENAI_API_KEY")

    @staticmethod
    def get_api_base(api_base: str | None = None) -> str | None:
        return (
            api_base
            or litellm.api_base
            or get_secret_str("OPENAI_BASE_URL")
            or get_secret_str("OPENAI_API_BASE")
            or "https://api.openai.com/v1"
        )

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. curl -H "Authorization: Bearer $KEY" {base}/v1/models to see the exact status/body returned.
  2. Verify the API key is set and valid (OPENAI_API_KEY env var or api_key arg).
  3. If the server lacks /v1/models, list models another way (provider docs) instead of relying on this helper.
  4. Note that any path in api_base is stripped to the host root — make sure the host root is where /v1/models lives.
Defensive patterns

Strategy: try-catch

Try / catch

try:
    models = get_models(api_key, api_base)
except Exception as e:
    if "Failed to get models" in str(e):
        # body of the failed /v1/models response is in the message
        log.warning("model listing unavailable: %s", e)
        models = FALLBACK_MODEL_LIST

Prevention

When it happens

Trigger: Calling the helper (e.g. get_models) with an invalid API key (401), an api_base whose host doesn't serve /v1/models (404), or rate-limited/keyless access (429/403). The api_base is stripped to scheme://host[:port], so path components in api_base are discarded.

Common situations: Using an OpenAI-compatible server without a /v1/models route, expired OPENAI_API_KEY, api_base with a path prefix that gets stripped, or firewall rules blocking the models endpoint specifically.

Related errors


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