stanfordnlp/CoreNLP · error · IllegalArgumentException

Could not compute span from tokens!

Error message

Could not compute span from tokens!

What it means

Util.tokensToSpan computes the token-index span covering a collection of CoreLabel/HasIndex tokens, initializing min to Integer.MAX_VALUE and max to Integer.MIN_VALUE. If no token yields a valid index (min stays at the sentinel or max stays at the sentinel), it throws IllegalArgumentException "Could not compute span from tokens!" — effectively meaning the token list is empty or all indices are degenerate.

Solutions

  1. Check that the token collection is non-empty and tokens have valid (>=1) sentence indices before calling tokensToSpan
  2. Fix upstream sentence/token extraction so indices are populated
  3. Catch IllegalArgumentException and return a default/empty span

Example fix

// before
Span s = Util.tokensToSpan(tokens);
// after
if (tokens.isEmpty()) return null;
Span s = Util.tokensToSpan(tokens);
Defensive patterns

Strategy: validation

Validate before calling

if (tokens == null || tokens.isEmpty() || tokens.stream().anyMatch(t -> t.index() < 1)) return null; // skip tokensToSpan

Type guard

boolean hasValidTokens(List<? extends HasIndex> toks) { return toks != null && !toks.isEmpty() && toks.stream().allMatch(t -> t.index() >= 1); }

Try / catch

try { return Util.tokensToSpan(tokens); } catch (IllegalArgumentException e) { return null; }

Prevention

When it happens

Trigger: Calling Util.tokensToSpan with an empty token collection (min remains Integer.MAX_VALUE... note the check is min < 0 || max == Integer.MAX_VALUE) or with tokens having out-of-range indices, so min < 0 or max == Integer.MIN_VALUE path produces the sentinel condition.

Common situations: Passing an empty list of tokens from a failed sentence split; tokens with index 0 or unset indices from partially populated CoreLabels.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/084b942e7e421e57. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/naturalli/Util.java:625

     add("past");
     add("proposed");
  }});

  /**
   * Construct the spanning span of the given list of tokens.
   *
   * @param tokens The tokens that should define the span.
   * @return A span (0-indexed) that covers all of the tokens.
   */
  public static Span tokensToSpan(List<? extends HasIndex> tokens) {
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    for (HasIndex token : tokens) {
      min = Math.min(token.index() - 1, min);
      max = Math.max(token.index(), max);
    }
    if (min < 0 || max == Integer.MAX_VALUE) {
      throw new IllegalArgumentException("Could not compute span from tokens!");
    } else if (min >= max) {
      throw new IllegalStateException("Either logic is broken or Gabor can't code.");
    } else {
      return new Span(min, max);
    }
  }
}

View on GitHub (pinned to 1b7edd19c4)