{"record":{"id":"8257fa9c053d9a0c","repo":"stanfordnlp/CoreNLP","slug":"more-labels-provided-than-tree-had-leaves","errorCode":null,"errorMessage":"More labels provided than tree had leaves","messagePattern":"More labels provided than tree had leaves","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Trees.java","lineNumber":289,"sourceCode":"  }\n\n  /**\n   * Replace the labels of the leaves with the given leaves.\n   */\n  public static void setLeafLabels(Tree tree, List<? extends Label> labels) {\n    Iterator<Tree> leafIterator = tree.getLeaves().iterator();\n    Iterator<? extends Label> labelIterator = labels.iterator();\n    while (leafIterator.hasNext() && labelIterator.hasNext()) {\n      Tree leaf = leafIterator.next();\n      Label label = labelIterator.next();\n      leaf.setLabel(label);\n      //leafIterator.next().setLabel(labelIterator.next());\n    }\n    if (leafIterator.hasNext()) {\n      throw new IllegalArgumentException(\"Tree had more leaves than the labels provided\");\n    }\n    if (labelIterator.hasNext()) {\n      throw new IllegalArgumentException(\"More labels provided than tree had leaves\");\n    }\n  }\n\n\n  /**\n   * returns the maximal projection of {@code head} in\n   * {@code root} given a {@link HeadFinder}\n   */\n  public static Tree maximalProjection(Tree head, Tree root, HeadFinder hf) {\n    Tree projection = head;\n    if (projection == root) {\n      return root;\n    }\n    Tree parent = projection.parent(root);\n    while (hf.determineHead(parent) == projection) {\n      projection = parent;\n      if (projection == root) {\n        return root;","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Trees.java#L271-L307","documentation":"Thrown by Trees.setLeafLabels when more labels are supplied than the tree has leaves. After all leaves have been relabeled, any leftover labels indicate the caller passed a list longer than the tree's yield, so the method rejects it rather than silently dropping labels.","triggerScenarios":"Calling Trees.setLeafLabels(tree, labels) where labels.size() > tree.yield().size(), e.g. including labels for punctuation or empty categories that were removed from the tree.","commonSituations":"Labels produced from raw tokenized text while the tree was already pruned; reusing a label list from a different sentence; forgetting that treebank trees may have been transformed (e.g. -NONE- nodes deleted).","solutions":["Compare labels.size() to tree.yield().size() and trim the label list before the call","Ensure both labels and tree derive from the same preprocessing pipeline","If extra labels are expected, slice with labels.subList(0, tree.yield().size()) only after verifying the prefix corresponds"],"exampleFix":"// before\nTrees.setLeafLabels(tree, allTokenLabels);\n// after\nList<Label> leaves = tree.yield();\nif (allTokenLabels.size() > leaves.size()) {\n  allTokenLabels = allTokenLabels.subList(0, leaves.size());\n}\nTrees.setLeafLabels(tree, allTokenLabels);","handlingStrategy":"validation","validationCode":"if (labels.size() > tree.yield().size()) {\n  labels = labels.subList(0, tree.yield().size());\n}\nTrees.setLeafLabels(tree, labels);","typeGuard":"boolean labelsFitTree(Tree t, List<Label> ls) {\n  return t != null && ls != null && ls.size() <= t.yield().size();\n}","tryCatchPattern":"try {\n  Trees.setLeafLabels(tree, labels);\n} catch (IllegalArgumentException e) {\n  logger.warn(\"too many labels: {}\", e.getMessage());\n}","preventionTips":["Trim label lists to the tree's yield before relabeling","Keep tokenization and tree pruning in lockstep","Unit-test relabeling with punctuation-containing sentences"],"tags":["java","trees","argument-mismatch"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}