stanfordnlp/CoreNLP · error · ClassCastException

ERROR: Serialized data does not contain an Annotation!

Error message

ERROR: Serialized data does not contain an Annotation!

What it means

GenericAnnotationSerializer.read deserializes an object (optionally gzip-compressed) and expects it to be an edu.stanford.nlp.pipeline.Annotation. If the object is non-null but of a different class, a ClassCastException with this message is thrown, meaning the stream contains serialized data that is not an Annotation.

Solutions

  1. Verify the file was written by GenericAnnotationSerializer.write with a pipeline Annotation as input
  2. Check for file mix-ups — ensure the path points to the annotation output, not a model or text file
  3. Confirm the serialized classes are on the classpath with matching versions (classloader/ serialVersionUID issues can alter the deserialized type)
  4. Log objectInput.readObject().getClass() on a copy of the stream to identify what type is actually stored

Example fix

// before: reading arbitrary serialized object as annotation
Pair<Annotation, InputStream> p = serializer.read(new FileInputStream("model.ser.gz"));
// after: read the file that actually contains serialized annotations
Pair<Annotation, InputStream> p = serializer.read(new FileInputStream("output.ser.gz"));
Defensive patterns

Strategy: type-guard

Validate before calling

Object o = new ObjectInputStream(new GZIPInputStream(in)).readObject();
if (!(o instanceof Annotation)) throw new IllegalArgumentException("Not an Annotation: " + o.getClass());

Type guard

static boolean isAnnotationStream(InputStream is) throws IOException, ClassNotFoundException {
  Object o = new ObjectInputStream(new GZIPInputStream(is)).readObject();
  return o instanceof Annotation;
}

Try / catch

try {
  Pair<Annotation, InputStream> p = serializer.read(in);
} catch (ClassCastException e) {
  log.severe("Stream does not contain an Annotation: " + e.getMessage());
  // load the correct file or re-serialize
}

Prevention

When it happens

Trigger: Calling read() on a stream whose contents were written with a plain Java ObjectOutputStream of some non-Annotation object, or data written by an incompatible version/class that deserializes to a different type.

Common situations: Pointing the deserializer at the wrong file (e.g. a plain text corpus file or a serialized model); mixing serialized outputs from different libraries; loading a file produced by GenericAnnotationSerializer.write on an object that wasn't an Annotation.

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/820f7bd746b646cc. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/pipeline/GenericAnnotationSerializer.java:63

    } else {
      ObjectOutputStream objectOutput = new ObjectOutputStream(compress ? new GZIPOutputStream(os) : os);
      objectOutput.writeObject(corpus);
      return objectOutput;
    }
  }

  @Override
  public Pair<Annotation, InputStream> read(InputStream is) throws IOException, ClassNotFoundException, ClassCastException {
    ObjectInputStream objectInput;
    if (is instanceof ObjectInputStream) {
      objectInput = (ObjectInputStream) is;
    } else {
      objectInput = new ObjectInputStream(compress ? new GZIPInputStream(is) : is);
    }
    Object annotation = objectInput.readObject();
    if(annotation == null) return null;
    if(! (annotation instanceof Annotation)){
      throw new ClassCastException("ERROR: Serialized data does not contain an Annotation!");
    }
    return Pair.makePair((Annotation) annotation, (InputStream) objectInput);
  }

}

View on GitHub (pinned to 1b7edd19c4)