{"record":{"id":"eb73a90711c30d59","repo":"stanfordnlp/CoreNLP","slug":"error-sentences-must-instantiate-annotation","errorCode":null,"errorMessage":"ERROR: Sentences must instantiate Annotation!","messagePattern":"ERROR: Sentences must instantiate Annotation!","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/machinereading/structure/AnnotationUtils.java","lineNumber":209,"sourceCode":"      sents = new ArrayList<>();\n      dataset.set(CoreAnnotations.SentencesAnnotation.class, sents);\n    }\n    sents.addAll(sentences);\n  }\n\n  /**\n   * Creates a deep copy of the given dataset with new lists for all mentions (entity, relation, event)\n   * @param dataset\n   */\n  public static Annotation deepMentionCopy(CoreMap dataset) {\n    Annotation newDataset = new Annotation(\"\");\n\n    List<CoreMap> sents = dataset.get(CoreAnnotations.SentencesAnnotation.class);\n    List<CoreMap> newSents = new ArrayList<>();\n    if(sents != null){\n      for(CoreMap sent: sents){\n        if(! (sent instanceof Annotation)){\n          throw new RuntimeException(\"ERROR: Sentences must instantiate Annotation!\");\n        }\n        CoreMap newSent = sentenceDeepMentionCopy((Annotation) sent);\n        newSents.add(newSent);\n      }\n    }\n\n    addSentences(newDataset, newSents);\n    return newDataset;\n  }\n\n  /**\n   * Deep copy of the sentence: we create new entity/relation/event lists here.\n   * However, we do not deep copy the ExtractionObjects themselves!\n   *\n   * @param sentence\n   */\n  public static Annotation sentenceDeepMentionCopy(Annotation sentence) {\n    Annotation newSent = new Annotation(sentence.get(CoreAnnotations.TextAnnotation.class));","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/machinereading/structure/AnnotationUtils.java#L191-L227","documentation":"AnnotationUtils.deepMentionCopy iterates the dataset's SentencesAnnotation and requires every sentence CoreMap to be a full Annotation instance, because sentenceDeepMentionCopy casts to Annotation. A sentence that is a plain CoreMap implementation throws this RuntimeException.","triggerScenarios":"Calling AnnotationUtils.deepMentionCopy on an Annotation whose sentences were created as generic CoreMaps (e.g. custom CoreMap objects, or sentences built without AnnotationBundle) instead of new Annotation(...).","commonSituations":"Custom pipeline code constructing sentences manually, third-party readers producing non-Annotation CoreMaps, mixing sentences from different pipeline sources before copying.","solutions":["Ensure all sentences are created via AnnotationUtils.sentenceFromCoreLabels or new Annotation(...) rather than raw CoreMap implementations.","Wrap sentences in an Annotation before adding them to SentencesAnnotation.","If you cannot change the source, copy sentences by rebuilding Annotations from their keys.","Check which reader produced the dataset and use the library's standard reader."],"exampleFix":"// before\nCoreMap sent = new Sentence(...); dataset.add(SentencesAnnotation, sent);\n// after\nAnnotation sent = new Annotation(coreMapContents);\ndataset.get(CoreAnnotations.SentencesAnnotation.class).add(sent);","handlingStrategy":"type-guard","validationCode":"List<CoreMap> sents = dataset.get(CoreAnnotations.SentencesAnnotation.class);\nif (sents != null && sents.stream().anyMatch(s -> !(s instanceof Annotation))) {\n  throw new IllegalStateException(\"all sentences must be Annotation instances\");\n}","typeGuard":"boolean isAnnotationSentence(CoreMap s) { return s instanceof Annotation; }","tryCatchPattern":"try { Annotation copy = AnnotationUtils.deepMentionCopy(dataset); } catch (RuntimeException e) { /* rebuild sentences as Annotation, retry */ }","preventionTips":["Create sentences with new Annotation(...) or AnnotationUtils helpers, never raw CoreMaps.","Check sentence types when merging data from multiple readers.","Wrap custom readers' output in Annotation before adding to SentencesAnnotation."],"tags":["java","stanford-nlp","coremap","copy"],"backgroundTag":"type-mismatch","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"}