stanfordnlp/CoreNLP · error · ClassCastException
Wanted LexicalizedParser, got
Error message
Wanted LexicalizedParser, got
What it means
loadModel(ObjectInputStream) reads one object and expects it to be a LexicalizedParser; anything else is rejected with this ClassCastException naming the actual class received. It also converts IOException/ClassNotFoundException from stream deserialization into RuntimeIOException/RuntimeException.
Solutions
- Point loadModel at a file that contains a serialized LexicalizedParser (created via saveParserToSerializedFile).
- Check the actual class named in the message and use the matching load API (e.g. loadParserQuery).
- Use LexicalizedParser.loadModel(String) so the correct loading path is chosen for the file.
Example fix
// before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("query.ser"));
LexicalizedParser lp = LexicalizedParser.loadModel(ois); // file is a query
// after
LexicalizedParser lp = LexicalizedParser.loadModel("parser.ser.gz"); Defensive patterns
Strategy: try-catch
Type guard
if (o instanceof LexicalizedParser) { LexicalizedParser lp = (LexicalizedParser) o; } Try / catch
try { parser = LexicalizedParser.loadModel(path); } catch (ClassCastException | RuntimeIOException | RuntimeException e) { log.error("Not a serialized parser model: " + e.getMessage()); } Prevention
- Save models only with saveParserToSerializedFile and load them with loadModel(String)
- Keep library versions consistent between save and load
When it happens
Trigger: Calling LexicalizedParser.loadModel(ObjectInputStream) on a stream whose next object is not a parser — e.g. the file holds a LexicalizedParserQuery, an Options object, or unrelated serialized data.
Common situations: Deserializing a file saved with parserQuery/other components, or reading a file produced by an incompatible library version where class layout differs.
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
- Classifier class casting problem.
- Expected tagged words
- This evaluator only works for the ShiftReduceParser
- This evaluator only works for the ShiftReduceParser
- addFeature was called with a features object that is…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/514d3d452fb3dae3.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java:202
LexicalizedParser parser = getParserFromFile(parserFileOrUrl, op);
if (extraFlags.length > 0) {
parser.setOptionFlags(extraFlags);
}
return parser;
}
/**
* Reads one object from the given ObjectInputStream, which is
* assumed to be a LexicalizedParser. Throws a ClassCastException
* if this is not true. The stream is not closed.
*/
public static LexicalizedParser loadModel(ObjectInputStream ois) {
try {
Object o = ois.readObject();
if (o instanceof LexicalizedParser) {
return (LexicalizedParser) o;
}
throw new ClassCastException("Wanted LexicalizedParser, got " +
o.getClass());
} catch (IOException e) {
throw new RuntimeIOException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static LexicalizedParser copyLexicalizedParser(LexicalizedParser parser) {
return new LexicalizedParser(parser.lex, parser.bg, parser.ug, parser.dg, parser.stateIndex, parser.wordIndex, parser.tagIndex, parser.op);
}
public LexicalizedParser(Lexicon lex, BinaryGrammar bg, UnaryGrammar ug, DependencyGrammar dg, Index<String> stateIndex, Index<String> wordIndex, Index<String> tagIndex, Options op) {
this.lex = lex;
this.bg = bg;
this.ug = ug;
this.dg = dg;
this.stateIndex = stateIndex;View on GitHub (pinned to 1b7edd19c4)