stanfordnlp/CoreNLP · error · IllegalArgumentException

Got a tree with labels which do not support lemma

Error message

Got a tree with labels which do not support lemma

What it means

TreeLemmatizer.transformTree requires every labeled word in the tree to implement the HasLemma interface so it can set the lemma via setLemma(). If a label does not support lemmas (e.g. plain CoreLabel built without the feature, or a custom Label), it throws immediately.

Solutions

  1. Build trees with CoreLabel-producing factories (e.g. LabeledScoredTreeFactory with CoreLabel factory or Tree.valueOf with a CoreLabel TreeReaderFactory).
  2. Transform tree labels to CoreLabel before lemmatizing (e.g. Trees.toTree with a CoreLabel factory).
  3. Ensure labels have value() and tag() populated so Morphology can compute the lemma.

Example fix

// before
Tree t = Tree.valueOf(str); // StringLabel leaves
// after
Tree t = new TreeReaderFactory() { ... }.newTreeReader(in).readTree(); // CoreLabel-backed
Defensive patterns

Strategy: type-guard

Validate before calling

for (Label lab : tree.yield()) { if (!(lab instanceof HasLemma)) { /* convert labels to CoreLabel first */ } }

Type guard

boolean lemmatizable(Tree t) { return t.yield().stream().allMatch(l -> l instanceof HasLemma); }

Try / catch

try { lemmatizer.transformTree(tree); } catch (IllegalArgumentException e) { tree = relabelWithCoreLabels(tree); lemmatizer.transformTree(tree); }

Prevention

When it happens

Trigger: Running the lemmatizer (annotator or main/collins tree transforms) over trees whose labels are not HasLemma instances — e.g. trees built with LabeledScoredTreeFactory using StringLabel, or CoreLabels created without word/tag info.

Common situations: Mixing tree factories: building parse trees with a default factory that produces StringLabel instead of CoreLabel; feeding pre-parsed trees into Stanford dependency/lemma conversion pipelines.

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/58437238b3a09cd1. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/trees/TreeLemmatizer.java:37

    for (Tree leaf : t.getLeaves()) {
      Label label = leaf.label();
      if (label == null) {
        continue;
      }

      String tag;
      if (!(label instanceof HasTag) || ((HasTag) label).tag() == null) {
        if (tagged == null) {
          tagged = t.taggedYield();
        }
        tag = tagged.get(index).tag();
      } else {
        tag = ((HasTag) label).tag();
      }

      if (!(label instanceof HasLemma)) {
        throw new IllegalArgumentException("Got a tree with labels which do not support lemma");
      }
      ((HasLemma) label).setLemma(morphology.lemma(label.value(), tag, true));
      ++index;
    }
    return t;
  }

}

View on GitHub (pinned to 1b7edd19c4)