Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#1338·cleanlab

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

Author: shaurya416Created Sep 26, 2026Updated Sep 26, 2026

cleanlab/token_classification/rank.py:223-228 (crash at line 226)

python
    else:
        ranking = np.argsort(sentence_scores)
        cutoff = 0
        while sentence_scores[ranking[cutoff]] < threshold and cutoff < len(ranking):
            cutoff += 1
        return ranking[:cutoff]

Python evaluates and from left to right, so the array access sentence_scores[ranking[cutoff]] runs before the bounds check cutoff < len(ranking). When every score is below threshold, cutoff reaches len(ranking) and the next loop test indexes past the end of the array. The function raises IndexError instead of returning all indices. An empty sentence_scores array crashes on the first iteration.

This path runs whenever issues_from_scores is called without token_scores, which is how the docstring example calls it.

The same fix, with a repro through the real get_label_quality_scores pipeline (labels=[[0,0,1],[0,1]], threshold=0.99), is in open PR #1078 ("Optimize token_classification/rank.py for performance"). That PR bundles the fix with an unrelated performance refactor and has not been merged, so the crash is still present on master.

Measured

The function body was extracted with ast.get_source_segment at HEAD 7506257 and run in an isolated namespace with only numpy and typing injected.

Input Expected Actual
Docstring example [0.1,0.3,0.6,0.2,0.05,0.9,0.8,0.0125,0.5,0.6], threshold=0.1 array([7, 4]) array([7, 4]) (control)
[0.6, 0.0, 0.8], threshold=0.1 (shape of tests/test_token_classification.py:276-283) array([1]) array([1]) (control)
[0.5, 0.6, 0.9], threshold=0.1 (none below threshold) array([]) array([]) (control)
[0.01, 0.02, 0.03], threshold=0.1 (all below threshold) array([0, 1, 2]) IndexError: index 3 is out of bounds for axis 0 with size 3
[0.01], threshold=0.1 array([0]) IndexError: index 1 is out of bounds for axis 0 with size 1
[], threshold=0.1 array([]) IndexError: index 0 is out of bounds for axis 0 with size 0
[0.2, 0.3], threshold=1.0 array([0, 1]) IndexError: index 2 is out of bounds for axis 0 with size 2

The only existing test (tests/test_token_classification.py:276-283) has exactly one of three scores below threshold, so CI never reaches the all-below or empty boundary.

Consequence

The sentence-level mode crashes instead of returning a result whenever no score is at or above threshold. This happens with a heavily mislabeled batch, a single-sentence input with a low score, an empty input, or a permissive threshold such as threshold=1.0 (the value the object-detection module passes to its own issues_from_scores). Callers get an exception instead of the list of flagged sentences.

Suggested fix

Put the bounds check first so that it short-circuits before the index access:

python
while cutoff < len(ranking) and sentence_scores[ranking[cutoff]] < threshold:
    cutoff += 1
return ranking[:cutoff]

Or use a loop-free form:

python
ranking = np.argsort(sentence_scores)
cutoff = np.searchsorted(sentence_scores[ranking], threshold)
return ranking[:cutoff]

Both forms return the expected value for every row in the table above. cleanlab/segmentation/rank.py:184-186 also uses np.searchsorted but returns ranking[: cutoff + 1]. That slice includes one index whose score is at or above the threshold (for example, [0.05, 0.5, 0.9] with threshold 0.1 returns [0, 1]), so it should not be copied as is. Regression tests for the all-below, single-element and empty cases would cover this boundary.

Source: cleanlab/cleanlab

View original on GitHubView discussion on GitHub