From 8b675e0d1c06b2273d5188eeef7ef9856370f794 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 22 Nov 2026 02:04:07 -0400 Subject: [PATCH 1/2] fix(rerank): honor rank_by in VoyageAIReranker rank_by was accepted but never read, so dict docs without a 'content' key were stringified wholesale. Build the rerank text from the rank_by fields (per-call override supported) and raise if a field is missing. Co-Authored-By: Claude Opus 5.5 --- redisvl/utils/rerank/voyageai.py | 18 +++++--- tests/unit/test_voyageai_reranker_rank_by.py | 47 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 tests/unit/test_voyageai_reranker_rank_by.py diff --git a/redisvl/utils/rerank/voyageai.py b/redisvl/utils/rerank/voyageai.py index 2de35c34..962ea25a 100644 --- a/redisvl/utils/rerank/voyageai.py +++ b/redisvl/utils/rerank/voyageai.py @@ -108,12 +108,18 @@ def _preprocess(self, query: str, docs: list[dict[str, Any]] | list[str], **kwar if not isinstance(docs, list): raise ValueError("Docs to rerank should either be a list of str or dict.") - if all(isinstance(doc, dict) and "content" in doc for doc in docs): - texts = [ - str(doc["content"]) - for doc in docs - if isinstance(doc, dict) and "content" in doc - ] + rank_by = kwargs.get("rank_by", self.rank_by) or [] + rank_by = [rank_by] if isinstance(rank_by, str) else rank_by + + if rank_by and all(isinstance(doc, dict) for doc in docs): + texts = [] + for doc in docs: + missing = [key for key in rank_by if key not in doc] + if missing: + raise ValueError(f"Document is missing rank_by field(s): {missing}") + texts.append(" ".join(str(doc[key]) for key in rank_by)) + elif all(isinstance(doc, dict) and "content" in doc for doc in docs): + texts = [str(doc["content"]) for doc in docs] else: texts = [str(doc) for doc in docs] diff --git a/tests/unit/test_voyageai_reranker_rank_by.py b/tests/unit/test_voyageai_reranker_rank_by.py new file mode 100644 index 00000000..62eebca4 --- /dev/null +++ b/tests/unit/test_voyageai_reranker_rank_by.py @@ -0,0 +1,47 @@ +import sys +import types +from unittest.mock import MagicMock + +import pytest + + +@pytest.fixture +def reranker(monkeypatch): + fake = types.ModuleType("voyageai") + fake.Client = MagicMock() + fake.AsyncClient = MagicMock() + monkeypatch.setitem(sys.modules, "voyageai", fake) + from redisvl.utils.rerank.voyageai import VoyageAIReranker + + return VoyageAIReranker(model="rerank-2", api_config={"api_key": "k"}, rank_by=["text"]) + + +DOCS = [{"text": "alpha", "id": 1}, {"text": "beta", "id": 2}] + + +def test_rank_by_selects_field(reranker): + kwargs, _ = reranker._preprocess("q", DOCS) + assert kwargs["documents"] == ["alpha", "beta"] + + +def test_rank_by_multiple_fields_and_override(reranker): + docs = [{"text": "alpha", "title": "T1"}, {"text": "beta", "title": "T2"}] + kwargs, _ = reranker._preprocess("q", docs, rank_by=["title", "text"]) + assert kwargs["documents"] == ["T1 alpha", "T2 beta"] + + +def test_rank_by_missing_field_raises(reranker): + with pytest.raises(ValueError, match="rank_by"): + reranker._preprocess("q", [{"other": "x"}]) + + +def test_content_key_still_default(monkeypatch): + fake = types.ModuleType("voyageai") + fake.Client = MagicMock() + fake.AsyncClient = MagicMock() + monkeypatch.setitem(sys.modules, "voyageai", fake) + from redisvl.utils.rerank.voyageai import VoyageAIReranker + + r = VoyageAIReranker(model="rerank-2", api_config={"api_key": "k"}) + kwargs, _ = r._preprocess("q", [{"content": "a"}, {"content": "b"}]) + assert kwargs["documents"] == ["a", "b"] From 8d81adc4cc2264f0413417f4b1547a7300eba81d Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 23 Nov 2026 17:56:41 -0400 Subject: [PATCH 2/2] fix(rerank): restore per-item isinstance(dict) guard for mypy --- redisvl/utils/rerank/voyageai.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/redisvl/utils/rerank/voyageai.py b/redisvl/utils/rerank/voyageai.py index 962ea25a..c22e9463 100644 --- a/redisvl/utils/rerank/voyageai.py +++ b/redisvl/utils/rerank/voyageai.py @@ -119,7 +119,10 @@ def _preprocess(self, query: str, docs: list[dict[str, Any]] | list[str], **kwar raise ValueError(f"Document is missing rank_by field(s): {missing}") texts.append(" ".join(str(doc[key]) for key in rank_by)) elif all(isinstance(doc, dict) and "content" in doc for doc in docs): - texts = [str(doc["content"]) for doc in docs] + texts = [ + str(doc["content"]) if isinstance(doc, dict) else str(doc) + for doc in docs + ] else: texts = [str(doc) for doc in docs]