{"record":{"id":"97866c976ad3bf5b","repo":"stanfordnlp/CoreNLP","slug":"empty-label-not-supported","errorCode":null,"errorMessage":"Empty label not supported","messagePattern":"Empty label not supported","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java","lineNumber":2139,"sourceCode":"        if (entityMentionForCorefMentionIndex != -1) {\n          ann.get(CorefMentionToEntityMentionMappingAnnotation.class).put(\n              corefMentionIndex, entityMentionForCorefMentionIndex);\n        }\n        corefMentionIndex++;\n      }\n    }\n\n    // Return\n    return ann;\n  }\n\n  public static void toFlattenedTree(Tree tree, CoreNLPProtos.FlattenedParseTree.Builder treeBuilder) {\n    CoreNLPProtos.FlattenedParseTree.Node.Builder nodeBuilder = CoreNLPProtos.FlattenedParseTree.Node.newBuilder();\n    nodeBuilder.setOpenNode(true);\n    treeBuilder.addNodes(nodeBuilder.build());\n\n    if (tree.label() == null) {\n      throw new UnsupportedOperationException(\"Empty label not supported\");\n    }\n\n    nodeBuilder = CoreNLPProtos.FlattenedParseTree.Node.newBuilder();\n    nodeBuilder.setValue(tree.label().value());\n    if (!Double.isNaN(tree.score())) {\n      nodeBuilder.setScore(tree.score());\n    }\n    treeBuilder.addNodes(nodeBuilder.build());\n\n    for (Tree child : tree.children()) {\n      if (child.numChildren() == 0) {\n        nodeBuilder = CoreNLPProtos.FlattenedParseTree.Node.newBuilder();\n        nodeBuilder.setValue(child.label().value());\n        if (!Double.isNaN(child.score())) {\n          nodeBuilder.setScore(child.score());\n        }\n        treeBuilder.addNodes(nodeBuilder.build());\n      } else {","sourceCodeStart":2121,"sourceCodeEnd":2157,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java#L2121-L2157","documentation":"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.","triggerScenarios":"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.","commonSituations":"Serializing constituency parses from annotators or custom parsers that build unlabeled nodes; hand-constructed trees in tests passed into the protobuf serializer.","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."],"exampleFix":"// before\nserializer.toFlattenedTree(unlabeledTree, builder); // throws\n// after\nif (unlabeledTree.label() == null) {\n  unlabeledTree.setLabel(new CoreLabel(\"\")); // or a placeholder category\n}\nserializer.toFlattenedTree(unlabeledTree, builder);","handlingStrategy":"validation","validationCode":"function hasLabels(Tree t) {\n  if (t.label() == null) return false;\n  for (Tree c : t.children()) if (!hasLabels(c)) return false;\n  return true;\n}\nif (!hasLabels(parseTree)) throw new IllegalArgumentException(\"tree contains unlabeled nodes\");","typeGuard":"boolean isLabeled(Tree t) { return t != null && t.label() != null; }","tryCatchPattern":"try {\n  serializer.toFlattenedTree(tree, builder);\n} catch (UnsupportedOperationException e) {\n  log.warn(\"Skipping unlabeled tree: \" + e.getMessage());\n}","preventionTips":["Always attach a CoreLabel when constructing tree nodes","Assert label() != null in tree-building code before serialization","Prefer parsers/annotators that produce labeled constituents"],"tags":["serialization","protobuf","tree","null-label"],"backgroundTag":"empty-required-field","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}