stanfordnlp/CoreNLP · error · IllegalArgumentException
Expected leaves to be CoreLabels
Error message
Expected leaves to be CoreLabels
What it means
Tree.spanString() reconstructs the original text covered by the tree from its leaves, which must be CoreLabels carrying word and after() (following whitespace) data. If the first leaf's label is not a CoreLabel, the method cannot read the needed fields and throws IllegalArgumentException immediately.
Solutions
- Rebuild the tree with CoreLabel leaves (CoreLabel-based TreeFactory) so word()/after() are populated
- Convert leaf labels to CoreLabels copying word and after values before calling spanString()
- Read trees via a TreeReader configured to produce CoreLabel leaves
Example fix
// before String s = stringLabeledTree.spanString(); // after CoreLabel cl = new CoreLabel(); cl.setWord(t.label().value()); leaf.setLabel(cl); String s = tree.spanString();
Defensive patterns
Strategy: type-guard
Validate before calling
if (!(tree.getLeaves().get(0).label() instanceof CoreLabel)) throw new IllegalStateException("leaves are not CoreLabels; cannot call spanString"); Type guard
boolean hasCoreLabelLeaves(Tree t) { return !t.getLeaves().isEmpty() && t.getLeaves().get(0).label() instanceof CoreLabel; } Try / catch
try { return tree.spanString(); } catch (IllegalArgumentException e) { return joinLeafValues(tree); } Prevention
- Ensure tree leaves are CoreLabel with populated word/after
- Validate leaf label type before text-extraction calls
- Standardize on CoreLabel factories across the pipeline
When it happens
Trigger: Calling spanString() on a tree whose leaf labels are not CoreLabel (e.g. StringLabel/WordLabel trees loaded from a reader or built with a non-CoreLabel factory).
Common situations: Trees parsed from PTB files with default label factories, trees returned from third-party parsers, or trees built in unit tests with simple labels, then trying to extract surface text via spanString().
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Expected CoreLabel's to have text
- 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/1abf4b0b502d8a42.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/Tree.java:1019
*/
public String pennString() {
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 theView on GitHub (pinned to 1b7edd19c4)