{"record":{"id":"5b364be34588b4d3","repo":"BerriAI/litellm","slug":"dashscope-api-key-is-required-set-dashscope-api-5b364b","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/rerank/transformation.py","lineNumber":92,"sourceCode":"            return cleaned\n\n        if cleaned.endswith(\"/v1\"):\n            return f\"{cleaned}/reranks\"\n\n        # Unknown base: append /reranks rather than silently ignoring the caller's api_base.\n        return f\"{cleaned}/reranks\"\n\n    def validate_environment(\n        self,\n        headers: dict,\n        model: str,\n        api_key: str | None = None,\n        optional_params: dict | 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\n        default_headers: Final = {\n            \"Authorization\": f\"Bearer {api_key}\",\n            \"accept\": \"application/json\",\n            \"content-type\": \"application/json\",\n        }\n        return {**default_headers, **headers}\n\n    def get_supported_cohere_rerank_params(self, model: str) -> list:\n        return [\"query\", \"documents\", \"top_n\", \"return_documents\"]\n\n    def map_cohere_rerank_params(\n        self,\n        non_default_params: dict | None,\n        model: str,\n        drop_params: bool,","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/dashscope/rerank/transformation.py#L74-L110","documentation":"Raised by the DashScope rerank transformer when no API key can be resolved: the api_key argument is None and the DASHSCOPE_API_KEY secret/env lookup also returns None. Thrown before any network call, as a ValueError from validate_environment.","triggerScenarios":"Calling litellm.rerank() with a DashScope rerank model (e.g. gte-rerank / qwen3-rerank) without an api_key parameter and without DASHSCOPE_API_KEY in the environment or secret manager.","commonSituations":"Rerank integration added to a RAG pipeline where only OPENAI_API_KEY was configured; env var set in the shell but not in the systemd/docker service unit; secret stored under a different key name in LiteLLM's secret manager.","solutions":["Export DASHSCOPE_API_KEY=sk-... in the process environment","Or pass api_key explicitly to the rerank call","If using LiteLLM proxy, add the key to the model's litellm_params in the config YAML","Confirm the env var is visible inside the actual runtime (docker exec / kubectl exec)"],"exampleFix":"# before\nresp = litellm.rerank(model=\"dashscope/gte-rerank\", query=\"q\", documents=docs)\n\n# after\nresp = litellm.rerank(model=\"dashscope/gte-rerank\", query=\"q\", documents=docs, api_key=os.environ[\"DASHSCOPE_API_KEY\"])","handlingStrategy":"validation","validationCode":"if not (api_key or os.getenv(\"DASHSCOPE_API_KEY\")):\n    raise RuntimeError(\"Set DASHSCOPE_API_KEY before calling DashScope rerank\")","typeGuard":null,"tryCatchPattern":"try:\n    resp = litellm.rerank(model=m, query=q, documents=docs)\nexcept ValueError as e:\n    if \"DASHSCOPE_API_KEY\" in str(e):\n        raise ConfigError(str(e)) from e\n    raise","preventionTips":["Centralize provider key checks in one preflight function called before any dashscope request","In LiteLLM proxy, always set the key in litellm_params so the env fallback is never exercised"],"tags":["dashscope","rerank","authentication","config"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}