stanfordnlp/CoreNLP · error · RuntimeException

ERROR: Sentences must instantiate Annotation!

Error message

ERROR: Sentences must instantiate Annotation!

What it means

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.

Solutions

  1. Ensure all sentences are created via AnnotationUtils.sentenceFromCoreLabels or new Annotation(...) rather than raw CoreMap implementations.
  2. Wrap sentences in an Annotation before adding them to SentencesAnnotation.
  3. If you cannot change the source, copy sentences by rebuilding Annotations from their keys.
  4. Check which reader produced the dataset and use the library's standard reader.

Example fix

// before
CoreMap sent = new Sentence(...); dataset.add(SentencesAnnotation, sent);
// after
Annotation sent = new Annotation(coreMapContents);
dataset.get(CoreAnnotations.SentencesAnnotation.class).add(sent);
Defensive patterns

Strategy: type-guard

Validate before calling

List<CoreMap> sents = dataset.get(CoreAnnotations.SentencesAnnotation.class);
if (sents != null && sents.stream().anyMatch(s -> !(s instanceof Annotation))) {
  throw new IllegalStateException("all sentences must be Annotation instances");
}

Type guard

boolean isAnnotationSentence(CoreMap s) { return s instanceof Annotation; }

Try / catch

try { Annotation copy = AnnotationUtils.deepMentionCopy(dataset); } catch (RuntimeException e) { /* rebuild sentences as Annotation, retry */ }

Prevention

When it happens

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

Common situations: Custom pipeline code constructing sentences manually, third-party readers producing non-Annotation CoreMaps, mixing sentences from different pipeline sources before copying.

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

Appendix: source

Thrown at src/edu/stanford/nlp/ie/machinereading/structure/AnnotationUtils.java:209

      sents = new ArrayList<>();
      dataset.set(CoreAnnotations.SentencesAnnotation.class, sents);
    }
    sents.addAll(sentences);
  }

  /**
   * Creates a deep copy of the given dataset with new lists for all mentions (entity, relation, event)
   * @param dataset
   */
  public static Annotation deepMentionCopy(CoreMap dataset) {
    Annotation newDataset = new Annotation("");

    List<CoreMap> sents = dataset.get(CoreAnnotations.SentencesAnnotation.class);
    List<CoreMap> newSents = new ArrayList<>();
    if(sents != null){
      for(CoreMap sent: sents){
        if(! (sent instanceof Annotation)){
          throw new RuntimeException("ERROR: Sentences must instantiate Annotation!");
        }
        CoreMap newSent = sentenceDeepMentionCopy((Annotation) sent);
        newSents.add(newSent);
      }
    }

    addSentences(newDataset, newSents);
    return newDataset;
  }

  /**
   * Deep copy of the sentence: we create new entity/relation/event lists here.
   * However, we do not deep copy the ExtractionObjects themselves!
   *
   * @param sentence
   */
  public static Annotation sentenceDeepMentionCopy(Annotation sentence) {
    Annotation newSent = new Annotation(sentence.get(CoreAnnotations.TextAnnotation.class));

View on GitHub (pinned to 1b7edd19c4)