stanfordnlp/CoreNLP · error · UnsupportedOperationException
Can only set spans on trees which use CoreLabel
Error message
Can only set spans on trees which use CoreLabel
What it means
Tree.setSpans() annotates every node with its token span via CoreAnnotations.SpanAnnotation, which requires each node's label to be a CoreLabel. In constituentsNodes (leaf case) a label that is not a CoreLabel cannot hold the annotation, so the library throws UnsupportedOperationException.
Solutions
- Rebuild or re-label the tree using CoreLabel instances: use a TreeFactory with CoreLabelFactory / LabeledScoredTreeFactory(CoreLabelFactory.instance()) so all labels are CoreLabels
- Convert labels via setLabel(new CoreLabel(...)) for every node, copying the value into the CoreLabel, before calling setSpans
- Read the tree with TreeReader that specifies CoreLabel as the label type, then call setSpans
Example fix
// before: StringLabel leaves TreeFactory tf = new TreeFactory(StringLabel.factory()); tree.setSpans(); // throws // after TreeFactory tf = new TreeFactory(CoreLabel.factory()); tree.setSpans(); // OK
Defensive patterns
Strategy: type-guard
Validate before calling
boolean allCoreLabels = true; for (Tree t : tree) { if (!(t.label() instanceof CoreLabel)) { allCoreLabels = false; break; } } Type guard
boolean canSetSpans(Tree t) { return t.getLeaves().stream().allMatch(l -> l.label() instanceof CoreLabel); } Try / catch
try { tree.setSpans(); } catch (UnsupportedOperationException e) { tree = relabelWithCoreLabels(tree); tree.setSpans(); } Prevention
- Read trees with CoreLabel-producing TreeReaders
- Convert labels at tree-construction boundaries, not at annotation time
- Assert all labels are CoreLabel in a pipeline entry point
When it happens
Trigger: Calling tree.setSpans() on a tree whose leaf labels are not CoreLabel instances (e.g. StringLabel, WordTag, IndexedWord), which reaches the leaf branch of constituentsNodes.
Common situations: Parsing trees from string formats with a default (non-CoreLabel) TreeReader/label factory, then calling span-dependent methods; mixing trees built with TreeFactory(StringLabel) into CoreNLP annotation pipelines.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Cannot set from string
- : Does not support parse operation.
- CORE: CoreLabel.initFromStrings: Can't handle
- CORE: CoreLabel.initFromStrings: Bad type for…
- This code branch left blank because we do not understand…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/55ef910f13840fc1.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/Tree.java:552
public Set<Constituent> constituents(ConstituentFactory cf, boolean charLevel, Predicate<Tree> filter) {
Set<Constituent> constituentsSet = Generics.newHashSet();
constituents(constituentsSet, 0, cf, charLevel, filter, -1, 0);
return constituentsSet;
}
/**
* Same as int constituents but just puts the span as an IntPair
* in the CoreLabel of the nodes.
*
* @param left The left position to begin labeling from
* @return The index of the right frontier of the constituent
*/
private int constituentsNodes(int left) {
if (isLeaf()) {
if (label() instanceof CoreLabel) {
((CoreLabel) label()).set(CoreAnnotations.SpanAnnotation.class, new IntPair(left, left));
} else {
throw new UnsupportedOperationException("Can only set spans on trees which use CoreLabel");
}
return (left + 1);
}
int position = left;
// enumerate through daughter trees
Tree[] kids = children();
for (Tree kid : kids)
position = kid.constituentsNodes(position);
//Parent span
if (label() instanceof CoreLabel) {
((CoreLabel) label()).set(CoreAnnotations.SpanAnnotation.class, new IntPair(left, position - 1));
} else {
throw new UnsupportedOperationException("Can only set spans on trees which use CoreLabel");
}
return position;View on GitHub (pinned to 1b7edd19c4)