BerriAI/litellm · error · ValueError
DASHSCOPE_API_KEY is not set
Error message
DASHSCOPE_API_KEY is not set
What it means
The DashScope image-generation handler's validate_environment needs a bearer token before any HTTP request: either an explicit api_key argument or the DASHSCOPE_API_KEY environment variable. Neither was present, so it raises ValueError('DASHSCOPE_API_KEY is not set') — a pure client-side configuration failure, no network call happened.
Solutions
- Export the variable in the runtime environment: export DASHSCOPE_API_KEY=sk-... (or inject it via your container/CI secret store)
- Or pass it per call: litellm.image_generation(..., api_key='sk-...')
- If using the LiteLLM proxy, set DASHSCOPE_API_KEY in the dashscope deployment's litellm_params/environment_variables in litellm_config.yaml
- Verify visibility in the failing process: python -c "import os; print(bool(os.getenv('DASHSCOPE_API_KEY')))"
Example fix
# before
img = litellm.image_generation(model='dashscope/wanx2.1-t2i-turbo', prompt='a cat') # ValueError
# after
img = litellm.image_generation(
model='dashscope/wanx2.1-t2i-turbo',
prompt='a cat',
api_key=os.environ['DASHSCOPE_API_KEY'], # or export DASHSCOPE_API_KEY first
) Defensive patterns
Strategy: validation
Validate before calling
import os
def dashscope_ready() -> bool:
return bool(os.getenv('DASHSCOPE_API_KEY'))
if not dashscope_ready():
raise RuntimeError('DASHSCOPE_API_KEY missing - refusing to start image-generation worker') Try / catch
try:
litellm.image_generation(model='dashscope/wanx2.1-t2i-turbo', prompt=p)
except ValueError as e:
if 'DASHSCOPE_API_KEY' in str(e):
raise ConfigError('DashScope key not provisioned for this environment') from e
raise Prevention
- Load .env at process start, before any litellm import-time config
- Run a boot-time assert on required provider env vars and fail fast
- Store keys in a secret manager and inject them into the runtime environment in every environment
When it happens
Trigger: litellm.image_generation(model='dashscope/<image-model>', prompt=...) executed in a process where DASHSCOPE_API_KEY was never exported and no api_key was passed (or supplied by the LiteLLM proxy config).
Common situations: .env file not loaded (python-dotenv not run at startup); key exported in a different shell/venv than the running app; Docker/K8s secret not mounted; proxy config lists the deployment under a provider whose env var name is misspelled.
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
- DashScope API key is required. Set 'DASHSCOPE_API_KEY' env…
- STABILITY_API_KEY is not set. Please set it via environment…
- API key is required for Topaz image variations. Set via…
- BFL_API_KEY is not set. Please set it via environment…
- CrowdStrike AIDR API Key not found. Set CS_AIDR_TOKEN…
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/c9dd7207e3546400.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/llms/dashscope/image_generation/transformation.py:112
optional_params: dict,
litellm_params: dict,
stream: bool | None = None,
) -> str:
return api_base or get_secret_str("DASHSCOPE_API_BASE_IMAGE") or DEFAULT_API_BASE
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:
final_api_key: Final = api_key or get_secret_str("DASHSCOPE_API_KEY")
if not final_api_key:
raise ValueError("DASHSCOPE_API_KEY is not set")
headers["Authorization"] = f"Bearer {final_api_key}"
headers["Content-Type"] = "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 image generation request to DashScope multimodal-generation format.
"""
parameters: Final[dict] = {}
for k, v in optional_params.items():
parameters[k] = vView on GitHub (pinned to 77b7c6c40c)