fix(python): skip null text when reranking with Jina (#5296)

Fixes #5295.

Vector results can include a row with a valid vector and null text. The
Jina text reranker would send that null in `documents`, although the API
requires text documents. Filter the Arrow table before extracting
request documents so returned indices still select the correct rows and
retain their original scores. When every candidate lacks text, return a
typed empty result without contacting Jina.

Both regression cases failed on the upstream Python source and passed
with the change. They cover a null before two valid documents, reordered
response indices, preservation of row IDs, distance and schema, and an
all-null input with no request. The local test loaded the exact source
reranker modules on top of LanceDB 0.41.0 with PyArrow 26.0.1 and a
mocked HTTP client. Ruff lint, formatting and whitespace checks passed.
This test did not use a live Jina service. AI assistance was used to
investigate and draft the change.

Signed-off-by: Raunak <raunakpandey1007@gmail.com>
This commit is contained in:
Raunak Kumar Pandey
2026-11-30 18:16:11 +08:00
committed by GitHub
parent 4feffc649a
commit f1320b8ef9
2 changed files with 48 additions and 0 deletions
@@ -65,6 +65,9 @@ class JinaReranker(Reranker):
return self._session
def _rerank(self, result_set: pa.Table, query: str):
if len(result_set) > 0:
# Jina 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
@@ -0,0 +1,45 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
from unittest.mock import Mock
import pyarrow as pa
import pytest
from lancedb.rerankers import JinaReranker
@pytest.mark.parametrize("with_text", [True, False])
def test_jina_rerank_vector_with_nullable_text(with_text):
candidates = pa.table(
{
"_rowid": pa.array([10, 11, 12], type=pa.uint64()),
"text": pa.array(
[None, "first", "second"] if with_text else [None, None, None],
type=pa.string(),
),
"_distance": [0.1, 0.2, 0.3],
}
)
reranker = JinaReranker(return_score="all")
client = Mock()
client.post.return_value.json.return_value = {
"results": [
{"index": 1, "relevance_score": 0.9},
{"index": 0, "relevance_score": 0.4},
]
}
reranker._client = client
results = reranker.rerank_vector("query", candidates)
if with_text:
assert client.post.call_args.kwargs["json"]["documents"] == ["first", "second"]
assert results["_rowid"].to_pylist() == [12, 11]
assert results["_distance"].to_pylist() == [0.3, 0.2]
assert results["_relevance_score"].to_pylist() == pytest.approx([0.9, 0.4])
else:
client.post.assert_not_called()
assert len(results) == 0
assert results.select(candidates.column_names).schema == candidates.schema
assert results.schema.field("_relevance_score").type == pa.float32()