BerriAI/litellm · error · ValueError
DASHSCOPE_API_KEY is not set
Error message
DASHSCOPE_API_KEY is not set
What it means
Raised during request preparation for DashScope image generation: neither an explicit api_key parameter nor the DASHSCOPE_API_KEY environment variable is available, so no Authorization header can be built. It is a plain ValueError thrown from validate_environment before any HTTP request is made.
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 6c2dcb801b)
Solutions
- Set DASHSCOPE_API_KEY in the environment: export DASHSCOPE_API_KEY=sk-...
- Or pass the key explicitly: litellm.image_generation(..., api_key=key)
- In server deployments, configure it as a provider environment variable in the LiteLLM proxy env settings
- Verify with: python -c "import os; print(bool(os.getenv('DASHSCOPE_API_KEY')))"
Example fix
# before img = litellm.image_generation(model="dashscope/wanx-v1", prompt="a cat") # after img = litellm.image_generation(model="dashscope/wanx-v1", prompt="a cat", api_key=os.environ["DASHSCOPE_API_KEY"])
Defensive patterns
Strategy: validation
Validate before calling
api_key = os.getenv("DASHSCOPE_API_KEY")
if not api_key:
raise RuntimeError("DASHSCOPE_API_KEY missing — set it before image generation") Type guard
def has_dashscope_key(params: dict) -> bool:
return bool(params.get("api_key") or os.getenv("DASHSCOPE_API_KEY")) Try / catch
try:
img = litellm.image_generation(model=m, prompt=p, api_key=os.getenv("DASHSCOPE_API_KEY"))
except ValueError as e:
if "DASHSCOPE_API_KEY" in str(e):
raise ConfigError("Configure DASHSCOPE_API_KEY") from e
raise Prevention
- Add a startup config check that asserts DASHSCOPE_API_KEY is present when dashscope models are configured
- Pass api_key explicitly from your secret manager instead of relying on ambient env
When it happens
Trigger: Calling litellm.image_generation() with a dashscope model without passing api_key and without DASHSCOPE_API_KEY set in the environment (or set to an empty string).
Common situations: Deploying to a new environment (container, CI) where the env var was not carried over; typos in the variable name (e.g. DASHSCOPE_KEY); expecting LiteLLM's generic API_KEY convention instead of the provider-specific name.
Related errors
- DashScope API key is required. Set 'DASHSCOPE_API_KEY' env v
- 'username' is required in litellm_params when auth_mode='cp4
- Missing Azure Document Intelligence API Key - Set AZURE_DOCU
- Missing Azure AI API Key - A call is being made to Azure AI
- Error transforming image generation config: {e}. Got params:
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/c9dd7207e3546400.
Report an issue: GitHub.