{"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/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/dashscope/rerank/transformation.py#L74-L110","documentation":"The DashScope rerank transformer's validate_environment refuses to build request headers without credentials: it first checks the api_key argument, then falls back to get_secret_str('DASHSCOPE_API_KEY'); if both are None it raises this ValueError. No HTTP request is made — the failure is entirely local configuration.","triggerScenarios":"litellm.rerank(model='dashscope/gte-rerank-...', query=..., documents=[...]) with no api_key argument and DASHSCOPE_API_KEY absent from the environment (fresh shell, CI job, container, or proxy deployment missing the key).","commonSituations":"Key configured for chat models but rerank call path runs in a separate worker without env; dotenv loaded after import in some code paths; key present under a different name (e.g. DASHSCOPE_TOKEN).","solutions":["export DASHSCOPE_API_KEY=sk-... in the environment that actually runs the rerank call","Or pass api_key explicitly: litellm.rerank(..., api_key='sk-...')","In the LiteLLM proxy, add DASHSCOPE_API_KEY to the dashscope rerank deployment config","Fail fast at startup by asserting the env var exists before serving traffic"],"exampleFix":"# before\nresult = litellm.rerank(model='dashscope/gte-rerank', query='q', documents=['a', 'b'])  # ValueError\n\n# after\nresult = litellm.rerank(\n    model='dashscope/gte-rerank',\n    query='q',\n    documents=['a', 'b'],\n    api_key=os.environ['DASHSCOPE_API_KEY'],\n)","handlingStrategy":"validation","validationCode":"import os\n\nif not os.getenv('DASHSCOPE_API_KEY'):\n    raise RuntimeError('Set DASHSCOPE_API_KEY before enabling the dashscope reranker')","typeGuard":null,"tryCatchPattern":"try:\n    result = litellm.rerank(model='dashscope/gte-rerank', query=q, documents=docs)\nexcept ValueError as e:\n    if 'DASHSCOPE_API_KEY' in str(e):\n        raise ConfigError('DashScope key not configured') from e\n    raise","preventionTips":["Assert provider env vars at startup in every worker that can issue rerank calls","Centralize credential loading in one module so rerank/chat/embedding paths share the same check"],"tags":["dashscope","rerank","api-key","environment-variables"],"backgroundTag":"missing-api-key","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}