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

On the OpenAI branch of litellm.image_variation, the api key is resolved via provider_config.get_api_key(litellm_params.get('api_key')), which falls back to the OPENAI_API_KEY environment variable and litellm module-level key. If that chain yields None, litellm refuses to make the unsigned /v1/images/variations request and raises this ValueError.

Solutions

  1. Export OPENAI_API_KEY in the environment running litellm.
  2. Or pass the key explicitly: litellm.image_variation(..., api_key='sk-...').
  3. In the proxy, define the key via litellm_settings/environment config or a secret manager rather than relying on an inherited shell.
  4. Fail fast at startup: assert os.getenv('OPENAI_API_KEY') before serving image-variation requests.

Example fix

# before
resp = litellm.image_variation(model="dall-e-2", image=img, api_base="https://api.openai.com/v1")
# ValueError: API key is required for OpenAI image variations

# after
export OPENAI_API_KEY=sk-...
# or
resp = litellm.image_variation(model="dall-e-2", image=img, api_base="https://api.openai.com/v1", api_key="sk-...")
Defensive patterns

Strategy: validation

Validate before calling

import os

api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
    raise RuntimeError("OPENAI_API_KEY not set; required for openai image variations")
resp = litellm.image_variation(model="dall-e-2", image=img, api_key=api_key, api_base="https://api.openai.com/v1")

Try / catch

try:
    resp = litellm.image_variation(model="dall-e-2", image=img, api_base=base)
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 image_variation(model='dall-e-2', image=..., api_base='...') with no api_key argument while OPENAI_API_KEY is unset in the process; running in a container/CI where the env var was never injected; having set the key only on a different litellm module attribute that this path does not read.

Common situations: Scripts that work locally (key in shell profile) but fail in Docker, systemd, cron, or CI where the env var is absent; proxy deployments missing the secret in the environment; explicit api_key=None overriding nothing.

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/7804b80a2ad85605. Report an issue: GitHub.

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