BerriAI/litellm · error · ValueError

Missing API key for DashScope. Set DASHSCOPE_API_KEY environ

Error message

Missing API key for DashScope. Set DASHSCOPE_API_KEY environment variable or pass api_key parameter.

What it means

DashScope (Alibaba) embeddings require an API key. litellm checks the api_key kwarg, litellm.api_key, then the DASHSCOPE_API_KEY environment variable; when all are unset it raises this ValueError before any request.

Source

Thrown at litellm/main.py:6893

            response = base_llm_http_handler.embedding(
                model=model,
                input=input,
                timeout=timeout,
                custom_llm_provider=custom_llm_provider,
                logging_obj=logging,
                api_base=api_base,
                optional_params=optional_params,
                litellm_params={},
                model_response=EmbeddingResponse(),
                api_key=volcengine_key,
                client=client,
                aembedding=aembedding,
                headers=headers,
            )
        elif custom_llm_provider == "dashscope":
            dashscope_key: Final = api_key or litellm.api_key or get_secret_str("DASHSCOPE_API_KEY")
            if dashscope_key is None:
                raise ValueError(
                    "Missing API key for DashScope. Set DASHSCOPE_API_KEY environment variable or pass api_key parameter."
                )
            if extra_headers is not None and isinstance(extra_headers, dict):
                headers = extra_headers
            else:
                headers = {}
            response = base_llm_http_handler.embedding(
                model=model,
                input=input,
                timeout=timeout,
                custom_llm_provider=custom_llm_provider,
                logging_obj=logging,
                api_base=api_base,
                optional_params=optional_params,
                litellm_params={},
                model_response=EmbeddingResponse(),
                api_key=dashscope_key,
                client=client,

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. export DASHSCOPE_API_KEY=<key> and restart the process
  2. Or pass api_key=<key> to the embedding call
  3. Or set litellm.api_key at startup
  4. Confirm with python -c "import os; print(bool(os.environ.get('DASHSCOPE_API_KEY')))"

Example fix

# before
resp = litellm.embedding(model="dashscope/text-embedding-v3", input=["hi"])

# after
os.environ["DASHSCOPE_API_KEY"] = "sk-..."
resp = litellm.embedding(model="dashscope/text-embedding-v3", input=["hi"])
Defensive patterns

Strategy: validation

Validate before calling

import os

if not (os.environ.get("DASHSCOPE_API_KEY") or litellm.api_key):
    raise SystemExit("DASHSCOPE_API_KEY not set")

Try / catch

try:
    resp = litellm.embedding(model="dashscope/text-embedding-v3", input=["hi"])
except ValueError as e:
    if "DashScope" in str(e):
        raise RuntimeError("DashScope credentials missing") from e

Prevention

When it happens

Trigger: litellm.embedding(model='dashscope/<model>', input=[...]) with no api_key kwarg, no litellm.api_key, and DASHSCOPE_API_KEY absent from the environment.

Common situations: First-time DashScope integration; key exported in a shell but not in the service's unit file or container spec; .env loaded after litellm reads config.

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


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/4bd441c92613d816. Report an issue: GitHub.