High severity beginner · Fix: 2-5 min

ValueError

builtins.ValueError

What this error means
Reranking fails because the query string provided to the reranker is empty or None, which is invalid input for the reranking model.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 42, in <module>
    rerank_results = reranker.rerank(query, documents)
  File "/usr/local/lib/python3.9/site-packages/langchain/rerankers/base.py", line 88, in rerank
    raise ValueError("Query string for reranking cannot be empty.")
ValueError: Query string for reranking cannot be empty.
QUICK FIX
Add a check to raise an error or provide a default non-empty query string before calling rerank.

Why it happens

Reranking models require a non-empty query string to compare and score documents. If the query string is empty or None, the reranker cannot compute relevance scores, causing it to raise a ValueError. This often happens when upstream code fails to pass or generate a valid query.

Detection

Add input validation before calling rerank to assert the query string is not empty or None, and log the query value to catch missing or empty inputs early.

Causes & fixes

1

The query string variable passed to the reranker is an empty string.

✓ Fix

Ensure the query string is populated with a meaningful non-empty string before calling the reranker.

2

The query string is None due to a missing or failed extraction from user input or previous pipeline step.

✓ Fix

Add checks to confirm the query extraction step returns a valid string and handle None cases with fallback or error messages.

3

The reranker is called before the query is generated or assigned in asynchronous or multi-step workflows.

✓ Fix

Reorder code or add synchronization to guarantee the query string is ready and valid before reranking.

Code: broken vs fixed

Broken - triggers the error
python
from langchain.rerankers import OpenAIReranker

reranker = OpenAIReranker()
query = ""  # Empty query string
documents = ["doc1", "doc2"]
rerank_results = reranker.rerank(query, documents)  # This line raises ValueError
print(rerank_results)
Fixed - works correctly
python
import os
from langchain.rerankers import OpenAIReranker

# Ensure OPENAI_API_KEY is set in environment
# os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")  # Usually set externally

reranker = OpenAIReranker()
query = "What is AI?"  # Non-empty query string
if not query:
    raise ValueError("Query string for reranking cannot be empty.")
documents = ["doc1", "doc2"]
rerank_results = reranker.rerank(query, documents)  # Fixed: query is non-empty
print(rerank_results)
Added a non-empty query string and a validation check before calling rerank to prevent the ValueError caused by empty input.

Workaround

Wrap the rerank call in try/except ValueError, and if caught, assign a default query string or skip reranking to avoid crashing.

Prevention

Implement strict input validation and pipeline checks to guarantee the query string is always non-empty before reranking, and use type hints or assertions to catch issues early.

Python 3.9+ · langchain-core >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

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