BerriAI/litellm · error · ValueError
STABILITY_API_KEY is not set. Please set it via environment
Error message
STABILITY_API_KEY is not set. Please set it via environment variable or pass api_key parameter.
What it means
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.
Source
Thrown at litellm/llms/stability/image_generation/transformation.py:151
return f"{base_url}{endpoint}"
def validate_environment(
self,
headers: dict,
model: str,
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
api_key: str | None = None,
api_base: str | None = None,
) -> dict:
"""
Validate environment and set up headers for Stability AI.
"""
final_api_key: Final[str | None] = api_key or get_secret_str("STABILITY_API_KEY")
if not final_api_key:
raise ValueError(
"STABILITY_API_KEY is not set. Please set it via environment variable or pass api_key parameter."
)
headers["Authorization"] = f"Bearer {final_api_key}"
headers["Accept"] = "application/json"
return headers
def transform_image_generation_request(
self,
model: str,
prompt: str,
optional_params: dict,
litellm_params: dict,
headers: dict,
) -> dict:
"""
Transform OpenAI-style request to Stability AI request format.
View on GitHub (pinned to 77b7c6c40c)
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.
Example fix
# before
resp = litellm.image_generation(model="stability/stable-image-ultra", prompt="a sunset")
# -> ValueError: STABILITY_API_KEY is not set...
# after
import litellm, os
litellm.api_key = None
resp = litellm.image_generation(
model="stability/stable-image-ultra",
prompt="a sunset",
api_key=os.environ["STABILITY_API_KEY"],
) Defensive patterns
Strategy: validation
Validate before calling
import os
def require_stability_key(api_key: str | None = None) -> str:
key = api_key or os.environ.get("STABILITY_API_KEY")
if not key:
raise RuntimeError(
"STABILITY_API_KEY missing — set it before generating images"
)
return key
key = require_stability_key()
resp = litellm.image_generation(model="stability/stable-image-core", prompt=p, api_key=key) Try / catch
try:
resp = litellm.image_generation(model="stability/...", prompt=p)
except ValueError as e:
if "STABILITY_API_KEY is not set" in str(e):
raise RuntimeError("deploy config incomplete: add STABILITY_API_KEY") from e
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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
- STABILITY_API_KEY is not set. Please set it via environment
- DASHSCOPE_API_KEY is not set
- Parameter {k} is not supported for model {model}. Supported
- TAVILY_API_KEY is not set. Set `TAVILY_API_KEY` environment
- TINYFISH_API_KEY is not set. Set `TINYFISH_API_KEY` environm
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/ca0455b73c23dc06.
Report an issue: GitHub.