Skip to content

fix(token_classification): prevent IndexError in issues_from_scores on empty or all-below threshold scores - #1343

Open
aakash1552005 wants to merge 1 commit into
cleanlab:masterfrom
aakash1552005:fix/token-classification-rank-bounds-check
Open

aakash1552005 wants to merge 1 commit into
cleanlab:masterfrom
aakash1552005:fix/token-classification-rank-bounds-check

Conversation

@aakash1552005

@aakash1552005 aakash1552005 commented Oct 13, 2026 •

Copy link
Copy Markdown

Summary

Fixes #1338

In \cleanlab.token_classification.rank.issues_from_scores, when \ oken_scores\ is not provided, the while loop:
\\python
while sentence_scores[ranking[cutoff]] < threshold and cutoff < len(ranking):
cutoff += 1
\
evaluated the array index \sentence_scores[ranking[cutoff]]\ before evaluating the bounds check \cutoff < len(ranking).

When all sentence scores fall below \ hreshold\ (or when given an empty sequence []\ or a single element below threshold), \cutoff\ increments to \len(ranking)\ and the next condition check indexed out of bounds, raising \IndexError.

Changes

  • Reordered the while loop condition in \cleanlab/token_classification/rank.py\ to \while cutoff < len(ranking) and scores[ranking[cutoff]] < threshold:\ ensuring bounds check short-circuits before indexing.
  • Used \scores = np.asarray(sentence_scores)\ so \scores\ handles lists, NumPy arrays, and pandas Series with non-default indexes consistently.
  • Added comprehensive unit tests in \ ests/test_token_classification.py::test_issues_from_scores_edge_cases\ covering:
    • All scores below threshold
    • Single score below threshold
    • Empty array and empty list inputs
    • No scores below threshold
    • pandas Series with non-default index

…n empty or all-below threshold scores

Resolves cleanlab#1338

- Bounds-check cutoff < len(ranking) before evaluating scores[ranking[cutoff]] to avoid IndexError when all scores are below threshold or input is empty
- Convert sentence_scores with np.asarray to support array, list, and pandas Series inputs cleanly
- Add regression unit tests in test_token_classification.py covering all-below, single-below, empty, and pandas Series inputs
@CLAassistant

CLAassistant commented Oct 13, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

No deployments
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.

token_classification.rank.issues_from_scores() raises IndexError when every sentence score is below threshold (or input is empty)

2 participants