stanfordnlp/CoreNLP · error · IllegalArgumentException

Expected HasLemma with the outputLemmas set, but got class…

Error message

Expected HasLemma with the outputLemmas set, but got class " + hw.getClass()

What it means

When lemma output is enabled (outputLemmas) the TSV path requires each token to implement HasLemma so its lemma can be printed; otherwise it throws IllegalArgumentException 'Expected HasLemma with the outputLemmas set, but got class <class>'. Tokens must also implement HasTag (checked just before), i.e. typically CoreLabel or a rich token type.

Solutions

  1. Use CoreLabel tokens (implements HasLemma and HasTag) and set the lemma value on each.
  2. Turn off lemma output in the output format configuration.
  3. Populate lemma via annotators (Morphology) before the output step.
  4. Guard: ensure every token instanceof HasLemma when lemma output is on.

Example fix

// before
props.setProperty("outputLemmas", "true");
List<HasWord> sent = Arrays.asList(new TaggedWord("dogs", "NNS")); // no lemma support
// after
CoreLabel cl = new CoreLabel(); cl.setWord("dogs"); cl.setTag("NNS"); cl.setLemma("dog");
List<HasWord> sent = Arrays.asList(cl);
Defensive patterns

Strategy: type-guard

Validate before calling

boolean supportsLemmas(java.util.List<? extends edu.stanford.nlp.ling.HasWord> sent) {
  return sent.stream().allMatch(w -> w instanceof edu.stanford.nlp.ling.HasLemma);
}

Type guard

if (outputLemmas && sent.stream().anyMatch(w -> !(w instanceof HasLemma)))
  throw new IllegalArgumentException("Lemma output requires HasLemma tokens (CoreLabel)");

Try / catch

try {
  tagger.outputTaggedSentence(sent, true, ...);
} catch (IllegalArgumentException e) {
  log.error("outputLemmas set but token lacks lemma support: {}", e.getMessage());
}

Prevention

When it happens

Trigger: outputLemmas=true in the tagger output config while outputting sentences whose tokens don't implement HasLemma (plain Word, custom token types).

Common situations: Configuring outputFormat with lemma fields while using basic token classes; tokens produced by a custom reader lacking lemma support; migrating output config from a CoreLabel-based pipeline to hand-made tokens.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/tagger/maxent/MaxentTagger.java:1442

        sb.append(label.endPosition());
        sb.append("\n");
      }
      sb.append('\n');
      return sb.toString();
    } // otherwise, fall through

    // either not verbose, or not CoreLabels
    for (HasWord hw : sentence) {
      String word = hw.word();
      if (!(hw instanceof HasTag)) {
        throw new IllegalArgumentException("Expected HasTags, got " +
                                           hw.getClass());
      }
      sb.append(word);
      sb.append('\t');
      if (outputLemmas) {
        if (!(hw instanceof HasLemma)) {
          throw new IllegalArgumentException("Expected HasLemma with the outputLemmas set, but got class " + hw.getClass());
        }
        String lemma = ((HasLemma) hw).lemma();
        sb.append(lemma);
        sb.append("\t");
      }
      String tag = ((HasTag) hw).tag();
      sb.append(tag);
      sb.append('\n');
    }
    sb.append('\n');
    return sb.toString();
  }

  /**
   * Takes a tagged sentence and writes out the xml version.
   *
   * @param w Where to write the output to
   * @param sent A tagged sentence

View on GitHub (pinned to 1b7edd19c4)