ValueError
builtins.ValueError
Stack trace
ValueError: reranking score is NaN, indicating an invalid query or malformed input causing the model to fail scoring.
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
Query string is empty or contains only whitespace
Ensure the query string is non-empty and trimmed of whitespace before calling the reranking API.
Query contains unsupported or non-UTF8 characters
Sanitize the query input to remove or encode unsupported characters before reranking.
Malformed input format sent to the reranking endpoint
Verify the input JSON structure matches the reranking API specification exactly, including required fields.
Using an outdated or incompatible reranking model version
Upgrade to the latest reranking model version supported by the OpenAI SDK to avoid known bugs causing NaN scores.
Code: broken vs fixed
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) 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) 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.