stanfordnlp/CoreNLP · error · IllegalArgumentException

More labels provided than tree had leaves

Error message

More labels provided than tree had leaves

What it means

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.

Solutions

  1. Compare labels.size() to tree.yield().size() and trim the label list before the call
  2. Ensure both labels and tree derive from the same preprocessing pipeline
  3. If extra labels are expected, slice with labels.subList(0, tree.yield().size()) only after verifying the prefix corresponds

Example fix

// before
Trees.setLeafLabels(tree, allTokenLabels);
// after
List<Label> leaves = tree.yield();
if (allTokenLabels.size() > leaves.size()) {
  allTokenLabels = allTokenLabels.subList(0, leaves.size());
}
Trees.setLeafLabels(tree, allTokenLabels);
Defensive patterns

Strategy: validation

Validate before calling

if (labels.size() > tree.yield().size()) {
  labels = labels.subList(0, tree.yield().size());
}
Trees.setLeafLabels(tree, labels);

Type guard

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

Try / catch

try {
  Trees.setLeafLabels(tree, labels);
} catch (IllegalArgumentException e) {
  logger.warn("too many labels: {}", e.getMessage());
}

Prevention

When it happens

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

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

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/8257fa9c053d9a0c. Report an issue: GitHub.

Appendix: source

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

  }

  /**
   * 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) {
      projection = parent;
      if (projection == root) {
        return root;

View on GitHub (pinned to 1b7edd19c4)