{"record":{"id":"820f7bd746b646cc","repo":"stanfordnlp/CoreNLP","slug":"error-serialized-data-does-not-contain-an-annotat","errorCode":null,"errorMessage":"ERROR: Serialized data does not contain an Annotation!","messagePattern":"ERROR: Serialized data does not contain an Annotation!","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/pipeline/GenericAnnotationSerializer.java","lineNumber":63,"sourceCode":"    } else {\n      ObjectOutputStream objectOutput = new ObjectOutputStream(compress ? new GZIPOutputStream(os) : os);\n      objectOutput.writeObject(corpus);\n      return objectOutput;\n    }\n  }\n\n  @Override\n  public Pair<Annotation, InputStream> read(InputStream is) throws IOException, ClassNotFoundException, ClassCastException {\n    ObjectInputStream objectInput;\n    if (is instanceof ObjectInputStream) {\n      objectInput = (ObjectInputStream) is;\n    } else {\n      objectInput = new ObjectInputStream(compress ? new GZIPInputStream(is) : is);\n    }\n    Object annotation = objectInput.readObject();\n    if(annotation == null) return null;\n    if(! (annotation instanceof Annotation)){\n      throw new ClassCastException(\"ERROR: Serialized data does not contain an Annotation!\");\n    }\n    return Pair.makePair((Annotation) annotation, (InputStream) objectInput);\n  }\n\n}\n","sourceCodeStart":45,"sourceCodeEnd":69,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/pipeline/GenericAnnotationSerializer.java#L45-L69","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the file was written by GenericAnnotationSerializer.write with a pipeline Annotation as input","Check for file mix-ups — ensure the path points to the annotation output, not a model or text file","Confirm the serialized classes are on the classpath with matching versions (classloader/ serialVersionUID issues can alter the deserialized type)","Log objectInput.readObject().getClass() on a copy of the stream to identify what type is actually stored"],"exampleFix":"// before: reading arbitrary serialized object as annotation\nPair<Annotation, InputStream> p = serializer.read(new FileInputStream(\"model.ser.gz\"));\n// after: read the file that actually contains serialized annotations\nPair<Annotation, InputStream> p = serializer.read(new FileInputStream(\"output.ser.gz\"));","handlingStrategy":"type-guard","validationCode":"Object o = new ObjectInputStream(new GZIPInputStream(in)).readObject();\nif (!(o instanceof Annotation)) throw new IllegalArgumentException(\"Not an Annotation: \" + o.getClass());","typeGuard":"static boolean isAnnotationStream(InputStream is) throws IOException, ClassNotFoundException {\n  Object o = new ObjectInputStream(new GZIPInputStream(is)).readObject();\n  return o instanceof Annotation;\n}","tryCatchPattern":"try {\n  Pair<Annotation, InputStream> p = serializer.read(in);\n} catch (ClassCastException e) {\n  log.severe(\"Stream does not contain an Annotation: \" + e.getMessage());\n  // load the correct file or re-serialize\n}","preventionTips":["Keep annotation outputs in a dedicated directory separate from models","Ensure serialized classes are on the classpath with matching versions","Log the stored object's class when debugging deserialization","Verify pipeline output files are produced by GenericAnnotationSerializer.write"],"tags":["java","serialization","corenlp","classcast","deserialization"],"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"}