stanfordnlp/CoreNLP · error · ClassCastException
Expected CoreLabels
Error message
Expected CoreLabels
What it means
castCoreLabels converts an input sentence to List<CoreLabel> and throws ClassCastException 'Expected CoreLabels' if any element does not implement CoreLabel. Some internal tagger paths (e.g. tagging with per-token required attributes) only work on CoreLabel tokens, so the library demands a homogeneous CoreLabel list.
Solutions
- Create the sentence as List<CoreLabel> using CoreLabel.wordFactory() or WordToSentenceProcessor output.
- Wrap each word in a CoreLabel and set the word field before calling the tagger.
- Run the text through a Stanford pipeline (tokenize/ssplit) so tokens are CoreLabels.
- Check each element with instanceof CoreLabel before calling.
Example fix
// before
List<HasWord> sent = new ArrayList<>();
sent.add(new Word("Hello"));
tagger.tagCoreLabels(sent); // ClassCastException
// after
List<CoreLabel> sent = new ArrayList<>();
CoreLabel cl = new CoreLabel();
cl.setWord("Hello");
sent.add(cl);
tagger.tagCoreLabels(sent); Defensive patterns
Strategy: type-guard
Validate before calling
boolean allCoreLabels(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 (sent.stream().anyMatch(w -> !(w instanceof CoreLabel))) {
throw new IllegalArgumentException("tagCoreLabels requires List<CoreLabel>");
} Try / catch
try {
tagger.tagCoreLabels(sent);
} catch (ClassCastException e) {
throw new IllegalArgumentException("Convert tokens to CoreLabel first", e);
} Prevention
- Build sentences via CoreLabel.wordFactory() or a Stanford pipeline
- Never mix token classes in one sentence
- Unit-test token construction for your pipeline
When it happens
Trigger: Calling tagger paths such as tagCoreLabels / apply on a List<? extends HasWord> containing plain Token/Word/TaggedWord or a String-based sentence instead of CoreLabel instances.
Common situations: Building the sentence yourself with BasicTokens or Words after loading from a custom reader; mixing token types in one list; passing output of a different annotator that produces non-CoreLabel tokens; Stanford CoreNLP pipeline normally produces CoreLabels, but hand-rolled code often does not.
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
- Unknown value for span
- don't know how to get Reader from class
- addFeature was called with a features object that is…
- Not sure if RVFDataset runs correctly in this method…
- minValue for feature
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/a81f1b78fcf8b274.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/tagger/maxent/MaxentTagger.java:1109
* Morphology object. The input list must already have tags set.
*/
public static void lemmatize(List<CoreLabel> sentence,
Morphology morpha) {
for (CoreLabel label : sentence) {
morpha.stem(label);
}
}
/**
* Casts a list of HasWords, which we secretly know to be
* CoreLabels, to a list of CoreLabels. Barfs if you didn't
* actually give it CoreLabels.
*/
private static List<CoreLabel> castCoreLabels(List<? extends HasWord> sent) {
List<CoreLabel> coreLabels = Generics.newArrayList();
for (HasWord word : sent) {
if (!(word instanceof CoreLabel)) {
throw new ClassCastException("Expected CoreLabels");
}
coreLabels.add((CoreLabel) word);
}
return coreLabels;
}
/**
* Reads data from r, tokenizes it with the default (Penn Treebank)
* tokenizer, and returns a List of Sentence objects, which can
* then be fed into tagSentence.
*
* @param r Reader where untokenized text is read
* @return List of tokenized sentences
*/
public static List<List<HasWord>> tokenizeText(Reader r) {
return tokenizeText(r, null);
}
View on GitHub (pinned to 1b7edd19c4)