stanfordnlp/CoreNLP · critical · javax.servlet.ServletException

Classifier class casting problem.

Error message

Classifier class casting problem.

What it means

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.

Solutions

  1. Verify the model file is a serialized CRFClassifier produced by CRFClassifier.serializeClassifier / CRFClassifier's trainer.
  2. Re-download or regenerate the model with the same Stanford CoreNLP version as the deployed webapp (models jar version must match the CoreNLP version).
  3. Check servlet logs for the underlying ClassCastException stack trace to see which class the stream actually produced.
  4. Validate the model locally before deployment: CRFClassifier.getClassifier(path) in a small test program.

Example fix

// before: pointing servlet at wrong model type
<init-param><param-name>classifier</param-name><param-value>english-left3words-distsim.tagger</param-value></init-param>
// after: use a CRF NER model
<init-param><param-name>classifier</param-name><param-value>english.all.3class.distsim.crf.ser.gz</param-value></init-param>
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate model locally before deploying
try (InputStream is = new BufferedInputStream(new FileInputStream(modelFile))) {
  Object o = new ObjectInputStream(is).readObject();
  if (!(o instanceof CRFClassifier)) throw new IllegalStateException("Not a CRF classifier: " + o.getClass());
}

Try / catch

try {
  initServletWithClassifier(model);
} catch (ServletException e) {
  log.severe("Bad classifier file (wrong type/corrupt): " + e.getMessage());
  throw new UnavailableException("NER classifier failed to load");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/342951ba164450a4. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ie/ner/webapp/NERServlet.java:86

    for (String classifier : classifiers) {
      CRFClassifier<CoreMap> model = null;
      String filename = "/WEB-INF/data/models/" + classifier;
      InputStream is = getServletConfig().getServletContext().getResourceAsStream(filename);

      if (is == null) {
        throw new ServletException("File not found. Filename = " + filename);
      }
      try {
        if (filename.endsWith(".gz")) {
          is = new BufferedInputStream(new GZIPInputStream(is));
        } else {
          is = new BufferedInputStream(is);
        }
        model = CRFClassifier.getClassifier(is);
      } catch (IOException e) {
        throw new ServletException("IO problem reading classifier.");
      } catch (ClassCastException e) {
        throw new ServletException("Classifier class casting problem.");
      } catch (ClassNotFoundException e) {
        throw new ServletException("Classifier class not found problem.");
      } finally {
        IOUtils.closeIgnoringExceptions(is);
      }
      ners.put(classifier, model);
    }
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    if (request.getCharacterEncoding() == null) {
      request.setCharacterEncoding("utf-8");
    }
    response.setContentType("text/html; charset=UTF-8");

View on GitHub (pinned to 1b7edd19c4)