{"record":{"id":"8ecc092985f993ce","repo":"BerriAI/litellm","slug":"dashscope-api-key-is-required-set-dashscope-api","errorCode":null,"errorMessage":"DashScope API key is required. Set 'DASHSCOPE_API_KEY' env var or pass api_key explicitly.","messagePattern":"DashScope API key is required\\. Set 'DASHSCOPE_API_KEY' env var or pass api_key explicitly\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/dashscope/embed/transformation.py","lineNumber":78,"sourceCode":"            # unsupported params are dropped when drop_params=True;\n            # the upstream _check_valid_arg already raised UnsupportedParamsError\n            # for drop_params=False before this method is called.\n        return optional_params\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        if api_key is None:\n            api_key = get_secret_str(\"DASHSCOPE_API_KEY\")\n        if api_key is None:\n            raise ValueError(\n                \"DashScope API key is required. Set 'DASHSCOPE_API_KEY' env var or pass api_key explicitly.\"\n            )\n        default_headers: Final = {\n            \"Content-Type\": \"application/json\",\n            \"Authorization\": f\"Bearer {api_key}\",\n        }\n        return {**default_headers, **headers}\n\n    def get_complete_url(\n        self,\n        api_base: str | None,\n        api_key: str | None,\n        model: str,\n        optional_params: dict,\n        litellm_params: dict,\n        stream: bool | None = None,\n    ) -> str:\n        base = api_base or get_secret_str(\"DASHSCOPE_API_BASE\") or DEFAULT_API_BASE","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/dashscope/embed/transformation.py#L60-L96","documentation":"DashScope embedding transformation validate_environment requires an API key: if api_key is None it falls back to the DASHSCOPE_API_KEY environment variable via get_secret_str, and if that is also absent it raises ValueError telling you to set the env var or pass the key explicitly. This fails before any HTTP request is made.","triggerScenarios":"Calling litellm.embedding with a dashscope model (e.g. text-embedding-v3 via DashScope) without api_key in the call/config and without DASHSCOPE_API_KEY exported in the environment (including the process env seen by litellm's secret resolver).","commonSituations":"Deployments where the env var is set in a shell but not in the service (systemd/docker/serverless); .env file not loaded; typo DASHSCOPE_APIkEY; key stored under a different name (e.g. OPENAI_API_KEY) while using DashScope models; proxy setups that strip env vars.","solutions":["Export DASHSCOPE_API_KEY in the environment the process runs in: export DASHSCOPE_API_KEY=sk-...","Or pass the key explicitly: litellm.embedding(model=\"dashscope/...\", ..., api_key=sk) / set api_key in the model's litellm_params on the proxy","If using a .env file, ensure it is loaded (litellm reads .env via dotenv only if configured) or set the var in the container/service unit","Verify with: python -c \"import os; print(bool(os.environ.get('DASHSCOPE_API_KEY')))\""],"exampleFix":"# before\nlitellm.embedding(model=\"dashscope/text-embedding-v3\", input=[\"hi\"])\n# ValueError: DashScope API key is required...\n\n# after\nlitellm.embedding(model=\"dashscope/text-embedding-v3\", input=[\"hi\"], api_key=os.environ[\"DASHSCOPE_API_KEY\"])","handlingStrategy":"validation","validationCode":"import os, litellm\nkey = os.environ.get(\"DASHSCOPE_API_KEY\")\nif not key:\n    raise RuntimeError(\"Set DASHSCOPE_API_KEY before calling dashscope embeddings\")\nlitellm.embedding(model=\"dashscope/text-embedding-v3\", input=[\"hi\"], api_key=key)","typeGuard":"def has_dashscope_credentials(api_key: str | None) -> bool:\n    import os\n    return bool(api_key or os.environ.get(\"DASHSCOPE_API_KEY\"))","tryCatchPattern":"try:\n    litellm.embedding(model=\"dashscope/text-embedding-v3\", input=texts)\nexcept ValueError as e:\n    if \"DASHSCOPE_API_KEY\" in str(e):\n        raise ConfigError(\"missing dashscope credentials\") from e\n    raise","preventionTips":["Set DASHSCOPE_API_KEY in the service's environment (docker-compose env, systemd Environment=, serverless config) — not just your shell","Pass api_key explicitly in multi-tenant proxy configs so environments without the var still work","Add a startup credential check for every provider you call"],"tags":["auth","api-key","dashscope","embeddings","environment","litellm"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}