diff --git a/litellm/llms/infinity/rerank/transformation.py b/litellm/llms/infinity/rerank/transformation.py index e089b3fecbe..7bd2c453072 100644 --- a/litellm/llms/infinity/rerank/transformation.py +++ b/litellm/llms/infinity/rerank/transformation.py @@ -31,12 +31,16 @@ class _InfinityRerankUsage(TypedDict, extra_items=ReadOnly[int]): """The token counters Infinity reports in the ``usage`` block of a rerank response.""" +class _InfinityRerankDocument(TypedDict): + text: ReadOnly[str] + + class _InfinityRerankResult(TypedDict): """One scored document in an Infinity ``/v1/rerank`` response.""" index: ReadOnly[int] relevance_score: ReadOnly[float] - document: ReadOnly[str] + document: ReadOnly[str | _InfinityRerankDocument] class _InfinityRerankResponse(TypedDict): @@ -104,8 +108,6 @@ class InfinityRerankConfig(CohereRerankConfig): ) -> RerankResponse: """ Transform Infinity rerank response - - No transformation required, Infinity follows Cohere API response format """ try: raw_response_json: Final = _parse_rerank_response(raw_response) @@ -129,8 +131,10 @@ class InfinityRerankConfig(CohereRerankConfig): index=result.get("index"), relevance_score=result.get("relevance_score"), ) - if result.get("document"): - _rerank_response["document"] = RerankResponseDocument(text=result.get("document")) + if document := result.get("document"): + _rerank_response["document"] = RerankResponseDocument( + text=document.get("text") if isinstance(document, Mapping) else document + ) cohere_results.append(_rerank_response) if cohere_results is None: raise ValueError(f"No results found in the response={raw_response_json}") diff --git a/tests/unit/llms/infinity/test_infinity.py b/tests/unit/llms/infinity/test_infinity.py index 2a544938a7a..3955964d5a1 100644 --- a/tests/unit/llms/infinity/test_infinity.py +++ b/tests/unit/llms/infinity/test_infinity.py @@ -69,8 +69,9 @@ async def test_infinity_rerank(): assert_response_shape(response, custom_llm_provider="infinity") +@pytest.mark.parametrize("provider_document", ["hello", {"text": "hello"}]) @pytest.mark.asyncio() -async def test_infinity_rerank_with_return_documents(): +async def test_infinity_rerank_with_return_documents(provider_document: str | dict[str, str]): mock_response = AsyncMock() mock_response = AsyncMock() @@ -78,7 +79,7 @@ async def test_infinity_rerank_with_return_documents(): def return_val(): return { "id": "cmpl-mockid", - "results": [{"index": 0, "relevance_score": 0.95, "document": "hello"}], + "results": [{"index": 0, "relevance_score": 0.95, "document": provider_document}], "usage": {"prompt_tokens": 100, "total_tokens": 150}, }