High severity intermediate · Fix: 5-10 min

ValueError

ragas.evaluation.metrics.ValueError

What this error means
The Ragas evaluation metric calculation returns NaN due to invalid or missing data inputs during AI model testing.

Stack trace

traceback
Traceback (most recent call last):
  File "test_ragas.py", line 42, in <module>
    score = ragas.evaluate_metric(predictions, references)
  File "/usr/local/lib/python3.9/site-packages/ragas/evaluation/metrics.py", line 88, in evaluate_metric
    result = compute_metric(preds, refs)
  File "/usr/local/lib/python3.9/site-packages/ragas/evaluation/metrics.py", line 55, in compute_metric
    raise ValueError('Metric calculation resulted in NaN value')
ValueError: Metric calculation resulted in NaN value
QUICK FIX
Validate and clean your prediction and reference inputs to be non-empty, aligned, and numeric before calling ragas.evaluate_metric.

Why it happens

Ragas evaluation metrics compute scores based on predictions and references. If the input data contains empty lists, mismatched lengths, or all zero/empty values, the metric calculation can produce NaN due to division by zero or invalid operations.

Detection

Add validation checks before metric computation to ensure inputs are non-empty, aligned in length, and contain valid numeric data to catch NaN risks early.

Causes & fixes

1

Input prediction or reference lists are empty or contain only empty strings

✓ Fix

Validate and filter out empty or null entries from predictions and references before passing to the metric function.

2

Mismatch in length between predictions and references causing invalid metric computation

✓ Fix

Ensure predictions and references lists have the same length before evaluation.

3

All prediction outputs or references are identical or zero, leading to division by zero in metric formula

✓ Fix

Add a check to detect uniform or zero-only inputs and handle them by returning a default score or skipping metric calculation.

Code: broken vs fixed

Broken - triggers the error
python
import os
import ragas

predictions = ["", "", ""]  # Empty predictions
references = ["Answer1", "Answer2", "Answer3"]

# This line triggers ValueError due to NaN metric
score = ragas.evaluate_metric(predictions, references)
print(f"Ragas score: {score}")
Fixed - works correctly
python
import os
import ragas

# Clean empty predictions
predictions = ["", "", ""]
predictions = [p for p in predictions if p.strip() != ""]
references = ["Answer1", "Answer2", "Answer3"]

# Ensure lengths match after cleaning
if len(predictions) != len(references):
    raise ValueError("Predictions and references must have the same length after cleaning")

score = ragas.evaluate_metric(predictions, references)  # Fixed: cleaned inputs
print(f"Ragas score: {score}")
Added input cleaning to remove empty predictions and length check to prevent NaN errors during metric calculation.

Workaround

Wrap the metric call in try/except ValueError, and on exception, log inputs and return a default score like 0.0 to avoid crashing.

Prevention

Implement strict input validation and normalization pipelines before evaluation, and use unit tests to catch empty or invalid data cases early.

Python 3.9+ · ragas >=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.