From 7c6ffe94c2cf47949642ca88e7eef8a323e65ed4 Mon Sep 17 00:00:00 2001 From: Divyam Talwar Date: Mon, 30 Nov 2026 17:43:28 +0530 Subject: [PATCH] fix(python): skip null texts when reranking with Cohere (#5283) ## Summary An Embedded vector search can include a row whose reranking text is null. The Cohere reranker sent that null in its documents array, which made the request invalid and prevented the remaining documents from being ranked. Filter null texts before building the request. Apply Cohere's result indexes to the same filtered Arrow table so the scores follow the correct rows. When every candidate has null text, return an empty scored table without sending a request. [Cohere Rerank API v1 document types](https://docs.cohere.com/v1/reference/rerank) Closes #5284. ## Verification - Reproduced with an Embedded table, the installed Cohere SDK and an in-process HTTP transport that enforced the documented document types. The base sent `["first", null, "last"]` and received the fixture's 400 response. The fix sent `["first", "last"]` and returned the original row IDs in the reranker's order. - Added Embedded search regressions for mixed and all-null candidate sets. Both fail on the parent and pass after the change. - Passed 12 relevant reranker tests and the repository-pinned Ruff lint and format checks. A second reviewer independently reran the 12 tests. --- python/python/lancedb/rerankers/cohere.py | 4 ++ python/python/tests/test_rerankers.py | 56 +++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/python/python/lancedb/rerankers/cohere.py b/python/python/lancedb/rerankers/cohere.py index a1e797f15..2ace18fc2 100644 --- a/python/python/lancedb/rerankers/cohere.py +++ b/python/python/lancedb/rerankers/cohere.py @@ -18,6 +18,8 @@ class CohereReranker(Reranker): Reranks the results using the Cohere Rerank API. https://docs.cohere.com/docs/rerank-guide + Rows with null values in ``column`` are omitted from the reranked results. + Parameters ---------- model_name : str, default "rerank-english-v2.0" @@ -65,6 +67,8 @@ class CohereReranker(Reranker): return cohere.Client(self.api_key or os.environ.get("COHERE_API_KEY")) def _rerank(self, result_set: pa.Table, query: str): + if len(result_set) > 0 and result_set[self.column].null_count: + 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 170893f76..8685f2792 100644 --- a/python/python/tests/test_rerankers.py +++ b/python/python/tests/test_rerankers.py @@ -1098,6 +1098,62 @@ def test_model_reranker_empty_results_preserve_schema(mocker, return_score): reranker._client.rerank.assert_not_called() +@pytest.mark.parametrize( + ("texts", "expected_ids"), + [ + (["first document", None, "last document"], [2, 0]), + ([None, None, None], []), + ], +) +def test_cohere_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 = CohereReranker(top_n=2, return_score="all") + 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_n=2, + model=reranker.model_name, + ) + 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")