From c3f005f7ffda207523ce8a9c109d066d07bffda4 Mon Sep 17 00:00:00 2001 From: Vibhanshu Singh <2k24.cs1q.2413927@gmail.com> Date: Wed, 2 Dec 2026 03:37:51 +0530 Subject: [PATCH] fix(python): skip null texts when reranking with watsonx.ai (#5337) ## Summary A vector, full-text or hybrid search can return a row whose reranking text is null. `WatsonxReranker` sent that null in the `inputs` list it passes to `Rerank.generate`, although watsonx.ai reranks 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 watsonx.ai 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 watsonx.ai reranker. [watsonx.ai text rerank API](https://cloud.ibm.com/apidocs/watsonx-ai#text-rerank) ## Verification - Added `test_watsonx_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 `watsonx.py` is identical to `main`. I did not call the live watsonx.ai API. - Repository Ruff lint and format checks pass. --- python/python/lancedb/rerankers/watsonx.py | 5 +++ python/python/tests/test_rerankers.py | 50 ++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/python/python/lancedb/rerankers/watsonx.py b/python/python/lancedb/rerankers/watsonx.py index 9b948d589..a050d68b8 100644 --- a/python/python/lancedb/rerankers/watsonx.py +++ b/python/python/lancedb/rerankers/watsonx.py @@ -20,6 +20,8 @@ class WatsonxReranker(Reranker): Uses the ``ibm_watsonx_ai`` SDK (``Rerank.generate``) under the hood. + Rows with null values in ``column`` are omitted from the reranked results. + API Docs: https://cloud.ibm.com/docs/apis/watsonx-ai#text-rerank @@ -139,6 +141,9 @@ class WatsonxReranker(Reranker): return params def _rerank(self, result_set: pa.Table, query: str) -> pa.Table: + if len(result_set) > 0 and result_set[self.column].null_count: + # watsonx.ai 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_rerankers.py b/python/python/tests/test_rerankers.py index 24152f328..8488d99eb 100644 --- a/python/python/tests/test_rerankers.py +++ b/python/python/tests/test_rerankers.py @@ -1634,3 +1634,53 @@ def test_watsonx_reranker(tmp_path): table, schema = get_test_table(tmp_path) reranker = WatsonxReranker() _run_test_reranker(reranker, table, "single player experience", None, schema) + + +@pytest.mark.parametrize( + ("texts", "expected_ids"), + [ + (["first document", None, "last document"], [2, 0]), + ([None, None, None], []), + ], +) +def test_watsonx_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 = WatsonxReranker(top_n=2, return_score="all") + reranker._client = mocker.Mock() + + def generate(*, inputs, **kwargs): + assert inputs == ["first document", "last document"] + return {"results": [{"index": 1, "score": 0.9}, {"index": 0, "score": 0.7}]} + + reranker._client.generate.side_effect = generate + 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.generate.assert_called_once_with( + query="Which document is last?", + inputs=["first document", "last document"], + params=reranker._build_params(), + ) + else: + reranker._client.generate.assert_not_called()