stanfordnlp/CoreNLP · error · IllegalArgumentException
Expected CoreLabel's to have text
Error message
Expected CoreLabel's to have text
What it means
spanString() needs each leaf's CoreLabel.word() to hold the token text; without it the surface form cannot be reconstructed. When the first leaf is a CoreLabel but its word field is null, the library throws this IllegalArgumentException.
Solutions
- Set the word on each leaf: ((CoreLabel) leaf.label()).setWord(tokenText) before calling spanString()
- Run trees through LexedTokenFactory/tokenization that populates WordAnnotation, or re-tokenize with CoreNLP tokenize step
- Use setWord/setValue consistently when constructing CoreLabels manually
Example fix
// before
CoreLabel cl = new CoreLabel(); cl.setTag("NN");
leaf.setLabel(cl); tree.spanString(); // throws
// after
cl.setWord("dog"); leaf.setLabel(cl); tree.spanString(); Defensive patterns
Strategy: type-guard
Validate before calling
CoreLabel first = (CoreLabel) tree.getLeaves().get(0).label(); if (first.word() == null) throw new IllegalStateException("leaf CoreLabels missing word"); Type guard
boolean leavesHaveWords(Tree t) { return t.getLeaves().stream().allMatch(l -> l.label() instanceof CoreLabel && ((CoreLabel) l.label()).word() != null); } Try / catch
try { return tree.spanString(); } catch (IllegalArgumentException e) { leaves.forEach(l -> ((CoreLabel) l.label()).setWord(l.value())); return tree.spanString(); } Prevention
- Always setWord() when constructing leaf CoreLabels manually
- Tokenize with CoreNLP tokenizers that populate WordAnnotation
- Unit-test tree construction with a spanString() smoke check
When it happens
Trigger: Calling spanString() on a tree whose leaves are CoreLabels that were constructed without setting the word (e.g. only tag/value set, or labels created by factories that never populate WordAnnotation).
Common situations: Programmatically built trees where CoreLabels were created empty and only value() was set; tokenization done outside CoreNLP so WordAnnotation was never applied; trees whose leaves carry only part-of-speech info.
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
- Expected leaves to be CoreLabels
- Expected CoreLabel's to have after() text
- Expected CoreLabels in the trees
- CORE: CoreLabel.initFromStrings: Can't handle
- CORE: CoreLabel.initFromStrings: Bad type for…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/6a9fa1c068c7442f.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/Tree.java:1021
StringWriter sw = new StringWriter();
pennPrint(new PrintWriter(sw));
return sw.toString();
}
/**
* Return String of leaves spanned by this tree assuming they are CoreLabel's
* Throws an IllegalArgumentException if the leaves are not CoreLabels that contain
* text info as in the typical use case of a Tree generated by a pipeline
*
* @return The text of the span of this Tree
*/
public String spanString() {
// check this Tree supports this method by having properly populated CoreLabel's
List<Tree> leaves = this.getLeaves();
if (!(leaves.get(0).label() instanceof CoreLabel)) {
throw new IllegalArgumentException("Expected leaves to be CoreLabels");
} else if (((CoreLabel) leaves.get(0).label()).word() == null) {
throw new IllegalArgumentException("Expected CoreLabel's to have text");
} else if (((CoreLabel) leaves.get(0).label()).after() == null) {
throw new IllegalArgumentException("Expected CoreLabel's to have after() text");
}
List<CoreLabel> coreLabels = this.getLeaves().stream().map(l -> ((CoreLabel) l.label())).collect(Collectors.toList());
// reconstruct original String from CoreLabel fields
String spanString = coreLabels.subList(0, Math.max(0, coreLabels.size()-1)).stream().map(
cl -> cl.word()+cl.after()).collect(Collectors.joining(""));
// don't add the after of the last word
spanString += coreLabels.get(coreLabels.size()-1).word();
return spanString;
}
/**
* Print the tree as done in Penn Treebank merged files.
* The formatting should be exactly the same, but we don't print the
* trailing whitespace found in Penn Treebank trees.
* The tree is printed to {@code System.out}. The basic deviationView on GitHub (pinned to 1b7edd19c4)