stanfordnlp/CoreNLP · error · UnsupportedOperationException
Empty label not supported
Error message
Empty label not supported
What it means
ProtobufAnnotationSerializer.toFlattenedTree throws UnsupportedOperationException when the Tree being flattened to a CoreNLPProtos.FlattenedParseTree has a null label. The flattened tree format requires every node to carry a label value, so label-less trees cannot be serialized.
Solutions
- Ensure every tree node has a label before serializing, e.g. wrap nodes with a CoreLabel via tree.setLabel(new CoreLabel(...)).
- Give unlabeled intermediate nodes an empty-string label instead of null.
- Skip or preprocess trees known to contain unlabeled nodes before calling toFlattenedTree.
Example fix
// before
serializer.toFlattenedTree(unlabeledTree, builder); // throws
// after
if (unlabeledTree.label() == null) {
unlabeledTree.setLabel(new CoreLabel("")); // or a placeholder category
}
serializer.toFlattenedTree(unlabeledTree, builder); Defensive patterns
Strategy: validation
Validate before calling
function hasLabels(Tree t) {
if (t.label() == null) return false;
for (Tree c : t.children()) if (!hasLabels(c)) return false;
return true;
}
if (!hasLabels(parseTree)) throw new IllegalArgumentException("tree contains unlabeled nodes"); Type guard
boolean isLabeled(Tree t) { return t != null && t.label() != null; } Try / catch
try {
serializer.toFlattenedTree(tree, builder);
} catch (UnsupportedOperationException e) {
log.warn("Skipping unlabeled tree: " + e.getMessage());
} Prevention
- Always attach a CoreLabel when constructing tree nodes
- Assert label() != null in tree-building code before serialization
- Prefer parsers/annotators that produce labeled constituents
When it happens
Trigger: Calling toFlattenedTree(tree, treeBuilder) on a LabeledScoredTree/parse tree whose label() returns null — typically a tree node created without a CoreLabel or an intermediate node in a custom grammar output.
Common situations: Serializing constituency parses from annotators or custom parsers that build unlabeled nodes; hand-constructed trees in tests passed into the protobuf serializer.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- Tree continued after it was already closed! Offending proto
- Tree added a child before a label was added to a node! …
- Tree started with a Close, not an Open! Offending proto
- Tree started with a label, not an Open! Offending proto
- Tree never finished! Offending proto
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/97866c976ad3bf5b.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java:2139
if (entityMentionForCorefMentionIndex != -1) {
ann.get(CorefMentionToEntityMentionMappingAnnotation.class).put(
corefMentionIndex, entityMentionForCorefMentionIndex);
}
corefMentionIndex++;
}
}
// Return
return ann;
}
public static void toFlattenedTree(Tree tree, CoreNLPProtos.FlattenedParseTree.Builder treeBuilder) {
CoreNLPProtos.FlattenedParseTree.Node.Builder nodeBuilder = CoreNLPProtos.FlattenedParseTree.Node.newBuilder();
nodeBuilder.setOpenNode(true);
treeBuilder.addNodes(nodeBuilder.build());
if (tree.label() == null) {
throw new UnsupportedOperationException("Empty label not supported");
}
nodeBuilder = CoreNLPProtos.FlattenedParseTree.Node.newBuilder();
nodeBuilder.setValue(tree.label().value());
if (!Double.isNaN(tree.score())) {
nodeBuilder.setScore(tree.score());
}
treeBuilder.addNodes(nodeBuilder.build());
for (Tree child : tree.children()) {
if (child.numChildren() == 0) {
nodeBuilder = CoreNLPProtos.FlattenedParseTree.Node.newBuilder();
nodeBuilder.setValue(child.label().value());
if (!Double.isNaN(child.score())) {
nodeBuilder.setScore(child.score());
}
treeBuilder.addNodes(nodeBuilder.build());
} else {View on GitHub (pinned to 1b7edd19c4)