mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
fix(python): skip null texts when reranking with VoyageAI (#5336)
## Summary A vector, full-text or hybrid search can return a row whose reranking text is null. `VoyageAIReranker` sent that null in its `documents` list, although the Voyage rerank API takes a list of strings, so one row with missing text broke reranking for every other row. Filter rows with null text out of the Arrow table before building the request, so the indices Voyage returns still select the right rows and keep their original columns. When every candidate has null text, return an empty scored table without calling the API. The class docstring now says that rows with a null `column` value are left out of the reranked results. This is the same change as #5283 (Cohere) and #5296 (Jina), applied to the VoyageAI reranker. [Voyage AI reranker API](https://docs.voyageai.com/reference/reranker-api) ## Verification - Added `test_voyageai_reranker_nullable_documents`, run through a real local table and `table.search(...).rerank(...)`, with two cases: - a null between two valid documents, where the mocked client returns reordered indices: only the two valid texts are sent, and the result keeps the correct row IDs, scores, `_distance` column and `float32` `_relevance_score`; - all-null input, where the client is never called and an empty typed result comes back. - Both cases fail on `main` and pass with this change. - Ran against LanceDB 0.42.1 with the changed reranker module copied over the installed one. The installed `voyageai.py` is identical to `main`. I did not call the live Voyage API. - Repository Ruff lint and format checks pass.
This commit is contained in:
@@ -17,6 +17,8 @@ class VoyageAIReranker(Reranker):
|
||||
Reranks the results using the VoyageAI Rerank API.
|
||||
https://docs.voyageai.com/docs/reranker
|
||||
|
||||
Rows with null values in ``column`` are omitted from the reranked results.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
model_name : str, default "rerank-english-v2.0"
|
||||
@@ -68,6 +70,9 @@ class VoyageAIReranker(Reranker):
|
||||
)
|
||||
|
||||
def _rerank(self, result_set: pa.Table, query: str):
|
||||
if len(result_set) > 0 and result_set[self.column].null_count:
|
||||
# Voyage result indices refer to the documents actually sent.
|
||||
result_set = result_set.filter(result_set[self.column].is_valid())
|
||||
result_set = self._handle_empty_results(result_set)
|
||||
if len(result_set) == 0:
|
||||
return result_set
|
||||
|
||||
@@ -1339,6 +1339,65 @@ def test_cohere_reranker_nullable_documents(tmp_path, mocker, texts, expected_id
|
||||
reranker._client.rerank.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("texts", "expected_ids"),
|
||||
[
|
||||
(["first document", None, "last document"], [2, 0]),
|
||||
([None, None, None], []),
|
||||
],
|
||||
)
|
||||
def test_voyageai_reranker_nullable_documents(tmp_path, mocker, texts, expected_ids):
|
||||
table = lancedb.connect(tmp_path).create_table(
|
||||
"documents",
|
||||
pa.table(
|
||||
{
|
||||
"id": [0, 1, 2],
|
||||
"text": pa.array(texts, type=pa.string()),
|
||||
"vector": pa.array(
|
||||
[[0.0, 0.0], [0.5, 0.0], [1.0, 0.0]],
|
||||
type=pa.list_(pa.float32(), 2),
|
||||
),
|
||||
}
|
||||
),
|
||||
)
|
||||
reranker = VoyageAIReranker(
|
||||
model_name="rerank-2.5", top_n=2, return_score="all", api_key="test"
|
||||
)
|
||||
reranker._client = mocker.Mock()
|
||||
|
||||
def rerank(*, documents, **kwargs):
|
||||
assert documents == ["first document", "last document"]
|
||||
return mocker.Mock(
|
||||
results=[
|
||||
mocker.Mock(index=1, relevance_score=0.9),
|
||||
mocker.Mock(index=0, relevance_score=0.7),
|
||||
]
|
||||
)
|
||||
|
||||
reranker._client.rerank.side_effect = rerank
|
||||
result = (
|
||||
table.search([0.0, 0.0])
|
||||
.limit(3)
|
||||
.rerank(reranker, query_string="Which document is last?")
|
||||
.to_arrow()
|
||||
)
|
||||
|
||||
assert result["id"].to_pylist() == expected_ids
|
||||
assert result.schema.field("_relevance_score").type == pa.float32()
|
||||
assert "_distance" in result.column_names
|
||||
if expected_ids:
|
||||
assert result["_relevance_score"].to_pylist() == pytest.approx([0.9, 0.7])
|
||||
reranker._client.rerank.assert_called_once_with(
|
||||
query="Which document is last?",
|
||||
documents=["first document", "last document"],
|
||||
top_k=2,
|
||||
model="rerank-2.5",
|
||||
truncation=reranker.truncation,
|
||||
)
|
||||
else:
|
||||
reranker._client.rerank.assert_not_called()
|
||||
|
||||
|
||||
def test_cross_encoder_reranker_return_all(tmp_path):
|
||||
pytest.importorskip("sentence_transformers")
|
||||
reranker = CrossEncoderReranker(return_score="all")
|
||||
|
||||
Reference in New Issue
Block a user