{"record":{"id":"7804b80a2ad85605","repo":"BerriAI/litellm","slug":"api-key-is-required-for-openai-image-variations","errorCode":null,"errorMessage":"API key is required for OpenAI image variations","messagePattern":"API key is required for OpenAI image variations","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/images/main.py","lineNumber":660,"sourceCode":"\n    response: ImageResponse | None = None\n\n    provider_config: Final = ProviderConfigManager.get_provider_model_info(\n        model=model or \"\",  # openai defaults to dall-e-2\n        provider=llm_provider,\n    )\n\n    if provider_config is None:\n        raise ValueError(\n            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}\"\n        )\n\n    api_key: Final = provider_config.get_api_key(litellm_params.get(\"api_key\", None))\n    api_base = provider_config.get_api_base(litellm_params.get(\"api_base\", None))\n\n    if image_variation_provider == LITELLM_IMAGE_VARIATION_PROVIDERS.OPENAI:\n        if api_key is None:\n            raise ValueError(\"API key is required for OpenAI image variations\")\n        if api_base is None:\n            raise ValueError(\"API base is required for OpenAI image variations\")\n\n        response = openai_image_variations.image_variations(\n            model_response=model_response,\n            api_key=api_key,\n            api_base=api_base,\n            model=model,\n            image=image,\n            timeout=litellm_params.get(\"timeout\", None),\n            custom_llm_provider=custom_llm_provider,\n            logging_obj=litellm_logging_obj,\n            optional_params={},\n            litellm_params=litellm_params,\n        )\n    elif image_variation_provider == LITELLM_IMAGE_VARIATION_PROVIDERS.TOPAZ:\n        if api_key is None:\n            raise ValueError(\"API key is required for Topaz image variations\")","sourceCodeStart":642,"sourceCodeEnd":678,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/images/main.py#L642-L678","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nlitellm.image_variations(model=\"dall-e-2\", image=image_file)\n\n# after\nlitellm.image_variations(model=\"dall-e-2\", image=image_file, api_key=os.environ[\"OPENAI_API_KEY\"])","handlingStrategy":"validation","validationCode":"import os\n\ndef has_openai_key(explicit: str | None = None) -> bool:\n    return bool(explicit or os.getenv(\"OPENAI_API_KEY\") or getattr(litellm, \"api_key\", None))","typeGuard":null,"tryCatchPattern":"try:\n    r = litellm.image_variations(model=\"dall-e-2\", image=img)\nexcept ValueError as e:\n    if \"API key is required for OpenAI image variations\" in str(e):\n        raise RuntimeError(\"set OPENAI_API_KEY or pass api_key=\") from e\n    raise","preventionTips":["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"],"tags":["image-variations","openai","authentication","api-key"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}