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
- Export OPENAI_API_KEY in the environment of the running process
- Or pass the key inline: litellm.image_variations(model='dall-e-2', image=img, api_key='sk-...')
- Or set litellm.api_key = 'sk-...' globally before the call
- 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
- Fail fast at startup if required provider env vars are missing
- Pass api_key explicitly in server contexts instead of relying on ambient env
- Add env-var checks to deployment checklists
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
- API key is required for Topaz image variations
- Unclassified keys in {PRICES_PATH.name}: {', '.join(unclassi
- No Braintrust API token provided. Pass via Authorization hea
- Error: {response.status_code} - {response.text}
- Missing Authorization header
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/7804b80a2ad85605.
Report an issue: GitHub.