{"record":{"id":"302ac2f094e1515a","repo":"stanfordnlp/CoreNLP","slug":"you-mixed-corelabels-with-hw-getclass","errorCode":null,"errorMessage":"You mixed CoreLabels with \" + hw.getClass() + \"?  Why would you do that?","messagePattern":"You mixed CoreLabels with \" \\+ hw\\.getClass\\(\\) \\+ \"\\?  Why would you do that\\?","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/tagger/maxent/MaxentTagger.java","lineNumber":1383,"sourceCode":"                                    int sentNum, boolean outputLemmas) {\n    if (sentence == null) {\n      return \"\";\n    }\n    boolean hasCoreLabels = sentence.size() > 0 && sentence.get(0) instanceof CoreLabel;\n    StringBuilder sb = new StringBuilder();\n    sb.append(\"<sentence id=\\\"\").append(sentNum).append(\"\\\">\\n\");\n    int wordIndex = 0;\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      String tag = ((HasTag) hw).tag();\n      sb.append(\"  <word wid=\\\"\").append(wordIndex).append(\"\\\" pos=\\\"\").append(XMLUtils.escapeAttributeXML(tag)).append(\"\\\"\");\n      if (outputLemmas && hasCoreLabels) {\n        if ( ! (hw instanceof CoreLabel)) {\n          throw new IllegalArgumentException(\"You mixed CoreLabels with \" +\n                                             hw.getClass() + \"?  \" +\n                                             \"Why would you do that?\");\n        }\n        CoreLabel label = (CoreLabel) hw;\n        String lemma = label.lemma();\n        if (lemma != null) {\n          sb.append(\" lemma=\\\"\").append(XMLUtils.escapeAttributeXML(lemma)).append('\\\"');\n        }\n      }\n      sb.append(\">\").append(XMLUtils.escapeElementXML(word)).append(\"</word>\\n\");\n      ++wordIndex;\n    }\n    sb.append(\"</sentence>\\n\");\n    return sb.toString();\n  }\n\n  private static String getTsvWords(boolean verbose, boolean outputLemmas,\n                                    List<? extends HasWord> sentence) {","sourceCodeStart":1365,"sourceCodeEnd":1401,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/tagger/maxent/MaxentTagger.java#L1365-L1401","documentation":"In the XML output path with lemmas enabled, every token must be a CoreLabel so the lemma annotation can be read; if some tokens are CoreLabels and others are not (or none are), the tagger throws IllegalArgumentException 'You mixed CoreLabels with <class>? Why would you do that?'. It enforces homogeneous CoreLabel tokens when lemmas are requested.","triggerScenarios":"XML output with outputLemmas=true (hasCoreLabels true) and a sentence where any element is not a CoreLabel; also the verbose TSV path at line 1407 where the first token is a CoreLabel but later tokens are not.","commonSituations":"Mixing token factories in one sentence (first token CoreLabel, rest Word); enabling lemmatization options in output config while supplying custom token types; hand-assembled sentences after pipeline tokens were converted to plain Words.","solutions":["Make every token in the sentence a CoreLabel when lemmas are enabled in output.","Set lemmas on each CoreLabel (label.setLemma(...)) before output if precomputed.","Disable lemma output in the output format configuration if you only have plain words.","Validate homogeneity: all tokens instanceof CoreLabel before the call."],"exampleFix":"// before\nList<HasWord> sent = new ArrayList<>();\nsent.add(new CoreLabel());           // word 1 is CoreLabel\nsent.add(new Word(\"dog\"));           // word 2 is not -> throws\n// after\nList<HasWord> sent = new ArrayList<>();\nCoreLabel c1 = new CoreLabel(); c1.setWord(\"The\");\nCoreLabel c2 = new CoreLabel(); c2.setWord(\"dog\");\nsent.add(c1); sent.add(c2);","handlingStrategy":"type-guard","validationCode":"boolean homogeneousCoreLabels(java.util.List<? extends edu.stanford.nlp.ling.HasWord> sent) {\n  return sent.stream().allMatch(w -> w instanceof edu.stanford.nlp.ling.CoreLabel);\n}","typeGuard":"if (outputLemmas && sent.stream().anyMatch(w -> !(w instanceof CoreLabel)))\n  throw new IllegalArgumentException(\"Lemma output requires all-CoreLabel tokens\");","tryCatchPattern":"try {\n  tagger.outputTaggedSentence(sent, ...);\n} catch (IllegalArgumentException e) {\n  log.error(\"Mixed token types with lemma output: {}\", e.getMessage());\n}","preventionTips":["When lemmas are enabled, enforce CoreLabel-only token lists","Set lemma annotations before output if precomputed","Disable lemma output when using plain word tokens"],"tags":["java","illegalargumentexception","lemma","mixed-types"],"backgroundTag":"type-mismatch","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"}