stanfordnlp/CoreNLP · error · IllegalArgumentException

Number of gold and predicted trees not equal!

Error message

Number of gold and predicted trees not equal!

What it means

ExternalEvaluate.populatePredictedLabels compares a list of gold trees against pre-supplied predicted trees and requires one predicted tree per gold tree. If the list sizes differ it throws IllegalArgumentException before per-tree label propagation begins.

Solutions

  1. Ensure both lists have the same number of trees (check sizes before calling)
  2. Regenerate predictions against the exact same tree list used as gold, applying identical filtering
  3. Check the prediction file for skipped/extra lines (blank lines, trailing newline handling)

Example fix

// before
List<Tree> gold = SentimentUtils.readTreesWithGoldLabels(treePath);
List<Tree> pred = readPredictions("pred.txt"); // e.g. 10 fewer lines
// after
assert gold.size() == pred.size();
List<Tree> pred = readPredictionsAligned("pred.txt", gold.size());
Defensive patterns

Strategy: validation

Validate before calling

if (goldTrees.size() != predictedTrees.size()) {
  throw new IllegalArgumentException("gold=" + goldTrees.size() + " predicted=" + predictedTrees.size());
}

Type guard

static boolean aligned(List<Tree> gold, List<Tree> pred) { return gold != null && pred != null && gold.size() == pred.size(); }

Try / catch

try {
  externalEval.populatePredictedLabels(goldTrees);
} catch (IllegalArgumentException e) {
  log.error("Gold/predicted count mismatch: " + e.getMessage());
}

Prevention

When it happens

Trigger: Constructing ExternalEvaluate (or calling populatePredictedLabels) with a predictedTrees list whose size differs from the number of gold trees passed in.

Common situations: Prediction file has fewer/more lines than the gold treebank (blank lines skipped inconsistently); filterUnknown applied to one list but not the other; concatenated prediction outputs misaligned.

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/79046da614495019. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/sentiment/ExternalEvaluate.java:31

 *
 * @author Michael Haas {@literal <haas@cl.uni-heidelberg.de>}
 */
public class ExternalEvaluate extends AbstractEvaluate  {

  /** A logger for this class */
  private static final Redwood.RedwoodChannels log = Redwood.channels(ExternalEvaluate.class);

  private List<Tree> predicted;

  public ExternalEvaluate(RNNOptions op, List<Tree> predictedTrees) {
    super(op);
    this.predicted = predictedTrees;
  }

  @Override
  public void populatePredictedLabels(List<Tree> trees) {
    if (trees.size() != this.predicted.size()) {
      throw new IllegalArgumentException("Number of gold and predicted trees not equal!");
    }
    for (int i = 0; i < trees.size(); i++) {
      Iterator<Tree> goldTree = trees.get(i).iterator();
      Iterator<Tree> predictedTree = this.predicted.get(i).iterator();
      while (goldTree.hasNext() || predictedTree.hasNext()) {
        Tree goldNode = goldTree.next();
        Tree predictedNode = predictedTree.next();
        if (goldNode == null || predictedNode == null) {
          throw new IllegalArgumentException("Trees not of equal length");
        }
        if (goldNode.isLeaf()) {
          continue;
        }
        CoreLabel label = (CoreLabel) goldNode.label();
        label.set(RNNCoreAnnotations.PredictedClass.class,
                RNNCoreAnnotations.getPredictedClass(predictedNode));
      }
    }

View on GitHub (pinned to 1b7edd19c4)