{"record":{"id":"ca0455b73c23dc06","repo":"BerriAI/litellm","slug":"stability-api-key-is-not-set-please-set-it-via-en-ca0455","errorCode":null,"errorMessage":"STABILITY_API_KEY is not set. Please set it via environment variable or pass api_key parameter.","messagePattern":"STABILITY_API_KEY is not set\\. Please set it via environment variable or pass api_key parameter\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/stability/image_generation/transformation.py","lineNumber":151,"sourceCode":"        return f\"{base_url}{endpoint}\"\n\n    def validate_environment(\n        self,\n        headers: dict,\n        model: str,\n        messages: list[AllMessageValues],\n        optional_params: dict,\n        litellm_params: dict,\n        api_key: str | None = None,\n        api_base: str | None = None,\n    ) -> dict:\n        \"\"\"\n        Validate environment and set up headers for Stability AI.\n        \"\"\"\n        final_api_key: Final[str | None] = api_key or get_secret_str(\"STABILITY_API_KEY\")\n\n        if not final_api_key:\n            raise ValueError(\n                \"STABILITY_API_KEY is not set. Please set it via environment variable or pass api_key parameter.\"\n            )\n\n        headers[\"Authorization\"] = f\"Bearer {final_api_key}\"\n        headers[\"Accept\"] = \"application/json\"\n        return headers\n\n    def transform_image_generation_request(\n        self,\n        model: str,\n        prompt: str,\n        optional_params: dict,\n        litellm_params: dict,\n        headers: dict,\n    ) -> dict:\n        \"\"\"\n        Transform OpenAI-style request to Stability AI request format.\n","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/stability/image_generation/transformation.py#L133-L169","documentation":"LiteLLM's Stability image-generation validate_environment resolves the key from the api_key argument or the STABILITY_API_KEY environment variable (get_secret_str). If neither yields a value it raises this ValueError and the generation request never leaves the process.","triggerScenarios":"litellm.image_generation(model=\"stability/...\", ...) with STABILITY_API_KEY unset/empty in the process environment and no api_key kwarg; serverless deployments where the env var was set in a different stage; notebooks after a kernel restart that lost os.environ changes.","commonSituations":"Local works / production fails because the key was only set in the developer shell; secrets managers (Vault, AWS Secrets Manager) not wired into the deployment; a .env file present but python-dotenv load_dotenv() never called; proxy restarted without the env var.","solutions":["Set STABILITY_API_KEY in the environment the process actually runs in (export, .env + load_dotenv(), CI/CD secret, container env).","Pass api_key explicitly per call or in the Router/proxy model deployment config.","Confirm the name is exactly STABILITY_API_KEY and is non-empty in that process (os.environ.get).","If using a secrets manager, configure litellm's secret manager rather than relying on plain env vars."],"exampleFix":"# before\nresp = litellm.image_generation(model=\"stability/stable-image-ultra\", prompt=\"a sunset\")\n# -> ValueError: STABILITY_API_KEY is not set...\n\n# after\nimport litellm, os\nlitellm.api_key = None\nresp = litellm.image_generation(\n    model=\"stability/stable-image-ultra\",\n    prompt=\"a sunset\",\n    api_key=os.environ[\"STABILITY_API_KEY\"],\n)","handlingStrategy":"validation","validationCode":"import os\n\n\ndef require_stability_key(api_key: str | None = None) -> str:\n    key = api_key or os.environ.get(\"STABILITY_API_KEY\")\n    if not key:\n        raise RuntimeError(\n            \"STABILITY_API_KEY missing — set it before generating images\"\n        )\n    return key\n\n\nkey = require_stability_key()\nresp = litellm.image_generation(model=\"stability/stable-image-core\", prompt=p, api_key=key)","typeGuard":null,"tryCatchPattern":"try:\n    resp = litellm.image_generation(model=\"stability/...\", prompt=p)\nexcept ValueError as e:\n    if \"STABILITY_API_KEY is not set\" in str(e):\n        raise RuntimeError(\"deploy config incomplete: add STABILITY_API_KEY\") from e\n    raise","preventionTips":["Add env-var presence checks to a startup/healthcheck hook for every provider you enable.","Load .env explicitly (python-dotenv) in dev so behavior matches production secret injection.","Store keys in the platform secret store (K8s secret, Lambda env) instead of shell rc files."],"tags":["stability-ai","image-generation","api-key","environment-variables","litellm"],"backgroundTag":"missing-api-key","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","schemaVersion":2},"datasetVersion":"2026-08-23T13:39:53.451Z"}