stanfordnlp/CoreNLP · error · IllegalArgumentException
Span must be entirely contained in the sentence:
Error message
Span must be entirely contained in the sentence:
What it means
modeInSpan computes the modal value of a sentence property over a given Span. Before iterating, it verifies the span lies within [0, sentence.length()) using Span.contains(); if the span extends beyond the sentence it throws IllegalArgumentException. This guards against out-of-bounds access when indexing into the selector's returned list.
Solutions
- Verify the span is within 0..sentence.length() before calling: Span.fromValues(0, sentence.length()).contains(span)
- Clamp or recompute the span against the current sentence length
- Ensure the span was created from the same Sentence/SentenceAlgorithms instance the call targets
Example fix
// before Span span = Span.fromValues(0, 15); algs.modeInSpan(span, Sentence::posTags); // throws if sentence has 10 tokens // after Span span = Span.fromValues(0, Math.min(15, algs.sentence.length())); algs.modeInSpan(span, Sentence::posTags);
Defensive patterns
Strategy: validation
Validate before calling
if (span.start() < 0 || span.end() > algs.sentence.length()) {
throw new IllegalArgumentException("Span out of bounds for sentence of length " + algs.sentence.length());
} Type guard
boolean isValidSpan(Span span, int sentenceLength) {
return span.start() >= 0 && span.end() <= sentenceLength;
} Try / catch
try {
E mode = algs.modeInSpan(span, Sentence::posTags);
} catch (IllegalArgumentException e) {
// recompute or clamp span
} Prevention
- Always derive spans from the same Sentence instance passed to the algorithm
- Remember spans are end-exclusive like Span.fromValues(start, end)
- Recompute spans when switching sentences or documents
When it happens
Trigger: Calling sentenceAlgorithms.modeInSpan(span, selector) with a Span whose end index exceeds sentence.length(), a negative start, or a span built for a different (longer) sentence.
Common situations: Reusing span indices computed from a previous sentence or a longer document; off-by-one errors building spans with an inclusive end index; running the same algorithm over multiple sentences without recomputing spans.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- conditionalLogProbGivenPrevious requires given one less…
- conditionalLogProbsGivenPrevious requires given one less…
- conditionalLogProbGivenFirst requires of one less than…
- unnormalizedConditionalLogProbGivenFirst requires of one…
- conditionalLogProbGivenNext requires given one less than…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/c942f589d2253e6e.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/simple/SentenceAlgorithms.java:337
/** @see SentenceAlgorithms#allSpans(Function, int) */
public Iterable<List<String>> allSpans() {
return allSpans(Sentence::words, sentence.length());
}
/**
* Select the most common element of the given type in the given span.
* This is useful for, e.g., finding the most likely NER span of a given span, or the most
* likely POS tag of a given span.
* Null entries are removed.
*
* @param span The span of the sentence to find the mode element in. This must be entirely contained in the sentence.
* @param selector The property of the sentence we are getting the mode of. For example, <code>Sentence::posTags</code>
* @param <E> The type of the element we are getting.
* @return The most common element of the given property in the sentence.
*/
public <E> E modeInSpan(Span span, Function<Sentence, List<E>> selector) {
if (!Span.fromValues(0, sentence.length()).contains(span)) {
throw new IllegalArgumentException("Span must be entirely contained in the sentence: " + span + " (sentence length=" + sentence.length() + ")");
}
Counter<E> candidates = new ClassicCounter<>();
for (int i : span) {
candidates.incrementCount(selector.apply(sentence).get(i));
}
candidates.remove(null);
return Counters.argmax(candidates);
}
/**
* Run a proper BFS over a dependency graph, finding the shortest path between two vertices.
*
* @param start The start index.
* @param end The end index.
* @param selector The selector to use for the word nodes.
*
* @return A path string, analogous to {@link #dependencyPathBetween(int, int)}View on GitHub (pinned to 1b7edd19c4)