Sub topic 11
Reranking retrieved passages
This chapter assumes you can write basic Python, know how top-k retrieval and reciprocal rank fusion work, and understand that embeddings turn text into vectors. It covers a second ranking pass that reorders the passages your first retrieval step returns.
Why a second pass helps
Vector search is fast because it embeds the query and each passage separately. The passage vectors are computed ahead of time, so the search only compares numbers. The cost is accuracy. A single vector has to summarize a whole passage, so fine details of how the query relates to the passage get lost.
A reranker is a model that reads the query and one passage together and outputs a relevance score. Because it sees both texts at once, it can notice negation, qualifiers, and specific terms that a vector comparison blurs. It is too slow to run over the whole corpus, so you run it only on a short candidate list.
The usual pattern has two stages:
- Retrieve a wide set of candidates, such as the top 50, with vector search, keyword search, or a hybrid of both.
- Rerank those candidates and keep the best few, such as the top 5, for the prompt.
Bi-encoders and cross-encoders
The embedding model used for first-stage search is a bi-encoder. It encodes the query and the passage independently. A reranker is usually a cross-encoder. It takes the query and passage as one input and scores the pair.
| Property | Bi-encoder | Cross-encoder |
|---|---|---|
| Input | One text at a time | Query and passage together |
| Precomputable | Yes, for passages | No, one pass per pair |
| Cost per query | Low | Grows with candidate count |
| Typical role | Candidate retrieval | Reordering candidates |
A reranking step in Python
This example uses the sentence-transformers library. Install it with pip install sentence-transformers. The model downloads on first run.
from sentence_transformers import CrossEncoder
model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
def rerank(query, passages, top_n=3):
pairs = [(query, p) for p in passages]
scores = model.predict(pairs)
ranked = sorted(zip(scores, passages), key=lambda x: x[0], reverse=True)
return [p for _, p in ranked[:top_n]]
candidates = [
"Refunds are issued within 14 days of a returned item.",
"Our office is closed on public holidays.",
"To request a refund, contact support with your order number.",
"Shipping is free for orders over 50 dollars.",
]
print(rerank("How do I get a refund?", candidates, top_n=2))The scores from this model are raw values, and they are not probabilities. Use them only to compare passages for the same query.
Choosing the candidate count
The candidate count is a trade-off. If it is too small, the right passage may never reach the reranker, and no reranker can recover a passage that retrieval missed. If it is too large, latency and cost rise. Measure recall at k for the first stage to pick a value. Then check how much the reranker improves the final top results.
Common problems:
- Long passages are truncated. Cross-encoders have an input limit, so check your chunk size against it.
- Domain mismatch. A model trained on web search may rank poorly on legal or medical text. Test on your own queries.
- Added latency. Each query pays for N model calls. Batch the pairs and consider a hosted reranking API or a GPU.
Exercise
Your first-stage search returns 10 candidates, and the best passage is at rank 14. Will adding a reranker fix this? What would you change?
Show answer
No. A reranker only reorders the candidates it receives, so a passage outside the top 10 is never seen. Increase the first-stage candidate count (for example to 30 or 50) so the passage is included, then rerank and keep the top few.