stanfordnlp/CoreNLP · error · IllegalArgumentException
CoreLabels required!
Error message
CoreLabels required!
What it means
SentimentUtils.attachLabels assigns a numeric gold sentiment class to each tree node via CoreLabel.set(annotationClass, value). The node label must be a CoreLabel instance; if it is any other Label implementation the method throws IllegalArgumentException('CoreLabels required!').
Solutions
- Read the trees with a CoreLabel factory, e.g. Tree.valueOf with a CoreLabel factory or use PennTreeReader with a CoreLabelTreeNormalizer / LabeledScoredTreeReader(new CoreLabel().labelFactory()).
- Convert trees before calling attachLabels: replace each label with a CoreLabel copying the value.
- If constructing trees programmatically, create nodes with new CoreLabel() rather than StringLabel/Word.
Example fix
// before
Tree tree = Tree.valueOf("(3 (2 It) (4 was))"); // StringLabel nodes
List<Tree> trees = SentimentUtils.readTreesWithLabels(file);
// after
Tree tree = Tree.valueOf("(3 (2 It) (4 was))", new CoreLabel().labelFactory());
List<Tree> trees = SentimentUtils.readTreesWithLabels(file, new NumberRangesFileScanner(...)); Defensive patterns
Strategy: type-guard
Validate before calling
// verify all node labels are CoreLabels before attaching
for (Tree t : trees)
for (Tree node : t)
if (!(node.label() instanceof CoreLabel)) throw new IllegalArgumentException("non-CoreLabel at " + node); Type guard
boolean hasOnlyCoreLabels(Tree t) {
return Trees.getLeaves(t).stream().allMatch(n -> n.label() instanceof CoreLabel)
&& (t.label() == null || t.label() instanceof CoreLabel);
} Prevention
- Read trees with a CoreLabel label factory.
- Never mix StringLabel/Word-based trees with sentiment code.
- Convert labels once at load time, not at use time.
When it happens
Trigger: Calling attachLabels (directly or via readTreesWithLabels) on trees whose node labels are not CoreLabel — e.g. trees read with a different Label factory, or a tree whose label().value() parses as an int but whose Label is a StringLabel/Word, at SentimentUtils.java:40.
Common situations: Loading custom tree files with Tree.valueOf() default label factory instead of CoreLabel factory, converting trees from another parser output, or mixing LabeledScoredTreeReader output with sentiment code.
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
- Only operates on CoreLabels
- addFeature was called with a features object that is…
- Attempting to remove features based on weight from a…
- Cannot cast " + classname + " into " + type.getName()
- Cannot get max of attribute " + key + ", object of type: "…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/cf8a857be4f75b26.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/sentiment/SentimentUtils.java:40
* @author John Bauer
*/
public class SentimentUtils {
private SentimentUtils() {
} // static methods only
public static void attachLabels(Tree tree, Class<? extends CoreAnnotation<Integer>> annotationClass) {
if (tree.isLeaf()) {
return;
}
for (Tree child : tree.children()) {
attachLabels(child, annotationClass);
}
// In the sentiment data set, the node labels are simply the gold
// class labels. There are no categories encoded.
int numericLabel = Integer.valueOf(tree.label().value());
Label label = tree.label();
if (!(label instanceof CoreLabel)) {
throw new IllegalArgumentException("CoreLabels required!");
}
((CoreLabel) label).set(annotationClass, numericLabel);
}
/**
* Given a file name, reads in those trees and returns them as a List
*/
public static List<Tree> readTreesWithGoldLabels(String path) {
return readTreesWithLabels(path, RNNCoreAnnotations.GoldClass.class);
}
/**
* Given a file name, reads in those trees and returns them as list with
* labels attached as predictions
*/
public static List<Tree> readTreesWithPredictedLabels(String path) {
return readTreesWithLabels(path, RNNCoreAnnotations.PredictedClass.class);View on GitHub (pinned to 1b7edd19c4)