{"record":{"id":"342951ba164450a4","repo":"stanfordnlp/CoreNLP","slug":"classifier-class-casting-problem","errorCode":null,"errorMessage":"Classifier class casting problem.","messagePattern":"Classifier class casting problem\\.","errorType":"http","errorClass":"javax.servlet.ServletException","httpStatus":500,"severity":"critical","filePath":"src/edu/stanford/nlp/ie/ner/webapp/NERServlet.java","lineNumber":86,"sourceCode":"    for (String classifier : classifiers) {\n      CRFClassifier<CoreMap> model = null;\n      String filename = \"/WEB-INF/data/models/\" + classifier;\n      InputStream is = getServletConfig().getServletContext().getResourceAsStream(filename);\n\n      if (is == null) {\n        throw new ServletException(\"File not found. Filename = \" + filename);\n      }\n      try {\n        if (filename.endsWith(\".gz\")) {\n          is = new BufferedInputStream(new GZIPInputStream(is));\n        } else {\n          is = new BufferedInputStream(is);\n        }\n        model = CRFClassifier.getClassifier(is);\n      } catch (IOException e) {\n        throw new ServletException(\"IO problem reading classifier.\");\n      } catch (ClassCastException e) {\n        throw new ServletException(\"Classifier class casting problem.\");\n      } catch (ClassNotFoundException e) {\n        throw new ServletException(\"Classifier class not found problem.\");\n      } finally {\n        IOUtils.closeIgnoringExceptions(is);\n      }\n      ners.put(classifier, model);\n    }\n  }\n\n  @Override\n  public void doGet(HttpServletRequest request, HttpServletResponse response)\n    throws ServletException, IOException\n  {\n    if (request.getCharacterEncoding() == null) {\n      request.setCharacterEncoding(\"utf-8\");\n    }\n    response.setContentType(\"text/html; charset=UTF-8\");\n","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/ner/webapp/NERServlet.java#L68-L104","documentation":"During NERServlet.init, CRFClassifier.getClassifier(is) deserializes a serialized CRF classifier from an input stream. If the deserialized object is not an instance of CRFClassifier (e.g. the stream holds a different Stanford NLP model type), a ClassCastException is caught and rethrown as this ServletException, aborting servlet startup so a bad model file is reported at deployment time.","triggerScenarios":"Deploying the NER servlet with a classifier file that deserializes to a type other than CRFClassifier (e.g. a TokenNameFinder model, a MaxentClassifier, or a corrupted/garbage file that still deserializes), passed via the classifier init parameter.","commonSituations":"Pointing the servlet at a model trained/saved by a different Stanford NLP component, mixing CoreNLP version X server with a model serialized by version Y, or accidentally serving a gzip/text file as a serialized classifier.","solutions":["Verify the model file is a serialized CRFClassifier produced by CRFClassifier.serializeClassifier / CRFClassifier's trainer.","Re-download or regenerate the model with the same Stanford CoreNLP version as the deployed webapp (models jar version must match the CoreNLP version).","Check servlet logs for the underlying ClassCastException stack trace to see which class the stream actually produced.","Validate the model locally before deployment: CRFClassifier.getClassifier(path) in a small test program."],"exampleFix":"// before: pointing servlet at wrong model type\n<init-param><param-name>classifier</param-name><param-value>english-left3words-distsim.tagger</param-value></init-param>\n// after: use a CRF NER model\n<init-param><param-name>classifier</param-name><param-value>english.all.3class.distsim.crf.ser.gz</param-value></init-param>","handlingStrategy":"try-catch","validationCode":"// Validate model locally before deploying\ntry (InputStream is = new BufferedInputStream(new FileInputStream(modelFile))) {\n  Object o = new ObjectInputStream(is).readObject();\n  if (!(o instanceof CRFClassifier)) throw new IllegalStateException(\"Not a CRF classifier: \" + o.getClass());\n}","typeGuard":null,"tryCatchPattern":"try {\n  initServletWithClassifier(model);\n} catch (ServletException e) {\n  log.severe(\"Bad classifier file (wrong type/corrupt): \" + e.getMessage());\n  throw new UnavailableException(\"NER classifier failed to load\");\n}","preventionTips":["Always pair model files with the same CoreNLP version that serialized them.","Smoke-test classifier loading in CI before deploying the WAR.","Only use models from CRFClassifier.serializeClassifier outputs."],"tags":["servlet","deserialization","class-cast","ner"],"backgroundTag":"type-mismatch","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}