stanfordnlp/CoreNLP · error · ClassCastException
cannot be cast into a…
Error message
{object.getClass()} cannot be cast into a edu.stanford.nlp.ie.KBPStatisticalExtractor What it means
KBPEnsembleExtractor's main, when loading the statistical model file, requires the deserialized object to be either a LinearClassifier<String,String> or an existing KBPStatisticalExtractor. Any other object type is rejected with this ClassCastException.
Solutions
- Point the statistical model option at the correct KBPStatisticalExtractor/LinearClassifier serialized file
- Re-run the KBP statistical trainer to produce a proper model file
- Inspect the object's actual class in the wrapped file to identify what was serialized
- Verify you are using matching Stanford CoreNLP versions for training and loading
Example fix
// before
args.put("stat.serialize.model", "models/relation-extractor.ser.gz"); // wrong file
// after
args.put("stat.serialize.model", "models/kbp-statistical-model.ser.gz"); // LinearClassifier file Defensive patterns
Strategy: try-catch
Validate before calling
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(statModelPath))) {
Object o = in.readObject();
if (!(o instanceof LinearClassifier) && !(o instanceof KBPStatisticalExtractor))
throw new IllegalStateException("stat model file holds " + o.getClass());
} Type guard
boolean isStatModel(Object o) {
return o instanceof LinearClassifier || o instanceof KBPStatisticalExtractor;
} Try / catch
try {
runKBPMain(args);
} catch (ClassCastException e) {
log.error("wrong statistical model file type: " + e.getMessage());
throw e;
} Prevention
- Keep stat model and relation extractor model files clearly named/separated
- Load model files produced by the same CoreNLP version's trainer
- Sanity-check serialized object types after deployment upgrades
When it happens
Trigger: Passing -stat.serialize.model (statistical model) pointing to a file containing an object of an unexpected type (e.g. a different model class, a Map, or text file bytes deserialized as something else).
Common situations: Pointing the stat model option at the wrong serialized file (e.g. the relation extractor model instead of the statistical model); models produced by incompatible KBP training code versions.
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
- cannot be cast into a
- First line of input file should be header definition
- Could not parse CoNLL file
- Gabor sucks at logic and he should feel bad about it
- ERROR: Serialized data does not contain an Annotation!
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/5cc933cd9d392ea2.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ie/KBPEnsembleExtractor.java:89
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
RedwoodConfiguration.standard().apply(); // Disable SLF4J crap.
ArgumentParser.fillOptions(KBPEnsembleExtractor.class, args);
KBPRelationExtractor statisticalExtractor;
if (STATISTICAL_MODEL.length() == 0) {
logger.info("No statistical model will be used.");
statisticalExtractor = null;
} else {
Object object = IOUtils.readObjectFromURLOrClasspathOrFileSystem(STATISTICAL_MODEL);
if (object instanceof LinearClassifier) {
//noinspection unchecked
statisticalExtractor = new KBPStatisticalExtractor((Classifier<String, String>) object);
} else if (object instanceof KBPStatisticalExtractor) {
statisticalExtractor = (KBPStatisticalExtractor) object;
} else {
throw new ClassCastException(object.getClass() + " cannot be cast into a " + KBPStatisticalExtractor.class);
}
logger.info("Read statistical model from " + STATISTICAL_MODEL);
}
KBPRelationExtractor extractor;
if (statisticalExtractor == null) {
extractor = new KBPEnsembleExtractor(
new KBPTokensregexExtractor(TOKENSREGEX_DIR),
new KBPSemgrexExtractor(SEMGREX_DIR));
} else {
extractor = new KBPEnsembleExtractor(
new KBPTokensregexExtractor(TOKENSREGEX_DIR),
new KBPSemgrexExtractor(SEMGREX_DIR),
statisticalExtractor);
}
List<Pair<KBPInput, String>> testExamples = KBPRelationExtractor.readDataset(TEST_FILE);
View on GitHub (pinned to 1b7edd19c4)