bug: FaithfulnessEvaluator / ContextRelevanceEvaluator silently return NaN when an LLM call fails
Describe the bug
FaithfulnessEvaluator and ContextRelevanceEvaluator silently return nan as the final
score when any LLM call fails (API error, timeout, network issue) and raise_on_failure=False.
No error is raised, no warning is logged — the user just gets nan with no explanation.
Error message
No error is thrown. Output is:
result["score"] # → nan
result["individual_scores"] # → [0.9, nan, 0.85]
Expected behavior
Failed queries should be excluded from the aggregate score, and a warning should be logged telling the user how many queries were skipped:
result["score"] # → 0.875 (average of valid scores only)
# WARNING: "1 query failed and was excluded from the score."
To Reproduce
import numpy as np
# This is exactly what happens inside faithfulness.py line 179
# when 1 out of 3 LLM calls fails
scores = [0.9, float("nan"), 0.85] # nan = failed LLM call
print(np.mean(scores)) # → nan (silent, no warning)
Full flow:
- Create
FaithfulnessEvaluator(raise_on_failure=False) - Run evaluation with multiple queries
- One LLM call fails (timeout / API error)
llm_evaluator.pyappendsNoneto resultsfaithfulness.pyline 171 convertsNone→{"score": float("nan")}faithfulness.pyline 179:np_mean([..., nan, ...])→nan- Final
result["score"]isnan— no error, no warning
Root Cause
faithfulness.py line 171:
if res is None:
result["results"][idx] = {"statements": [], "statement_scores": [], "score": float("nan")}
faithfulness.py line 179 (same pattern in context_relevance.py line 185):
result["score"] = np_mean([res["score"] for res in result["results"]])
# numpy.mean and statistics.mean both propagate NaN silently
Proposed Fix
valid_scores = [res["score"] for res in result["results"]
if not math.isnan(res["score"])]
result["score"] = np_mean(valid_scores) if valid_scores else float("nan")
failed = len(result["results"]) - len(valid_scores)
if failed:
logger.warning("{failed} queries failed and were excluded from the score.", failed=failed)
Additional context
- This is distinct from #7671 (fixed in PR #7685) which handled empty-statements NaN — that was when LLM responded but returned no statements. This bug is about the LLM call itself failing.
- Related to #7973 (JSON parse failure NaN)
- Related to ongoing PR #11333 (adding
evaluation_statusesoutput)
FAQ Check
- [x] Have you had a look at our new FAQ page?
System:
- OS: any
- Haystack version: latest main
- Component:
FaithfulnessEvaluator,ContextRelevanceEvaluator
This issue with FaithfulnessEvaluator and ContextRelevanceEvaluator returning NaN without any warning feels familiar. In production, we’ve encountered similar situations where silent failures lead to confusion.
To handle this, I suggest modifying the scoring logic. Instead of allowing None results to propagate through to the final score calculation, we could filter these out explicitly before averaging. Here’s a quick idea on how to implement this:
- Filter invalid scores: Create a list comprehension to exclude
nanor None values before passing them tonp.mean(). - Log the warning: Use Python's
loggingmodule to add a warning when a score is excluded from the average.
Here's a small snippet to illustrate:
valid_scores = [score for score in scores if score is not None and not np.isnan(score)]
if len(valid_scores) != len(scores):
logging.warning(f"{len(scores) - len(valid_scores)} queries failed and were excluded from the score.")
result["score"] = np.mean(valid_scores) if valid_scores else float("nan")
This approach keeps the user informed and prevents the silent nan trap. A similar pattern has worked well for us in our voice agent evaluations.