stanfordnlp/CoreNLP · error · IllegalArgumentException
Only works on CoreLabel
Error message
Only works on CoreLabel
What it means
Trees.setSentIndex only operates on trees whose leaves are CoreLabel instances, because the sentence index annotation lives on CoreLabel. If any leaf implements Label but not CoreLabel, it throws IllegalArgumentException immediately.
Solutions
- Build or re-read the tree with a CoreLabel-producing factory such as LabeledScoredTreeFactory(CoreLabel.factory())
- Convert existing leaves to CoreLabel by rebuilding the tree with TreeCoreAnnotations or a transform that replaces labels
- Set sentIndex directly by iterating leaves and casting only after instanceof CoreLabel check
Example fix
// before Tree t = Trees.readTree(ptbStr); // leaves may be StringLabel Trees.setSentIndex(t, 5); // throws // after Tree t = Trees.readTree(ptbStr, new LabeledScoredTreeFactory(CoreLabel.factory())); Trees.setSentIndex(t, 5);
Defensive patterns
Strategy: type-guard
Validate before calling
for (Label leaf : tree.yield()) {
if (!(leaf instanceof CoreLabel)) {
throw new IllegalArgumentException("leaf " + leaf + " is not a CoreLabel");
}
} Type guard
boolean hasCoreLeaves(Tree t) {
return t.yield().stream().allMatch(l -> l instanceof CoreLabel);
} Try / catch
try {
Trees.setSentIndex(tree, sentIndex);
} catch (IllegalArgumentException e) {
tree = rebuildWithCoreLabels(tree);
Trees.setSentIndex(tree, sentIndex);
} Prevention
- Build trees with LabeledScoredTreeFactory(CoreLabel.factory()) when CoreNLP annotations will be applied
- Check leaf label types before annotation utilities
- Standardize on CoreLabel across the pipeline
When it happens
Trigger: Calling Trees.setSentIndex(tree, i) on a tree built with a factory that produces non-CoreLabel leaves, e.g. LabeledScoredTreeFactory with StringLabel or Word leaves.
Common situations: Mixing tree factories: reading a tree with default (StringLabel-based) factories then attempting CoreNLP annotations; converting between treebanks that use different Label types.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Cannot set from string
- CoreLabels required!
- Label argument lacks IndexAnnotation.
- No label for '" + text + "'
- Only operates on CoreLabels
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/4ff3ab4fd2912804.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/Trees.java:897
cl.setValue(l.value());
tree.setLabel(cl);
}
for (Tree kid : tree.children()) {
convertToCoreLabels(kid);
}
}
/**
* Set the sentence index of all the leaves in the tree
* (only works on CoreLabel)
*/
public static void setSentIndex(Tree tree, int sentIndex) {
List<Label> leaves = tree.yield();
for (Label leaf : leaves) {
if (!(leaf instanceof CoreLabel)) {
throw new IllegalArgumentException("Only works on CoreLabel");
}
((CoreLabel) leaf).setSentIndex(sentIndex);
}
}
}
View on GitHub (pinned to 1b7edd19c4)