Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(hosted_vllm): reject invalid rerank truncation params with a 400

mateo-berrief14bed0296b
diff --git a/litellm/llms/hosted_vllm/rerank/transformation.py b/litellm/llms/hosted_vllm/rerank/transformation.py
index 265eb350fc6..764d80c6f82 100644
--- a/litellm/llms/hosted_vllm/rerank/transformation.py
+++ b/litellm/llms/hosted_vllm/rerank/transformation.py
@@ -7,8 +7,10 @@ from types import MappingProxyType
from typing import Any, Final
import httpx
+from pydantic import ValidationError
from litellm._uuid import uuid
+from litellm.exceptions import UnsupportedParamsError
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
from litellm.llms.base_llm.chat.transformation import BaseLLMException
from litellm.llms.base_llm.rerank.transformation import BaseRerankConfig
@@ -36,6 +38,13 @@ class HostedVLLMRerankError(BaseLLMException):
super().__init__(status_code=status_code, message=message, headers=headers)
+def validated_truncation_params(non_default_params: Mapping[str, object] | None) -> HostedVLLMRerankTruncationParams:
+ try:
+ return HostedVLLMRerankTruncationParams.model_validate(non_default_params or MappingProxyType({}))
+ except ValidationError as error:
+ raise UnsupportedParamsError(status_code=400, message=f"hosted_vllm rerank: {error}") from error
+
+
class HostedVLLMRerankConfig(BaseRerankConfig):
def __init__(self) -> None:
pass
@@ -106,7 +115,7 @@ class HostedVLLMRerankConfig(BaseRerankConfig):
if instruction is not None:
mapped_params["instruction"] = instruction
- truncation: Final = HostedVLLMRerankTruncationParams.model_validate(non_default_params or MappingProxyType({}))
+ truncation: Final = validated_truncation_params(non_default_params)
forwarded: Final[OptionalRerankParams] = {
**mapped_params,
"max_tokens_per_doc": max_tokens_per_doc,
diff --git a/tests/test_litellm/llms/hosted_vllm/test_hosted_vllm_rerank_transformation.py b/tests/test_litellm/llms/hosted_vllm/test_hosted_vllm_rerank_transformation.py
index 49d58cc28c4..9a62fcf6f0f 100644
--- a/tests/test_litellm/llms/hosted_vllm/test_hosted_vllm_rerank_transformation.py
+++ b/tests/test_litellm/llms/hosted_vllm/test_hosted_vllm_rerank_transformation.py
@@ -202,6 +202,22 @@ class TestHostedVLLMRerankTruncationParams:
assert params["max_tokens_per_doc"] == 128
assert "metadata" not in params
+ @pytest.mark.parametrize(
+ "bad_params",
+ [{"truncation_side": "middle"}, {"truncate_prompt_tokens": "lots"}, {"max_tokens_per_query": -1.5}],
+ )
+ def test_map_cohere_rerank_params_rejects_invalid_truncation_params_as_400(self, bad_params: dict[str, object]):
+ with pytest.raises(litellm.UnsupportedParamsError) as raised:
+ self.config.map_cohere_rerank_params(
+ non_default_params=dict(bad_params),
+ model=self.model,
+ drop_params=False,
+ query="test query",
+ documents=["doc1", "doc2"],
+ )
+ assert raised.value.status_code == 400
+ assert next(iter(bad_params)) in str(raised.value)
+
def test_map_cohere_rerank_params_omits_truncation_params_when_absent(self):
params: Final = self.config.map_cohere_rerank_params(
non_default_params={"metadata": {"user_api_key": "sk-test"}},
@@ -225,16 +241,6 @@ class TestHostedVLLMRerankTruncationParams:
"return_documents": True,
}
- def test_map_cohere_rerank_params_rejects_invalid_truncation_side(self):
- with pytest.raises(ValueError, match="truncation_side"):
- self.config.map_cohere_rerank_params(
- non_default_params={"truncation_side": "middle"},
- model=self.model,
- drop_params=False,
- query="test query",
- documents=["doc1", "doc2"],
- )
-
def test_transform_request_forwards_truncation_params(self):
body: Final = self.config.transform_rerank_request(
model=self.model,