{"record":{"id":"6b39d5fba267f34c","repo":"stanfordnlp/CoreNLP","slug":"tree-had-more-leaves-than-the-labels-provided","errorCode":null,"errorMessage":"Tree had more leaves than the labels provided","messagePattern":"Tree had more leaves than the labels provided","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Trees.java","lineNumber":286,"sourceCode":"        setLeafTagsIfUnset(child);\n      }\n    }\n  }\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) {","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Trees.java#L268-L304","documentation":"Thrown by Trees.setLeafLabels when the tree being relabeled has more leaf nodes than the supplied labels list. The method walks the leaf iterator and label iterator in lockstep; once labels run out but leaves remain, it raises this IllegalArgumentException to signal a size mismatch between the tree and the label collection.","triggerScenarios":"Calling Trees.setLeafLabels(tree, labels) where tree.yield().size() > labels.size(), e.g. after attaching/extracting a subtree with extra leaves or passing a partially built label list.","commonSituations":"Mismatches after punctuation stripping or node pruning changed the tree's yield; passing labels for a tokenized sentence while the tree still contains POS-only leaves; off-by-one from filtering null labels out of the list.","solutions":["Count the tree's leaves with tree.yield().size() and confirm it equals labels.size() before calling setLeafLabels","Regenerate the label list from the same tree version used for relabeling (re-tokenize or re-extract after any tree edits)","If labels may be fewer, decide on a policy: pad the list or use a relabeling loop keyed on leaf index instead"],"exampleFix":"// before\nTrees.setLeafLabels(tree, wordLabels);\n// after\nList<Label> leaves = tree.yield();\nif (leaves.size() != wordLabels.size()) {\n  throw new IllegalStateException(\"expected \" + leaves.size() + \" labels, got \" + wordLabels.size());\n}\nTrees.setLeafLabels(tree, wordLabels);","handlingStrategy":"validation","validationCode":"if (tree.yield().size() != labels.size()) {\n  throw new IllegalArgumentException(\"leaf/label count mismatch: \" + tree.yield().size() + \" vs \" + labels.size());\n}\nTrees.setLeafLabels(tree, labels);","typeGuard":"boolean isRelabelable(Tree t, List<Label> ls) {\n  return t != null && ls != null && t.yield().size() == ls.size();\n}","tryCatchPattern":"try {\n  Trees.setLeafLabels(tree, labels);\n} catch (IllegalArgumentException e) {\n  logger.warn(\"leaf/label mismatch: {}\", e.getMessage());\n}","preventionTips":["Always derive labels from the same tokenization/pipeline as the tree","Assert yield size equals label size in tests","Re-derive labels after any tree transformation"],"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"}