BerriAI/litellm · error · ValueError

API key is required for OpenAI image variations

Error message

API key is required for OpenAI image variations

What it means

For OpenAI image variations, litellm resolves the API key via provider_config.get_api_key() (litellm_params 'api_key', then environment). If it is still None at litellm/images/main.py:660, it raises ValueError — OpenAI's /images/variations endpoint requires a key.

Source

Thrown at litellm/images/main.py:660

    response: ImageResponse | None = None

    provider_config: Final = ProviderConfigManager.get_provider_model_info(
        model=model or "",  # openai defaults to dall-e-2
        provider=llm_provider,
    )

    if provider_config is None:
        raise ValueError(
            f"image variation provider has no known model info config - required for getting api keys, etc.: {custom_llm_provider}. Supported providers are: {LITELLM_IMAGE_VARIATION_PROVIDERS}"
        )

    api_key: Final = provider_config.get_api_key(litellm_params.get("api_key", None))
    api_base = provider_config.get_api_base(litellm_params.get("api_base", None))

    if image_variation_provider == LITELLM_IMAGE_VARIATION_PROVIDERS.OPENAI:
        if api_key is None:
            raise ValueError("API key is required for OpenAI image variations")
        if api_base is None:
            raise ValueError("API base is required for OpenAI image variations")

        response = openai_image_variations.image_variations(
            model_response=model_response,
            api_key=api_key,
            api_base=api_base,
            model=model,
            image=image,
            timeout=litellm_params.get("timeout", None),
            custom_llm_provider=custom_llm_provider,
            logging_obj=litellm_logging_obj,
            optional_params={},
            litellm_params=litellm_params,
        )
    elif image_variation_provider == LITELLM_IMAGE_VARIATION_PROVIDERS.TOPAZ:
        if api_key is None:
            raise ValueError("API key is required for Topaz image variations")

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Export OPENAI_API_KEY in the environment of the running process
  2. Or pass the key inline: litellm.image_variations(model='dall-e-2', image=img, api_key='sk-...')
  3. Or set litellm.api_key = 'sk-...' globally before the call
  4. Check for whitespace/quotes around the env var value in .env files or CI secret settings

Example fix

# before
litellm.image_variations(model="dall-e-2", image=image_file)

# after
litellm.image_variations(model="dall-e-2", image=image_file, api_key=os.environ["OPENAI_API_KEY"])
Defensive patterns

Strategy: validation

Validate before calling

import os

def has_openai_key(explicit: str | None = None) -> bool:
    return bool(explicit or os.getenv("OPENAI_API_KEY") or getattr(litellm, "api_key", None))

Try / catch

try:
    r = litellm.image_variations(model="dall-e-2", image=img)
except ValueError as e:
    if "API key is required for OpenAI image variations" in str(e):
        raise RuntimeError("set OPENAI_API_KEY or pass api_key=") from e
    raise

Prevention

When it happens

Trigger: Calling litellm.image_variations(model='dall-e-2', image=...) with no api_key argument, no litellm.api_key set, and no OPENAI_API_KEY in the environment; or env var name typo; or secrets loaded after import in a different process.

Common situations: Local scripts where OPENAI_API_KEY is only set in the shell that launched a different process; CI pipelines missing the secret; key stored under a custom name never passed through.

Related errors


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