From 69148759b9e8a327169a63b18d52d7036589af08 Mon Sep 17 00:00:00 2001 From: sovopr Date: Tue, 13 Oct 2026 00:43:47 +0530 Subject: [PATCH] fix(huggingface): honor server rerank truncation defaults --- .../llms/huggingface/rerank/transformation.py | 1 - .../test_huggingface_rerank_transformation.py | 26 ++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/litellm/llms/huggingface/rerank/transformation.py b/litellm/llms/huggingface/rerank/transformation.py index 334c60ee848..1ae4fc52d25 100644 --- a/litellm/llms/huggingface/rerank/transformation.py +++ b/litellm/llms/huggingface/rerank/transformation.py @@ -158,7 +158,6 @@ class HuggingFaceRerankConfig(BaseRerankConfig): # HuggingFace API expects return_text parameter, corresponding to our return_documents parameter request_body: Final = { "raw_scores": False, - "truncate": False, "truncation_direction": "Right", } diff --git a/tests/unit/llms/huggingface/rerank/test_huggingface_rerank_transformation.py b/tests/unit/llms/huggingface/rerank/test_huggingface_rerank_transformation.py index 6fd2b006fef..497ee091065 100644 --- a/tests/unit/llms/huggingface/rerank/test_huggingface_rerank_transformation.py +++ b/tests/unit/llms/huggingface/rerank/test_huggingface_rerank_transformation.py @@ -5,6 +5,7 @@ Based on the test patterns from other rerank providers and the current HuggingFa import asyncio import json +from typing import Final from unittest.mock import AsyncMock, MagicMock, patch import pytest @@ -275,10 +276,29 @@ def test_request_transformation(): assert request_body["top_n"] == 2 assert request_body["return_text"] is True assert request_body["raw_scores"] is False - assert request_body["truncate"] is False + assert "truncate" not in request_body assert request_body["truncation_direction"] == "Right" +@pytest.mark.parametrize("truncate", [True, False, None]) +def test_request_transformation_preserves_explicit_truncation(truncate: bool | None) -> None: + from litellm.llms.huggingface.rerank.transformation import HuggingFaceRerankConfig + + request_body: Final = HuggingFaceRerankConfig().transform_rerank_request( + model="test", + optional_rerank_params={"query": "hello", "texts": ["world"], "truncate": truncate}, + headers={}, + ) + + assert request_body == { + "query": "hello", + "texts": ["world"], + "raw_scores": False, + "truncate": truncate, + "truncation_direction": "Right", + } + + def test_response_transformation(): """Test response transformation logic.""" from litellm.llms.huggingface.rerank.transformation import HuggingFaceRerankConfig @@ -385,13 +405,11 @@ def test_huggingface_rerank_request_payload(mock_post): "texts": ["hello", "world"], "raw_scores": False, "return_text": True, - "truncate": False, "truncation_direction": "Right", "top_n": 2, } - for key, value in expected_request.items(): - assert request_data[key] == value + assert request_data == expected_request assert response.results is not None assert len(response.results) == 2