stanfordnlp/CoreNLP · error · IllegalArgumentException

Tree had more leaves than the labels provided

Error message

Tree had more leaves than the labels provided

What it means

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.

Solutions

  1. Count the tree's leaves with tree.yield().size() and confirm it equals labels.size() before calling setLeafLabels
  2. Regenerate the label list from the same tree version used for relabeling (re-tokenize or re-extract after any tree edits)
  3. If labels may be fewer, decide on a policy: pad the list or use a relabeling loop keyed on leaf index instead

Example fix

// before
Trees.setLeafLabels(tree, wordLabels);
// after
List<Label> leaves = tree.yield();
if (leaves.size() != wordLabels.size()) {
  throw new IllegalStateException("expected " + leaves.size() + " labels, got " + wordLabels.size());
}
Trees.setLeafLabels(tree, wordLabels);
Defensive patterns

Strategy: validation

Validate before calling

if (tree.yield().size() != labels.size()) {
  throw new IllegalArgumentException("leaf/label count mismatch: " + tree.yield().size() + " vs " + labels.size());
}
Trees.setLeafLabels(tree, labels);

Type guard

boolean isRelabelable(Tree t, List<Label> ls) {
  return t != null && ls != null && t.yield().size() == ls.size();
}

Try / catch

try {
  Trees.setLeafLabels(tree, labels);
} catch (IllegalArgumentException e) {
  logger.warn("leaf/label mismatch: {}", e.getMessage());
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/6b39d5fba267f34c. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/trees/Trees.java:286

        setLeafTagsIfUnset(child);
      }
    }
  }

  /**
   * Replace the labels of the leaves with the given leaves.
   */
  public static void setLeafLabels(Tree tree, List<? extends Label> labels) {
    Iterator<Tree> leafIterator = tree.getLeaves().iterator();
    Iterator<? extends Label> labelIterator = labels.iterator();
    while (leafIterator.hasNext() && labelIterator.hasNext()) {
      Tree leaf = leafIterator.next();
      Label label = labelIterator.next();
      leaf.setLabel(label);
      //leafIterator.next().setLabel(labelIterator.next());
    }
    if (leafIterator.hasNext()) {
      throw new IllegalArgumentException("Tree had more leaves than the labels provided");
    }
    if (labelIterator.hasNext()) {
      throw new IllegalArgumentException("More labels provided than tree had leaves");
    }
  }


  /**
   * returns the maximal projection of {@code head} in
   * {@code root} given a {@link HeadFinder}
   */
  public static Tree maximalProjection(Tree head, Tree root, HeadFinder hf) {
    Tree projection = head;
    if (projection == root) {
      return root;
    }
    Tree parent = projection.parent(root);
    while (hf.determineHead(parent) == projection) {

View on GitHub (pinned to 1b7edd19c4)