stanfordnlp/CoreNLP · error · IllegalArgumentException

Only works with CoreLabels trees

Error message

Only works with CoreLabels trees

What it means

SplitCanditoTrees.mungeLeaves processes tree leaf labels to extract lemma/morphology, but requires each leaf to be a CoreLabel carrying annotations. If a leaf is a plain Label/StringLabel, it throws this IllegalArgumentException since the needed annotation keys are unavailable.

Solutions

  1. Configure the tree reader with a factory producing CoreLabels, e.g. use a TreeReaderFactory that wraps labels via CoreLabelTokenFactory or TreeCoreAnnotations
  2. Convert the tree's labels to CoreLabel before calling mungeLeaves
  3. Check which Treebank factory was used to load the trees and switch it to one that preserves CoreLabels

Example fix

// before
Treebank tb = new DiskTreebank();
// after
Treebank tb = new DiskTreebank(new TreeReaderFactory() {
  public TreeReader newTreeReader(Reader in) {
    return new PennTreeReader(new BufferedReader(in), new LabeledScoredTreeFactory(CoreLabel.factory()));
  }
});
Defensive patterns

Strategy: type-guard

Validate before calling

for (Label l : tree.yield()) { if (!(l instanceof CoreLabel)) throw new IllegalStateException("Tree leaves must be CoreLabels"); }

Type guard

boolean hasCoreLabelLeaves(Tree tree) { return tree.yield().stream().allMatch(l -> l instanceof CoreLabel); }

Try / catch

try { mungeLeaves(tree, lemmasAsLeaves, addMorphoToLeaves); } catch (IllegalArgumentException e) { log.error("Wrong label type: " + e.getMessage()); }

Prevention

When it happens

Trigger: Reading Candito treebank trees with a TreeReaderFactory that produces plain labels (not CoreLabels) and then calling outputSplits, which invokes mungeLeaves.

Common situations: Using a default Treebank/TreeReader without setting a CoreLabel-producing factory (e.g. LabeledScoredTreeReaderFactory with StringLabels); mixing corpora read with different label factories.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/international/french/scripts/SplitCanditoTrees.java:166

    for (Tree t : treeMap.values()) {
      MWEPreprocessor.countMWEStatistics(t, unigramTagger,
                                         labelPreterm, pretermLabel,
                                         labelTerm, termLabel);
    }

    for (Tree t : treeMap.values()) {
      MWEPreprocessor.traverseAndFix(t, pretermLabel, unigramTagger);
    }
  }

  public static void mungeLeaves(Tree tree, boolean lemmasAsLeaves, boolean addMorphoToLeaves) {
    List<Label> labels = tree.yield();
    
    for (Label label : labels) {
      ++nTokens;
      if (!(label instanceof CoreLabel)) {
        throw new IllegalArgumentException("Only works with CoreLabels trees");
      }

      CoreLabel coreLabel = (CoreLabel) label;
      String lemma = coreLabel.lemma();
      //PTB escaping since we're going to put this in the leaf
      if (lemma == null) {
        // No lemma, so just add the surface form
        lemma = coreLabel.word();
      } else if (ESCAPE_PARENS && lemma.equals("(")) {
        lemma = "-LRB-";
      } else if (ESCAPE_PARENS && lemma.equals(")")) {
        lemma = "-RRB-";
      }

      if (lemmasAsLeaves) {
        String escapedLemma = lemma;
        coreLabel.setWord(escapedLemma);
        coreLabel.setValue(escapedLemma);

View on GitHub (pinned to 1b7edd19c4)