stanfordnlp/CoreNLP · error · ClassCastException

Wanted SingletonPredictor, got

Error message

Wanted SingletonPredictor, got ${o.getClass()}

What it means

This ClassCastException is thrown by SieveCoreferenceSystem.loadSingletonPredictor when the object deserialized from the singleton-predictor model file is not a LogisticClassifier as expected. It indicates the serialized file's contents do not match what this code path expects, typically because the wrong model file was supplied or the model format changed between CoreNLP versions.

Solutions

  1. Verify the serialized file passed to the loader is the singleton predictor model produced by SingletonPredictor.saveToSerialized
  2. Regenerate the model file with a matching CoreNLP version
  3. Inspect the file's class with a small test deserialization to confirm what it contains
  4. Update CoreNLP so model and code versions match

Example fix

// before
props.setProperty("singletonModel", "my-coref-model.ser");
// after
props.setProperty("singletonModel", "singleton-predictor.ser"); // file saved by SingletonPredictor.saveToSerialized
Defensive patterns

Strategy: try-catch

Validate before calling

// Before loading, sanity-check the model file exists
File f = new File(modelPath);
if (!f.isFile() || f.length() == 0) throw new IllegalArgumentException("Bad singleton model file: " + modelPath);

Type guard

if (o instanceof LogisticClassifier<?, ?>) { /* safe cast */ } else { throw new IllegalArgumentException("Wrong model class: " + o.getClass()); }

Try / catch

try {
  predictor = SieveCoreferenceSystem.loadSingletonPredictor(path);
} catch (ClassCastException e) {
  throw new IllegalStateException("Singleton model file has wrong content; regenerate it with SingletonPredictor.saveToSerialized", e);
} catch (RuntimeIOException e) {
  throw new IllegalStateException("Cannot read singleton model: " + path, e);
}

Prevention

When it happens

Trigger: Calling the method that loads the singleton predictor model (via IOUtils.readStreamFromString on serializedFile) when the file contains any object that is not an instance of LogisticClassifier.

Common situations: Pointing -singletonModel (or dcoref properties) at a wrong or corrupted serialized file; using a predictor serialized by a different CoreNLP version whose class changed; confusing the singleton predictor file with another dcoref model file.

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


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

Appendix: source

Thrown at src/edu/stanford/nlp/dcoref/SieveCoreferenceSystem.java:1043

        removeClusterSet.add(c.clusterID);
      }
    }
    for (int removeId : removeClusterSet){
      document.corefClusters.remove(removeId);
    }
    for(Mention m : removeSet){
      document.positions.remove(m);
    }
  }

  public static LogisticClassifier<String, String> getSingletonPredictorFromSerializedFile(String serializedFile) {
    try {
      ObjectInputStream ois = IOUtils.readStreamFromString(serializedFile);
      Object o = ois.readObject();
      if (o instanceof LogisticClassifier<?, ?>) {
        return (LogisticClassifier<String, String>) o;
      }
      throw new ClassCastException("Wanted SingletonPredictor, got " + o.getClass());
    } catch (IOException e) {
      throw new RuntimeIOException(e);
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
  }

  /** Remove singleton clusters */
  public static List<List<Mention>> filterMentionsWithSingletonClusters(Document document, List<List<Mention>> mentions)
  {

    List<List<Mention>> res = new ArrayList<>(mentions.size());
    for (List<Mention> ml:mentions) {
      List<Mention> filtered = new ArrayList<>();
      for (Mention m:ml) {
        CorefCluster cluster = document.corefClusters.get(m.corefClusterID);
        if (cluster != null && cluster.getCorefMentions().size() > 1) {
          filtered.add(m);

View on GitHub (pinned to 1b7edd19c4)