haystack icon indicating copy to clipboard operation
haystack copied to clipboard

bug: FaithfulnessEvaluator / ContextRelevanceEvaluator silently return NaN when an LLM call fails

Open sachinn854 opened this issue 6 months ago • 1 comments

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:

  1. Create FaithfulnessEvaluator(raise_on_failure=False)
  2. Run evaluation with multiple queries
  3. One LLM call fails (timeout / API error)
  4. llm_evaluator.py appends None to results
  5. faithfulness.py line 171 converts None → {"score": float("nan")}
  6. faithfulness.py line 179: np_mean([..., nan, ...]) → nan
  7. Final result["score"] is nan — 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_statuses output)

FAQ Check

System:

  • OS: any
  • Haystack version: latest main
  • Component: FaithfulnessEvaluator, ContextRelevanceEvaluator

sachinn854 avatar May 23 '26 17:05 sachinn854

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 nan or None values before passing them to np.mean().
  • Log the warning: Use Python's logging module 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.

reallyticsai avatar May 27 '26 11:05 reallyticsai