BerriAI/litellm · error · ValueError

OPENAI_API_KEY is required for Evals API

Error message

OPENAI_API_KEY is required for Evals API

What it means

litellm's OpenAI Evals API integration requires an API key before it can build Authorization headers. It checks, in order: litellm_params.api_key, litellm.api_key, litellm.openai_key, and the OPENAI_API_KEY environment variable (via the secret manager). If none is found it raises ValueError('OPENAI_API_KEY is required for Evals API') because anonymous access to the Evals API is impossible.

Solutions

  1. Export OPENAI_API_KEY in the environment where the code runs: export OPENAI_API_KEY=sk-... (or add it to your .env and load it)
  2. Pass the key explicitly in the call params (api_key=...) or set litellm.api_key = 'sk-...' before calling evals
  3. For Docker/CI, inject the variable at runtime (docker run -e OPENAI_API_KEY=..., GitHub/Actions secret, etc.) rather than baking it into images
  4. If using a secret manager, confirm get_secret_str('OPENAI_API_KEY') can actually resolve it (correct provider enabled and configured)

Example fix

# before
result = litellm.analyze_evals(...)  # ValueError: OPENAI_API_KEY is required for Evals API

# after
import litellm
litellm.api_key = os.environ["OPENAI_API_KEY"]  # or pass api_key=... in params
result = litellm.analyze_evals(...)
Defensive patterns

Strategy: validation

Validate before calling

import os, litellm
from litellm.secret_managers.main import get_secret_str

def has_openai_key_for_evals() -> bool:
    return bool(
        getattr(litellm, "api_key", None)
        or getattr(litellm, "openai_key", None)
        or get_secret_str("OPENAI_API_KEY")
        or os.getenv("OPENAI_API_KEY")
    )

assert has_openai_key_for_evals(), "set OPENAI_API_KEY before running evals"

Try / catch

try:
    run_evals()
except ValueError as e:
    if "OPENAI_API_KEY is required" in str(e):
        raise SystemExit("Export OPENAI_API_KEY and re-run") from e
    raise

Prevention

When it happens

Trigger: Calling litellm evals endpoints (create/retrieve/list runs, e.g. via litellm.evaluate or the evals handler) without api_key in the request params, without litellm.api_key/litellm.openai_key being set globally, and without OPENAI_API_KEY exported in the environment (or present in a .env/Doppler/etc. source the secret manager reads).

Common situations: CI jobs and Docker containers where the env var was never injected; notebooks that call evals before litellm.api_key = 'sk-...' is set; .env files not loaded because python-dotenv was never invoked; using Azure or other providers for chat but calling OpenAI Evals without realizing it specifically needs an OpenAI key.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/10b353834380bb42. Report an issue: GitHub.

Appendix: source

Thrown at litellm/llms/openai/evals/transformation.py:59

    @property
    def custom_llm_provider(self) -> LlmProviders:
        return LlmProviders.OPENAI

    def validate_environment(self, headers: dict, litellm_params: GenericLiteLLMParams | None) -> dict:
        """Add OpenAI-specific headers"""
        import litellm
        from litellm.secret_managers.main import get_secret_str

        # Get API key following OpenAI pattern
        api_key = None
        if litellm_params:
            api_key = litellm_params.api_key

        api_key = api_key or litellm.api_key or litellm.openai_key or get_secret_str("OPENAI_API_KEY")

        if not api_key:
            raise ValueError("OPENAI_API_KEY is required for Evals API")

        # Add required headers
        headers["Authorization"] = f"Bearer {api_key}"
        headers["Content-Type"] = "application/json"

        return headers

    def get_complete_url(
        self,
        api_base: str | None,
        endpoint: str,
        eval_id: str | None = None,
    ) -> str:
        """Get complete URL for OpenAI Evals API"""
        if api_base is None:
            api_base = "https://api.openai.com"

        if eval_id:

View on GitHub (pinned to 77b7c6c40c)