stanfordnlp/CoreNLP · error · java.lang.ClassCastException
Wanted LexicalizedParser, got
Error message
Wanted LexicalizedParser, got ${object.getClass()} What it means
ParserGrammar.loadModelFromZip() throws this ClassCastException-style RuntimeException when the object deserialized from the zip entry is not an instance of ParserGrammar. The message reports the actual class of the loaded object. Historically the expected type was LexicalizedParser, hence the wording.
Solutions
- Verify the zip contains a serialized ParserGrammar/LexicalizedParser model
- Use the correct loader for the artifact type (e.g. Tagger's loader for tagger models)
- Regenerate the model with a compatible version of the parser
Example fix
// before
ParserGrammar pg = ParserGrammar.loadModelFromZip("tagger.zip", "model");
// after
MaxentTagger tagger = new MaxentTagger("tagger.zip"); // correct loader for tagger models Defensive patterns
Strategy: try-catch
Validate before calling
try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(in))) { Object o = ois.readObject(); if (!(o instanceof ParserGrammar)) throw new IllegalArgumentException("not a parser model: " + o.getClass()); } Type guard
boolean isParserModel(Object o) { return o instanceof ParserGrammar; } Try / catch
try { return ParserGrammar.loadModelFromZip(zip, name); } catch (RuntimeException e) { if (e.getMessage().startsWith("Wanted")) { throw new IOException("artifact is not a ParserGrammar; use the right loader", e); } throw e; } Prevention
- Use the loader matching the artifact type (parser vs tagger vs pipeline)
- Confirm model provenance and library version before loading
- Serialize models only with the same (or compatible) library version that reads them
When it happens
Trigger: Passing a zip whose entry contains some other serialized object (a different model type, a Pipeline, an arbitrary serialized blob) to loadModelFromZip.
Common situations: Mixing up model files, e.g. trying to load a tagger model zip as a parser; loading a model produced by an incompatible library version whose serialized class changed.
Understand the failure class
Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.
Related errors
- cannot be cast into a
- Error loading classifier from
- cannot be cast into a…
- format error in embeddings
- format error unexpected featureFactory line:
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/d8e69ad9bf88ec77.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/parser/common/ParserGrammar.java:241
ois.close();
in.close();
}
}
} else {
throw new FileNotFoundException("Could not find " + modelName +
" inside " + zipFilename);
}
} catch (IOException e) {
throw new RuntimeIOException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
if (object instanceof ParserGrammar) {
return (ParserGrammar) object;
}
throw new ClassCastException("Wanted LexicalizedParser, got " +
object.getClass());
}
}
View on GitHub (pinned to 1b7edd19c4)