fix(python): skip null texts when reranking with AssetFeed (#5346)

## Summary

A vector, full-text, or hybrid search can return rows whose reranking
text column is
ull. AssetFeedReranker._rerank currently forwards None values inside the
JSON documents list to the AssetFeed rerank API, which rejects or
misaligns results when null texts are present, still makes an HTTP
request when every candidate text is
ull, and counts null rows against MAX_DOCUMENTS = 100.

This aligns AssetFeedReranker with the existing null-filtering pattern
in #5283 (Cohere), #5296 (Jina), #5336 (VoyageAI), and #5337
(watsonx.ai):

- Filter out rows where esult_set[self.column] is null before
self._handle_empty_results(result_set) and MAX_DOCUMENTS validation, so
returned AssetFeed result indices select the exact matching rows and
keep all original columns.
- Return an empty loat32-scored result table without calling the
AssetFeed API when all candidate rows have null text.
- Document in AssetFeedReranker that rows with null values in column are
omitted from the reranked results.

## Verification

- Added unit tests in python/python/tests/test_assetfeed_reranker.py:
- est_nullable_documents (["first document", None, "last document"] and
[None, None, None]): verifies only non-null strings are sent, row
IDs/scores/_distance/ loat32 _relevance_score align cleanly, and
all-null inputs skip the HTTP request.
- est_null_documents_excluded_before_max_documents_check: verifies 100
valid documents plus 1 null row pass MAX_DOCUMENTS validation after null
filtering.
- Ran pytest -o addopts="" --noconftest -v
python/python/tests/test_assetfeed_reranker.py on Python 3.11.18 +
PyArrow 26.0.1 (15 passed in 1.16s with lancedb._lancedb stubbed in the
scratch test runner because Windows Application Control on the local
test machine blocks the unsigned native Rust .pyd binary; mocked HTTP
session, no live AssetFeed API calls).
- Verified uff check and uff format --check pass cleanly on both changed
files.
This commit is contained in:
PNJ
2026-12-02 15:10:40 +08:00
committed by GitHub
parent 43d28def80
commit 9b3398e31b
2 changed files with 65 additions and 0 deletions
@@ -19,6 +19,8 @@ class AssetFeedReranker(Reranker):
Reranks the results using the AssetFeed rerank API.
https://assetfeed.com/models/reranker
Rows with null values in ``column`` are omitted from the reranked results.
Parameters
----------
model_name : str, default "assetfeed/assetfeed-rerank-v1"
@@ -68,6 +70,9 @@ class AssetFeedReranker(Reranker):
return session
def _rerank(self, result_set: pa.Table, query: str):
if len(result_set) > 0 and result_set[self.column].null_count:
# AssetFeed 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
@@ -179,3 +179,63 @@ def test_rerank_hybrid_return_score_all(api):
)
result = AssetFeedReranker(return_score="all").rerank_hybrid("query", vector, fts)
assert {"_distance", "_score", "_relevance_score"} <= set(result.column_names)
@pytest.mark.parametrize(
("texts", "expected_ids"),
[
(["first document", None, "last document"], [2, 0]),
([None, None, None], []),
],
)
def test_nullable_documents(api, texts, expected_ids):
calls, reply = api
reply["results"] = [
{"index": 1, "relevance_score": 0.9},
{"index": 0, "relevance_score": 0.4},
]
table = pa.table(
{
"id": [0, 1, 2],
"text": pa.array(texts, type=pa.string()),
"_distance": pa.array([0.0, 0.5, 1.0], type=pa.float32()),
}
)
result = AssetFeedReranker(top_n=2, return_score="all").rerank_vector(
"Which document is last?", table
)
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.4])
assert calls == [
(
API_URL,
{
"model": "assetfeed/assetfeed-rerank-v1",
"query": "Which document is last?",
"documents": ["first document", "last document"],
"top_n": 2,
},
)
]
else:
assert calls == []
def test_null_documents_excluded_before_max_documents_check(api):
calls, reply = api
reply["results"] = [{"index": i, "relevance_score": 0.5} for i in range(100)]
texts = [str(i) for i in range(100)] + [None]
table = pa.table(
{
"text": pa.array(texts, type=pa.string()),
"_distance": pa.array([0.0] * 101, type=pa.float32()),
}
)
result = AssetFeedReranker().rerank_vector("query", table)
assert len(result) == 100
assert len(calls) == 1
assert len(calls[0][1]["documents"]) == 100