{"record":{"id":"70f1dcff33dd8b6a","repo":"stanfordnlp/CoreNLP","slug":"expected-haslemma-with-the-outputlemmas-set-but-g","errorCode":null,"errorMessage":"Expected HasLemma with the outputLemmas set, but got class \" + hw.getClass()","messagePattern":"Expected HasLemma with the outputLemmas set, but got class \" \\+ hw\\.getClass\\(\\)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/tagger/maxent/MaxentTagger.java","lineNumber":1442,"sourceCode":"        sb.append(label.endPosition());\n        sb.append(\"\\n\");\n      }\n      sb.append('\\n');\n      return sb.toString();\n    } // otherwise, fall through\n\n    // either not verbose, or not CoreLabels\n    for (HasWord hw : sentence) {\n      String word = hw.word();\n      if (!(hw instanceof HasTag)) {\n        throw new IllegalArgumentException(\"Expected HasTags, got \" +\n                                           hw.getClass());\n      }\n      sb.append(word);\n      sb.append('\\t');\n      if (outputLemmas) {\n        if (!(hw instanceof HasLemma)) {\n          throw new IllegalArgumentException(\"Expected HasLemma with the outputLemmas set, but got class \" + hw.getClass());\n        }\n        String lemma = ((HasLemma) hw).lemma();\n        sb.append(lemma);\n        sb.append(\"\\t\");\n      }\n      String tag = ((HasTag) hw).tag();\n      sb.append(tag);\n      sb.append('\\n');\n    }\n    sb.append('\\n');\n    return sb.toString();\n  }\n\n  /**\n   * Takes a tagged sentence and writes out the xml version.\n   *\n   * @param w Where to write the output to\n   * @param sent A tagged sentence","sourceCodeStart":1424,"sourceCodeEnd":1460,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/tagger/maxent/MaxentTagger.java#L1424-L1460","documentation":"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.","triggerScenarios":"outputLemmas=true in the tagger output config while outputting sentences whose tokens don't implement HasLemma (plain Word, custom token types).","commonSituations":"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.","solutions":["Use CoreLabel tokens (implements HasLemma and HasTag) and set the lemma value on each.","Turn off lemma output in the output format configuration.","Populate lemma via annotators (Morphology) before the output step.","Guard: ensure every token instanceof HasLemma when lemma output is on."],"exampleFix":"// before\nprops.setProperty(\"outputLemmas\", \"true\");\nList<HasWord> sent = Arrays.asList(new TaggedWord(\"dogs\", \"NNS\")); // no lemma support\n// after\nCoreLabel cl = new CoreLabel(); cl.setWord(\"dogs\"); cl.setTag(\"NNS\"); cl.setLemma(\"dog\");\nList<HasWord> sent = Arrays.asList(cl);","handlingStrategy":"type-guard","validationCode":"boolean supportsLemmas(java.util.List<? extends edu.stanford.nlp.ling.HasWord> sent) {\n  return sent.stream().allMatch(w -> w instanceof edu.stanford.nlp.ling.HasLemma);\n}","typeGuard":"if (outputLemmas && sent.stream().anyMatch(w -> !(w instanceof HasLemma)))\n  throw new IllegalArgumentException(\"Lemma output requires HasLemma tokens (CoreLabel)\");","tryCatchPattern":"try {\n  tagger.outputTaggedSentence(sent, true, ...);\n} catch (IllegalArgumentException e) {\n  log.error(\"outputLemmas set but token lacks lemma support: {}\", e.getMessage());\n}","preventionTips":["Enable outputLemmas only with CoreLabel tokens","Run Morphology/lemma annotation before output","Keep output format config aligned with token types"],"tags":["java","illegalargumentexception","lemma","token-type"],"backgroundTag":"incompatible-source-type","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}