Fix TokenOverlap crash when references list is empty - #1973
Open
romainsc wants to merge 1 commit into
Open
Conversation
romainsc
force-pushed
the
fix/token-overlap-empty-references
branch
from
July 29, 2026 09:50
674b9a7 to
93dbd8d
Compare
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
force-pushed
the
fix/token-overlap-empty-references
branch
from
July 29, 2026 09:59
93dbd8d to
095637f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix TokenOverlap crash when references list is empty
Summary
TokenOverlap.compute()raisesValueError: max() iterable argument is emptywhenreferencesis anempty list. This adds a guard that returns zero scores
instead of crashing.
Problem
When
TokenOverlapis used via a metric pipeline thatmaps a potentially empty field to
references, the listcomprehension produces an empty
resultslist, andmax(r[i] for r in results)raisesValueError.This occurs in practice with the
faithfulnessmetric(
metrics.rag.external_rag.faithfulness), which copiescontextstoreferences. If a vector store queryreturns zero chunks for a given question (e.g. due to
eventual consistency in the vector database, or a query
with no semantic match),
contextsis an empty list,and
TokenOverlapcrashes instead of returning a zeroscore.
Reproduction
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 fastGPU-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 toreferences=[],triggering the crash.
While the operational fix is to use
Strongconsistencyin Milvus,
TokenOverlapshould handle empty referencesgracefully regardless of the upstream cause.
Fix
Add
if not results: return {"precision": 0, "recall": 0, "f1": 0}before themax()call, consistent with thezero-score behavior already present in
_compute_single_refwhennum_same == 0.Test
Added
test_token_overlap_empty_referencesthat verifiesthe metric returns zero scores when references is empty,
instead of crashing.
Developed with AI assistance (Claude). Code reviewed and empirically validated by the author.