{"record":{"id":"2baf65b699ac7eab","repo":"BerriAI/litellm","slug":"query-is-required-for-dashscope-rerank","errorCode":null,"errorMessage":"query is required for DashScope rerank","messagePattern":"query is required for DashScope rerank","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/dashscope/rerank/transformation.py","lineNumber":141,"sourceCode":"        params: Final[OptionalRerankParams] = OptionalRerankParams(\n            query=query,\n            documents=documents,\n        )\n        if top_n is not None:\n            params[\"top_n\"] = top_n\n        if return_documents is not None:\n            params[\"return_documents\"] = return_documents\n        return dict(params)\n\n    def transform_rerank_request(\n        self,\n        model: str,\n        optional_rerank_params: dict,\n        headers: dict,\n        litellm_params: dict | None = None,\n    ) -> dict:\n        if \"query\" not in optional_rerank_params:\n            raise ValueError(\"query is required for DashScope rerank\")\n        if \"documents\" not in optional_rerank_params:\n            raise ValueError(\"documents is required for DashScope rerank\")\n\n        request: Final[dict[str, Any]] = {\n            \"model\": model,\n            \"query\": optional_rerank_params[\"query\"],\n            \"documents\": optional_rerank_params[\"documents\"],\n        }\n        if optional_rerank_params.get(\"top_n\") is not None:\n            request[\"top_n\"] = optional_rerank_params[\"top_n\"]\n        if optional_rerank_params.get(\"return_documents\") is not None:\n            request[\"return_documents\"] = optional_rerank_params[\"return_documents\"]\n        return request\n\n    def transform_rerank_response(\n        self,\n        model: str,\n        raw_response: httpx.Response,","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/dashscope/rerank/transformation.py#L123-L159","documentation":"transform_rerank_request builds the upstream DashScope /reranks body directly from the rerank parameters and hard-requires 'query'. If the params dict passed to it has no 'query' key it raises ValueError('query is required for DashScope rerank') before any network I/O — the request was structurally incomplete.","triggerScenarios":"litellm.rerank(model='dashscope/...', documents=[...]) with query omitted or dropped (None); dynamically-built params where the query field is missing; ported Cohere-style code where the query key got lost; passing query under a different key name ('q', 'question').","commonSituations":"Search/RAG pipelines assembling rerank calls from user input where the query can be empty; wrapper functions with optional query that forward **kwargs directly; schema drift between your internal API and litellm.rerank's parameters.","solutions":["Pass the query explicitly: litellm.rerank(model=..., query='...', documents=[...])","If building calls dynamically, assert 'query' exists and is a non-empty string before invoking rerank","Check for key-name mismatches ('q', 'question', 'search') in whatever dict/kwargs you forward"],"exampleFix":"# before\nlitellm.rerank(model='dashscope/gte-rerank', documents=['a', 'b'])  # ValueError: query is required\n\n# after\nlitellm.rerank(\n    model='dashscope/gte-rerank',\n    query='which item is the letter a?',\n    documents=['a', 'b'],\n)","handlingStrategy":"validation","validationCode":"def validate_rerank_call(query, documents) -> None:\n    if not isinstance(query, str) or not query.strip():\n        raise ValueError('rerank requires a non-empty query string')\n    if not isinstance(documents, list) or not documents:\n        raise ValueError('rerank requires a non-empty documents list')","typeGuard":"def has_valid_query(params: dict) -> bool:\n    q = params.get('query')\n    return isinstance(q, str) and bool(q.strip())","tryCatchPattern":"try:\n    litellm.rerank(model='dashscope/gte-rerank', **params)\nexcept ValueError as e:\n    if 'query is required' in str(e):\n        return skip_with_warning('rerank skipped: no query')\n    raise","preventionTips":["Validate query/documents at your API boundary before building provider calls","Short-circuit rerank when the query is empty instead of forwarding the call"],"tags":["dashscope","rerank","required-parameter","validation"],"backgroundTag":"missing-required-argument","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}