High severity beginner · Fix: 2-5 min

ValueError

builtins.ValueError

What this error means
This error occurs when the reranking function's top_n parameter exceeds the number of available documents to rerank.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 42, in <module>
    reranked_docs = reranker.rerank(documents, query, top_n=10)  # triggers error
  File "/usr/local/lib/python3.9/site-packages/reranking_lib/core.py", line 88, in rerank
    raise ValueError(f"top_n={top_n} cannot be greater than number of documents={len(documents)}")
ValueError: top_n=10 cannot be greater than number of documents=5
QUICK FIX
Add a check to set top_n = min(top_n, len(documents)) before calling the rerank function.

Why it happens

The reranking function expects the top_n parameter to specify how many top documents to return after reranking. If top_n is larger than the number of documents provided, the function raises a ValueError to prevent invalid indexing or empty results.

Detection

Validate the top_n parameter against the length of the documents list before calling rerank, or catch ValueError exceptions and log the mismatch details.

Causes & fixes

1

top_n parameter is set larger than the number of documents available for reranking

✓ Fix

Ensure top_n is less than or equal to the length of the documents list before calling rerank, e.g., top_n = min(top_n, len(documents))

2

Documents list is empty or smaller than expected due to upstream filtering or data loading issues

✓ Fix

Add checks to confirm documents list is populated correctly before reranking and handle empty or small lists gracefully

3

Hardcoded top_n value without dynamic adjustment based on input size

✓ Fix

Make top_n a dynamic parameter that adjusts to the input documents count or add validation logic to prevent invalid values

Code: broken vs fixed

Broken - triggers the error
python
documents = ['doc1', 'doc2', 'doc3', 'doc4', 'doc5']
query = 'example query'

# This line triggers the ValueError because top_n=10 > len(documents)=5
reranked_docs = reranker.rerank(documents, query, top_n=10)
Fixed - works correctly
python
documents = ['doc1', 'doc2', 'doc3', 'doc4', 'doc5']
query = 'example query'

# Fix: Adjust top_n to not exceed number of documents
top_n = 10
top_n = min(top_n, len(documents))

reranked_docs = reranker.rerank(documents, query, top_n=top_n)
print('Reranking succeeded with top_n:', top_n)
Added a min() check to ensure top_n does not exceed the number of documents, preventing the ValueError.

Workaround

Wrap the rerank call in try/except ValueError, and if caught, reduce top_n to the documents count and retry reranking.

Prevention

Always validate or dynamically set top_n based on the input documents length before reranking to avoid invalid parameter errors.

Python 3.9+ · reranking-lib >=1.0.0 · tested on 1.2.0
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.