stanfordnlp/CoreNLP · error · IllegalArgumentException
You mixed CoreLabels with " + hw.getClass() + "? Why would…
Error message
You mixed CoreLabels with " + hw.getClass() + "? Why would you do that?
What it means
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.
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.
Example fix
// before
List<HasWord> sent = new ArrayList<>();
sent.add(new CoreLabel()); // word 1 is CoreLabel
sent.add(new Word("dog")); // word 2 is not -> throws
// after
List<HasWord> sent = new ArrayList<>();
CoreLabel c1 = new CoreLabel(); c1.setWord("The");
CoreLabel c2 = new CoreLabel(); c2.setWord("dog");
sent.add(c1); sent.add(c2); Defensive patterns
Strategy: type-guard
Validate before calling
boolean homogeneousCoreLabels(java.util.List<? extends edu.stanford.nlp.ling.HasWord> sent) {
return sent.stream().allMatch(w -> w instanceof edu.stanford.nlp.ling.CoreLabel);
} Type guard
if (outputLemmas && sent.stream().anyMatch(w -> !(w instanceof CoreLabel)))
throw new IllegalArgumentException("Lemma output requires all-CoreLabel tokens"); Try / catch
try {
tagger.outputTaggedSentence(sent, ...);
} catch (IllegalArgumentException e) {
log.error("Mixed token types with lemma output: {}", e.getMessage());
} Prevention
- When lemmas are enabled, enforce CoreLabel-only token lists
- Set lemma annotations before output if precomputed
- Disable lemma output when using plain word tokens
When it happens
Trigger: 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.
Common situations: 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.
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
- Expected HasLemma with the outputLemmas set, but got class…
- Expected HasTags, got " + hw.getClass()
- Invalid metricType
- Invalid sub score type
- is not a legal LogPrior.
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/302ac2f094e1515a.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/tagger/maxent/MaxentTagger.java:1383
int sentNum, boolean outputLemmas) {
if (sentence == null) {
return "";
}
boolean hasCoreLabels = sentence.size() > 0 && sentence.get(0) instanceof CoreLabel;
StringBuilder sb = new StringBuilder();
sb.append("<sentence id=\"").append(sentNum).append("\">\n");
int wordIndex = 0;
for (HasWord hw : sentence) {
String word = hw.word();
if ( ! (hw instanceof HasTag)) {
throw new IllegalArgumentException("Expected HasTags, got " +
hw.getClass());
}
String tag = ((HasTag) hw).tag();
sb.append(" <word wid=\"").append(wordIndex).append("\" pos=\"").append(XMLUtils.escapeAttributeXML(tag)).append("\"");
if (outputLemmas && hasCoreLabels) {
if ( ! (hw instanceof CoreLabel)) {
throw new IllegalArgumentException("You mixed CoreLabels with " +
hw.getClass() + "? " +
"Why would you do that?");
}
CoreLabel label = (CoreLabel) hw;
String lemma = label.lemma();
if (lemma != null) {
sb.append(" lemma=\"").append(XMLUtils.escapeAttributeXML(lemma)).append('\"');
}
}
sb.append(">").append(XMLUtils.escapeElementXML(word)).append("</word>\n");
++wordIndex;
}
sb.append("</sentence>\n");
return sb.toString();
}
private static String getTsvWords(boolean verbose, boolean outputLemmas,
List<? extends HasWord> sentence) {View on GitHub (pinned to 1b7edd19c4)