{"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/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/dashscope/rerank/transformation.py#L123-L159","documentation":"The DashScope rerank request transformer requires a 'query' field; if the optional rerank params dict passed to transform_rerank_request lacks 'query', a ValueError is raised before any HTTP call. This typically means the caller invoked the rerank API without a query argument.","triggerScenarios":"Calling litellm.rerank(model=\"dashscope/...\", documents=[...]) with no query= argument, or constructing a rerank request via the lower-level transformer without 'query' in the params dict.","commonSituations":"Adapting code from a Cohere-style SDK call where query was optional or named differently; building the request dict dynamically and the query key is dropped when the search box is empty.","solutions":["Pass a non-empty query: litellm.rerank(model=..., query=\"search text\", documents=[...])","If query is empty in your app, guard upstream and skip/short-circuit the rerank call","Check any dynamic dict construction that builds rerank params to ensure 'query' survives"],"exampleFix":"# before\nresp = litellm.rerank(model=\"dashscope/gte-rerank\", documents=chunks)\n\n# after\nresp = litellm.rerank(model=\"dashscope/gte-rerank\", query=user_question, documents=chunks)","handlingStrategy":"validation","validationCode":"if not query or not str(query).strip():\n    raise ValueError(\"rerank requires a non-empty query\")","typeGuard":"def is_valid_rerank_request(params: dict) -> bool:\n    return isinstance(params.get(\"query\"), str) and bool(params[\"query\"].strip()) and isinstance(params.get(\"documents\"), list)","tryCatchPattern":"try:\n    resp = litellm.rerank(model=m, query=q, documents=docs)\nexcept ValueError as e:\n    if \"query is required\" in str(e):\n        return docs  # graceful degrade: skip reranking\n    raise","preventionTips":["Type your rerank entry point to require query: str and documents: list[str] parameters","Skip the rerank stage when the upstream search query is empty rather than forwarding it"],"tags":["dashscope","rerank","validation","request"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}