BerriAI/litellm · error · Exception

Failed to get models

Error message

Failed to get models: {response.text}

What it means

Raised when the GET {base_url}/v1/models request used to discover OpenAI-compatible models returns a non-success response; the response body text is embedded verbatim. Typically caused by a wrong api_base, an invalid/expired API key, or a backend that does not implement the /v1/models listing endpoint.

Solutions

  1. Check the response body in the message; verify the API key has permission to list models and the base URL is correct.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at litellm/llms/openai/chat/gpt_transformation.py:711 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/db7eb74e3fdb74c4. Report an issue: GitHub.

Appendix: 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 77b7c6c40c)