High severity intermediate · Fix: 2-5 min

ValueError

builtins.ValueError

What this error means
The reranking API returned a NaN score due to an invalid or malformed query input that the model could not process.

Stack trace

traceback
ValueError: reranking score is NaN, indicating an invalid query or malformed input causing the model to fail scoring.
QUICK FIX
Validate and sanitize your query input to be non-empty and UTF-8 encoded before calling the reranking API.

Why it happens

Reranking models expect valid, non-empty query strings and well-formed candidate documents. If the query is empty, contains unsupported characters, or the input format is incorrect, the model may return NaN scores. This indicates the model could not compute a valid relevance score for the given input.

Detection

Validate query strings before sending to the reranking API by checking for empty or malformed inputs and log any NaN scores returned to catch invalid queries early.

Causes & fixes

1

Query string is empty or contains only whitespace

✓ Fix

Ensure the query string is non-empty and trimmed of whitespace before calling the reranking API.

2

Query contains unsupported or non-UTF8 characters

✓ Fix

Sanitize the query input to remove or encode unsupported characters before reranking.

3

Malformed input format sent to the reranking endpoint

✓ Fix

Verify the input JSON structure matches the reranking API specification exactly, including required fields.

4

Using an outdated or incompatible reranking model version

✓ Fix

Upgrade to the latest reranking model version supported by the OpenAI SDK to avoid known bugs causing NaN scores.

Code: broken vs fixed

Broken - triggers the error
python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))

query = "   "  # Empty or whitespace query
candidates = ["Document 1 text", "Document 2 text"]

# This line triggers ValueError due to NaN score from invalid query
scores = client.reranking.score(query=query, candidates=candidates)
print(scores)
Fixed - works correctly
python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

query = "What is AI?"  # Fixed: non-empty, valid query
candidates = ["Document 1 text", "Document 2 text"]

# Fixed: query validated and sanitized to avoid NaN scores
scores = client.reranking.score(query=query.strip(), candidates=candidates)
print(scores)
Added query validation by ensuring it is non-empty and stripped of whitespace before calling reranking to prevent NaN score errors.

Workaround

Wrap the reranking call in try/except ValueError, and if a NaN score error occurs, log the raw inputs and fallback to a default ranking or skip reranking for that query.

Prevention

Implement strict input validation and sanitization for queries before reranking calls, and use schema validation to enforce correct input formats to avoid invalid queries causing NaN scores.

Python 3.9+ · openai >=1.0.0 · tested on 1.x
Verified 2026-04 · gpt-4o-mini, gemini-2.5-pro
Verify ↗

Community Notes

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