stanfordnlp/CoreNLP · error · ClassCastException
cannot be cast into a
Error message
${object.getClass()} cannot be cast into a ${KBPStatisticalExtractor.class} What it means
KBPAnnotator's constructor loads a statistical relation-extraction model and requires the deserialized object to be either a LinearClassifier (wrapped into a KBPStatisticalExtractor) or an already-built KBPStatisticalExtractor. If the model file deserializes to any other class, a ClassCastException is thrown naming the actual and expected types.
Solutions
- Point the KBP statistical model property at the correct serialized model from the matching corenlp models jar (kbp models zip)
- Verify the model file deserializes to LinearClassifier or KBPStatisticalExtractor (inspect with ObjectInputStream in a scratch program)
- Ensure the CoreNLP and model archive versions match (e.g. both from the same release)
- Re-download the kbp models archive and check checksums to rule out corruption
Example fix
// before
props.setProperty("kbp.stat_model", "models/ner-model.ser.gz");
// after
props.setProperty("kbp.stat_model", "edu/stanford/nlp/models/kbp/kbp_statistical_model.ser.gz"); Defensive patterns
Strategy: validation
Validate before calling
try (ObjectInputStream in = new ObjectInputStream(new GZIPInputStream(new FileInputStream(modelPath)))) {
Object o = in.readObject();
if (!(o instanceof LinearClassifier) && !(o instanceof KBPStatisticalExtractor))
throw new IllegalArgumentException("Not a KBP statistical model: " + o.getClass());
} Type guard
static boolean isKbpModel(Object o) {
return o instanceof LinearClassifier || o instanceof KBPStatisticalExtractor;
} Try / catch
try {
pipeline = new StanfordCoreNLP(props);
} catch (ClassCastException e) {
if (e.getMessage().contains("cannot be cast into a")) {
log.severe("Wrong KBP model file: " + e.getMessage());
// point kbp model property at the correct .ser.gz
} else throw e;
} Prevention
- Load KBP models only from the official corenlp models archive
- Match CoreNLP and models archive versions
- Verify model paths in properties point to KBP models, not NER/sentiment models
- Prefer classpath resource paths (edu/stanford/nlp/models/kbp/...) over ad-hoc files
When it happens
Trigger: Setting the kbp.stat_extractor (or equivalent model path) property to a file that is not a serialized KBP statistical model — e.g. a different classifier, a generic model, or a corrupt/mismatched file.
Common situations: Pointing KBP model properties at the wrong serialized model (e.g. an NER or sentiment classifier); CoreNLP/kbp-models version mismatch where the model class changed; downloading partial or wrong model archives.
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…
- 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
- Wanted LexicalizedParser, got
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/9f2298b6fe673a50.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/pipeline/KBPAnnotator.java:127
ArrayList<KBPRelationExtractor> extractors = new ArrayList<>();
// add tokensregex rules
if (!tokensregexdir.equals(NOT_PROVIDED))
extractors.add(new KBPTokensregexExtractor(tokensregexdir, VERBOSE));
// add semgrex rules
if (!semgrexdir.equals(NOT_PROVIDED))
extractors.add(new KBPSemgrexExtractor(semgrexdir,VERBOSE));
// attempt to add statistical model
if (!model.equals(NOT_PROVIDED)) {
log.info("Loading KBP classifier from: " + model);
Object object = IOUtils.readObjectFromURLOrClasspathOrFileSystem(model);
KBPRelationExtractor statisticalExtractor;
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);
}
extractors.add(statisticalExtractor);
}
// build extractor
this.extractor = new KBPEnsembleExtractor(extractors.toArray(new KBPRelationExtractor[0]));
// set maximum length of sentence to operate on
maxLength = Integer.parseInt(props.getProperty("kbp.maxlen", "-1"));
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeIOException(e);
}
// set up map for converting between older and new KBP relation names
relationNameConversionMap = new HashMap<>();
relationNameConversionMap.put("org:dissolved", "org:date_dissolved");
relationNameConversionMap.put("org:founded", "org:date_founded");
relationNameConversionMap.put("org:number_of_employees/members", "org:number_of_employees_members");
relationNameConversionMap.put("org:political/religious_affiliation", "org:political_religious_affiliation");
relationNameConversionMap.put("org:top_members/employees", "org:top_members_employees");View on GitHub (pinned to 1b7edd19c4)