From f1320b8ef94a6684b0055089a1729a6d905b9a24 Mon Sep 17 00:00:00 2001 From: Raunak Kumar Pandey <65064180+raunakpandey1@users.noreply.github.com> Date: Mon, 30 Nov 2026 05:16:11 -0500 Subject: [PATCH] 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 --- python/python/lancedb/rerankers/jinaai.py | 3 ++ .../python/tests/test_jina_nullable_text.py | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 python/python/tests/test_jina_nullable_text.py diff --git a/python/python/lancedb/rerankers/jinaai.py b/python/python/lancedb/rerankers/jinaai.py index 349a8eca5..065862eea 100644 --- a/python/python/lancedb/rerankers/jinaai.py +++ b/python/python/lancedb/rerankers/jinaai.py @@ -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 diff --git a/python/python/tests/test_jina_nullable_text.py b/python/python/tests/test_jina_nullable_text.py new file mode 100644 index 000000000..9aadc78ee --- /dev/null +++ b/python/python/tests/test_jina_nullable_text.py @@ -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()