From ce6923fa826f8e4f2d0f7d78c499e45c1386f653 Mon Sep 17 00:00:00 2001 From: adityaanikam Date: Mon, 17 Aug 2026 14:03:48 +0530 Subject: [PATCH 1/2] Fix reranked scores not matching reranked document order in HFCrossEncoderReranker --- redisvl/utils/rerank/hf_cross_encoder.py | 2 +- .../integration/test_cross_encoder_reranker.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/redisvl/utils/rerank/hf_cross_encoder.py b/redisvl/utils/rerank/hf_cross_encoder.py index fd6b3325a..0543b6a97 100644 --- a/redisvl/utils/rerank/hf_cross_encoder.py +++ b/redisvl/utils/rerank/hf_cross_encoder.py @@ -121,7 +121,7 @@ def rank( docs_with_scores = list(zip(doc_subset, scores)) docs_with_scores.sort(key=lambda x: x[1], reverse=True) reranked_docs = [doc for doc, _ in docs_with_scores[:limit]] - scores = scores[:limit] + scores = [score for _, score in docs_with_scores[:limit]] if return_score: return reranked_docs, scores # type: ignore diff --git a/tests/integration/test_cross_encoder_reranker.py b/tests/integration/test_cross_encoder_reranker.py index a43115446..3196cc8b4 100644 --- a/tests/integration/test_cross_encoder_reranker.py +++ b/tests/integration/test_cross_encoder_reranker.py @@ -33,6 +33,23 @@ async def test_async_rank_documents(reranker): assert all(isinstance(score, float) for score in scores) +def test_rank_scores_align_with_reranked_docs(reranker): + # https://github.com/redis/redis-vl-python/issues/693 + docs = [ + "irrelevant document", + "highly relevant document", + "somewhat relevant document", + ] + query = "relevant document" + + reranked_docs, scores = reranker.rank(query, docs, limit=len(docs)) + + # reranked_docs is sorted by score descending, so scores must be too, + # this catches the case where scores are taken from the original + # unsorted list instead of the sorted one. + assert scores == sorted(scores, reverse=True) + + def test_bad_input(reranker): with pytest.raises(ValueError): reranker.rank("", []) # Empty query From 919bddc4a9463a36bab90c73aa141e96ea57b16c Mon Sep 17 00:00:00 2001 From: adityaanikam Date: Mon, 17 Aug 2026 16:34:54 +0530 Subject: [PATCH 2/2] Apply reviewer suggestion to unzip docs and scores in one pass --- redisvl/utils/rerank/hf_cross_encoder.py | 4 ++-- tests/integration/test_cross_encoder_reranker.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/redisvl/utils/rerank/hf_cross_encoder.py b/redisvl/utils/rerank/hf_cross_encoder.py index 0543b6a97..d28f8b94c 100644 --- a/redisvl/utils/rerank/hf_cross_encoder.py +++ b/redisvl/utils/rerank/hf_cross_encoder.py @@ -120,8 +120,8 @@ def rank( scores = [float(score) for score in scores] docs_with_scores = list(zip(doc_subset, scores)) docs_with_scores.sort(key=lambda x: x[1], reverse=True) - reranked_docs = [doc for doc, _ in docs_with_scores[:limit]] - scores = [score for _, score in docs_with_scores[:limit]] + reranked_docs_tuple, scores_tuple = zip(*docs_with_scores[:limit]) + reranked_docs, scores = list(reranked_docs_tuple), list(scores_tuple) if return_score: return reranked_docs, scores # type: ignore diff --git a/tests/integration/test_cross_encoder_reranker.py b/tests/integration/test_cross_encoder_reranker.py index 3196cc8b4..528b264e8 100644 --- a/tests/integration/test_cross_encoder_reranker.py +++ b/tests/integration/test_cross_encoder_reranker.py @@ -47,7 +47,7 @@ def test_rank_scores_align_with_reranked_docs(reranker): # reranked_docs is sorted by score descending, so scores must be too, # this catches the case where scores are taken from the original # unsorted list instead of the sorted one. - assert scores == sorted(scores, reverse=True) + assert list(scores) == sorted(scores, reverse=True) def test_bad_input(reranker):