stanfordnlp/CoreNLP · error · IllegalArgumentException
Expected tree labels to be CoreLabel
Error message
Expected tree labels to be CoreLabel
What it means
CreateTransitionSequence.createTransitionSequenceHelper() derives the head words of a binarized tree node and its two children to emit BinaryTransitions; it requires all three labels to be CoreLabel instances. If any label is another Label implementation, it throws this IllegalArgumentException, because only CoreLabel can carry the HeadWordLabelAnnotation it reads next.
Solutions
- Preprocess trees with the parser's TreeBinarizer/CoreLabel conversion (as ShiftReduceParser.train does) before generating transition sequences.
- Ensure the tree is binarized so children()[0] and children()[1] exist.
- Use Trees.toTree / the same TreebankLangParserParams pipeline that produced the training data.
- In unit tests, build trees with CoreLabelFactory-labeled nodes.
Example fix
// before List<Transition> trans = CreateTransitionSequence.createTransitionSequence(rawTree, false); // after Tree binarized = Binarizer.binarez(rawTree); // or run parser's preprocessing List<Transition> trans = CreateTransitionSequence.createTransitionSequence(binarized, false);
Defensive patterns
Strategy: validation
Validate before calling
boolean ok = tree.label() instanceof CoreLabel
&& tree.children().length >= 2
&& tree.children()[0].label() instanceof CoreLabel
&& tree.children()[1].label() instanceof CoreLabel;
if (!ok) throw new IllegalArgumentException("Tree must be binarized with CoreLabel nodes"); Type guard
boolean isBinarizedCoreLabelTree(Tree t) {
return t.label() instanceof CoreLabel
&& t.children().length == 2
&& t.children()[0].label() instanceof CoreLabel
&& t.children()[1].label() instanceof CoreLabel;
} Try / catch
try {
List<Transition> trans = CreateTransitionSequence.createTransitionSequence(tree, false);
} catch (IllegalArgumentException e) {
if (e.getMessage().equals("Expected tree labels to be CoreLabel")) {
// binarize + convert labels, then retry
}
} Prevention
- Binarize trees before generating transition sequences.
- Ensure the tree pipeline produces CoreLabel-backed nodes.
- Reuse the trainer's preprocessing rather than calling the helper directly.
When it happens
Trigger: Calling createTransitionSequence on trees not preprocessed into CoreLabel-backed binarized form — e.g. raw trees from a different parser or trees whose children array has fewer than 2 non-null children (children()[1] null).
Common situations: Training the shift-reduce parser with a custom treebank loaded without the parser's tree normalization, or passing non-binarized/unheaded trees directly to the transition-sequence generator.
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
- Expected a tree
- Stack should have CoreLabel nodes
- Expected tree labels to have their heads assigned. Failed…
- Required a tree with CoreLabels
- Only operates on CoreLabels
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/d7ec7ca8ea1a2dec.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/parser/shiftreduce/CreateTransitionSequence.java:75
createTransitionSequenceHelper(transitions, tree, compoundUnary, rootOnlyStates);
transitions.add(new CompoundUnaryTransition(labels, isRoot));
} else {
createTransitionSequenceHelper(transitions, tree.children()[0], compoundUnary, rootOnlyStates);
transitions.add(new UnaryTransition(tree.label().value(), isRoot));
}
} else if (tree.children().length == 2) {
createTransitionSequenceHelper(transitions, tree.children()[0], compoundUnary, rootOnlyStates);
createTransitionSequenceHelper(transitions, tree.children()[1], compoundUnary, rootOnlyStates);
// This is the tricky part... need to decide if the binary
// transition is a left or right transition. This is done by
// looking at the existing heads of this node and its two
// children. The expectation is that the tree already has heads
// assigned; otherwise, exception is thrown
if (!(tree.label() instanceof CoreLabel) ||
!(tree.children()[0].label() instanceof CoreLabel) ||
!(tree.children()[1].label() instanceof CoreLabel)) {
throw new IllegalArgumentException("Expected tree labels to be CoreLabel");
}
CoreLabel label = (CoreLabel) tree.label();
CoreLabel leftLabel = (CoreLabel) tree.children()[0].label();
CoreLabel rightLabel = (CoreLabel) tree.children()[1].label();
CoreLabel head = label.get(TreeCoreAnnotations.HeadWordLabelAnnotation.class);
CoreLabel leftHead = leftLabel.get(TreeCoreAnnotations.HeadWordLabelAnnotation.class);
CoreLabel rightHead = rightLabel.get(TreeCoreAnnotations.HeadWordLabelAnnotation.class);
if (head == null || leftHead == null || rightHead == null) {
throw new IllegalArgumentException("Expected tree labels to have their heads assigned. Failed at: " + tree);
}
boolean isRoot = rootOnlyStates.contains(tree.label().value());
if (head == leftHead) {
transitions.add(new BinaryTransition(tree.label().value(), BinaryTransition.Side.LEFT, isRoot));
} else if (head == rightHead) {
transitions.add(new BinaryTransition(tree.label().value(), BinaryTransition.Side.RIGHT, isRoot));
} else {
throw new IllegalArgumentException("Heads were incorrectly assigned: tree's head is not matched to either the right or left head");
}View on GitHub (pinned to 1b7edd19c4)