{"record":{"id":"a81f1b78fcf8b274","repo":"stanfordnlp/CoreNLP","slug":"expected-corelabels","errorCode":null,"errorMessage":"Expected CoreLabels","messagePattern":"Expected CoreLabels","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/tagger/maxent/MaxentTagger.java","lineNumber":1109,"sourceCode":"   * Morphology object.  The input list must already have tags set.\n   */\n  public static void lemmatize(List<CoreLabel> sentence,\n                               Morphology morpha) {\n    for (CoreLabel label : sentence) {\n      morpha.stem(label);\n    }\n  }\n\n  /**\n   * Casts a list of HasWords, which we secretly know to be\n   * CoreLabels, to a list of CoreLabels.  Barfs if you didn't\n   * actually give it CoreLabels.\n   */\n  private static List<CoreLabel> castCoreLabels(List<? extends HasWord> sent) {\n    List<CoreLabel> coreLabels = Generics.newArrayList();\n    for (HasWord word : sent) {\n      if (!(word instanceof CoreLabel)) {\n        throw new ClassCastException(\"Expected CoreLabels\");\n      }\n      coreLabels.add((CoreLabel) word);\n    }\n    return coreLabels;\n  }\n\n  /**\n   * Reads data from r, tokenizes it with the default (Penn Treebank)\n   * tokenizer, and returns a List of Sentence objects, which can\n   * then be fed into tagSentence.\n   *\n   * @param r Reader where untokenized text is read\n   * @return List of tokenized sentences\n   */\n  public static List<List<HasWord>> tokenizeText(Reader r) {\n    return tokenizeText(r, null);\n  }\n","sourceCodeStart":1091,"sourceCodeEnd":1127,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/tagger/maxent/MaxentTagger.java#L1091-L1127","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nList<HasWord> sent = new ArrayList<>();\nsent.add(new Word(\"Hello\"));\ntagger.tagCoreLabels(sent); // ClassCastException\n// after\nList<CoreLabel> sent = new ArrayList<>();\nCoreLabel cl = new CoreLabel();\ncl.setWord(\"Hello\");\nsent.add(cl);\ntagger.tagCoreLabels(sent);","handlingStrategy":"type-guard","validationCode":"boolean allCoreLabels(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 (sent.stream().anyMatch(w -> !(w instanceof CoreLabel))) {\n  throw new IllegalArgumentException(\"tagCoreLabels requires List<CoreLabel>\");\n}","tryCatchPattern":"try {\n  tagger.tagCoreLabels(sent);\n} catch (ClassCastException e) {\n  throw new IllegalArgumentException(\"Convert tokens to CoreLabel first\", e);\n}","preventionTips":["Build sentences via CoreLabel.wordFactory() or a Stanford pipeline","Never mix token classes in one sentence","Unit-test token construction for your pipeline"],"tags":["java","classcastexception","type-mismatch","stanford-nlp"],"backgroundTag":"type-mismatch","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}