{"record":{"id":"9f2298b6fe673a50","repo":"stanfordnlp/CoreNLP","slug":"object-getclass-cannot-be-cast-into-a-kbpst","errorCode":null,"errorMessage":"${object.getClass()} cannot be cast into a ${KBPStatisticalExtractor.class}","messagePattern":"(.+?) cannot be cast into a (.+?)","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/pipeline/KBPAnnotator.java","lineNumber":127,"sourceCode":"      ArrayList<KBPRelationExtractor> extractors = new ArrayList<>();\n      // add tokensregex rules\n      if (!tokensregexdir.equals(NOT_PROVIDED))\n        extractors.add(new KBPTokensregexExtractor(tokensregexdir, VERBOSE));\n      // add semgrex rules\n      if (!semgrexdir.equals(NOT_PROVIDED))\n        extractors.add(new KBPSemgrexExtractor(semgrexdir,VERBOSE));\n      // attempt to add statistical model\n      if (!model.equals(NOT_PROVIDED)) {\n        log.info(\"Loading KBP classifier from: \" + model);\n        Object object = IOUtils.readObjectFromURLOrClasspathOrFileSystem(model);\n        KBPRelationExtractor statisticalExtractor;\n        if (object instanceof LinearClassifier) {\n          //noinspection unchecked\n          statisticalExtractor = new KBPStatisticalExtractor((Classifier<String, String>) object);\n        } else if (object instanceof KBPStatisticalExtractor) {\n          statisticalExtractor = (KBPStatisticalExtractor) object;\n        } else {\n          throw new ClassCastException(object.getClass() + \" cannot be cast into a \" + KBPStatisticalExtractor.class);\n        }\n        extractors.add(statisticalExtractor);\n      }\n      // build extractor\n      this.extractor = new KBPEnsembleExtractor(extractors.toArray(new KBPRelationExtractor[0]));\n      // set maximum length of sentence to operate on\n      maxLength = Integer.parseInt(props.getProperty(\"kbp.maxlen\", \"-1\"));\n    } catch (IOException | ClassNotFoundException e) {\n      throw new RuntimeIOException(e);\n    }\n\n    // set up map for converting between older and new KBP relation names\n    relationNameConversionMap = new HashMap<>();\n    relationNameConversionMap.put(\"org:dissolved\", \"org:date_dissolved\");\n    relationNameConversionMap.put(\"org:founded\", \"org:date_founded\");\n    relationNameConversionMap.put(\"org:number_of_employees/members\", \"org:number_of_employees_members\");\n    relationNameConversionMap.put(\"org:political/religious_affiliation\", \"org:political_religious_affiliation\");\n    relationNameConversionMap.put(\"org:top_members/employees\", \"org:top_members_employees\");","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/pipeline/KBPAnnotator.java#L109-L145","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nprops.setProperty(\"kbp.stat_model\", \"models/ner-model.ser.gz\");\n// after\nprops.setProperty(\"kbp.stat_model\", \"edu/stanford/nlp/models/kbp/kbp_statistical_model.ser.gz\");","handlingStrategy":"validation","validationCode":"try (ObjectInputStream in = new ObjectInputStream(new GZIPInputStream(new FileInputStream(modelPath)))) {\n  Object o = in.readObject();\n  if (!(o instanceof LinearClassifier) && !(o instanceof KBPStatisticalExtractor))\n    throw new IllegalArgumentException(\"Not a KBP statistical model: \" + o.getClass());\n}","typeGuard":"static boolean isKbpModel(Object o) {\n  return o instanceof LinearClassifier || o instanceof KBPStatisticalExtractor;\n}","tryCatchPattern":"try {\n  pipeline = new StanfordCoreNLP(props);\n} catch (ClassCastException e) {\n  if (e.getMessage().contains(\"cannot be cast into a\")) {\n    log.severe(\"Wrong KBP model file: \" + e.getMessage());\n    // point kbp model property at the correct .ser.gz\n  } else throw e;\n}","preventionTips":["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"],"tags":["java","corenlp","kbp","model-loading","classcast"],"backgroundTag":"incompatible-source-type","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"}