Skip to content

Fix TokenOverlap crash when references list is empty - #1973

Open
romainsc wants to merge 1 commit into
IBM:mainfrom
romainsc:fix/token-overlap-empty-references
Open

Fix TokenOverlap crash when references list is empty#1973
romainsc wants to merge 1 commit into
IBM:mainfrom
romainsc:fix/token-overlap-empty-references

Conversation

@romainsc

@romainsc romainsc commented Jul 27, 2026

Copy link
Copy Markdown

Fix TokenOverlap crash when references list is empty

Summary

TokenOverlap.compute() raises ValueError: max() iterable argument is empty when references is an
empty list. This adds a guard that returns zero scores
instead of crashing.

Problem

When TokenOverlap is used via a metric pipeline that
maps a potentially empty field to references, the list
comprehension produces an empty results list, and
max(r[i] for r in results) raises ValueError.

This occurs in practice with the faithfulness metric
(metrics.rag.external_rag.faithfulness), which copies
contexts to references. If a vector store query
returns zero chunks for a given question (e.g. due to
eventual consistency in the vector database, or a query
with no semantic match), contexts is an empty list,
and TokenOverlap crashes instead of returning a zero
score.

Reproduction

import pandas as pd
from unitxt.eval_utils import evaluate

data = [{
    "question": "Q1",
    "answer": "Some answer",
    "ground_truths": ["Ground truth"],
    "contexts": [],  # no chunks retrieved
    "context_ids": [],
    "question_id": "q1",
    "additional_data": None,
    "ground_truths_context_ids": ["doc1"],
}]
df = pd.DataFrame(data)
evaluate(
    df,
    metric_names=[
        "metrics.rag.external_rag.faithfulness"
    ],
    compute_conf_intervals=True,
)
# ValueError: max() iterable argument is empty

Root cause in practice

This was encountered using the AutoRAG pipeline in
Red Hat OpenShift AI 3.4 with a Milvus vector store
configured with consistency_level=Bounded. With fast
GPU-based embedding, chunks are inserted and queried
within ~100ms. Bounded consistency does not guarantee
immediate visibility of inserted data, so some queries
return zero results. The faithfulness metric then
receives contexts=[], which maps to references=[],
triggering the crash.

While the operational fix is to use Strong consistency
in Milvus, TokenOverlap should handle empty references
gracefully regardless of the upstream cause.

Fix

Add if not results: return {"precision": 0, "recall": 0, "f1": 0} before the max() call, consistent with the
zero-score behavior already present in
_compute_single_ref when num_same == 0.

Test

Added test_token_overlap_empty_references that verifies
the metric returns zero scores when references is empty,
instead of crashing.

Developed with AI assistance (Claude). Code reviewed and empirically validated by the author.

@romainsc
romainsc force-pushed the fix/token-overlap-empty-references branch from 674b9a7 to 93dbd8d Compare July 29, 2026 09:50
TokenOverlap.compute() raises ValueError when references
is an empty list because max() is called on an empty
generator. This occurs via the faithfulness metric when
contexts is empty (e.g. vector store returns zero chunks
due to eventual consistency).

Add guard: if not results, return zero scores.
Add test: test_token_overlap_empty_references.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Romain Chantereau <romain@chantereau.fr>
@romainsc
romainsc force-pushed the fix/token-overlap-empty-references branch from 93dbd8d to 095637f Compare July 29, 2026 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant