stanfordnlp/CoreNLP · error · IllegalStateException

Registered entity mentions without relation mentions

Error message

Registered entity mentions without relation mentions

What it means

During sentence serialization the builder tracks whether entity mentions were registered via hasRelationAnnotations. If relation mentions are found in the key set but entity mentions were not serialized first, the internal state is inconsistent and an IllegalStateException is thrown.

Solutions

  1. Ensure sentences with RelationMentionsAnnotation also contain EntityMentionsAnnotation (run the entity/mention annotators before relation extraction)
  2. Remove RelationMentionsAnnotation if you only intend to keep entity mentions, or serialize before stripping
  3. Check pipeline ordering: mention annotators must precede relation annotators

Example fix

// before
sentence.remove(EntityMentionsAnnotation.class); // relations still present -> throws on serialize
// after
sentence.remove(RelationMentionsAnnotation.class); // drop both, or keep both
Defensive patterns

Strategy: validation

Validate before calling

boolean hasRel = sentence.containsKey(RelationMentionsAnnotation.class); boolean hasEnt = sentence.containsKey(EntityMentionsAnnotation.class); if (hasRel && !hasEnt) { sentence.remove(RelationMentionsAnnotation.class); }

Try / catch

try { serializer.toProto(sentence); } catch (IllegalStateException e) { if (e.getMessage().contains("entity mentions")) { sentence.remove(RelationMentionsAnnotation.class); serializer.toProto(sentence); } else throw e; }

Prevention

When it happens

Trigger: Serializing a sentence whose keySet contains RelationMentionsAnnotation but where EntityMentionsAnnotation was absent (or consumed out of order), so getHasRelationAnnotations() is false when relations are processed.

Common situations: Custom annotators adding relation mentions without the prerequisite entity mentions (e.g. running RelationExtractorAnnotator output manipulation); manually removing or skipping EntityMentionsAnnotation during serialization; mutating the sentence's keys mid-pipeline.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java:595

    if (keySet.contains(KBPTriplesAnnotation.class)) {
      // mark that this sentence has kbp triples, potentially empty list
      builder.setHasKBPTriplesAnnotation(true);
      // store each of the kbp triples
      for (RelationTriple triple : getAndRegister(sentence, keysToSerialize, KBPTriplesAnnotation.class)) {
        builder.addKbpTriple(toProto(triple));
      }
    }
    // Non-default annotators
    if (keySet.contains(EntityMentionsAnnotation.class)) {
      builder.setHasRelationAnnotations(true);
      for (EntityMention entity : getAndRegister(sentence, keysToSerialize, EntityMentionsAnnotation.class)) {
        builder.addEntity(toProto(entity));
      }
    } else {
      builder.setHasRelationAnnotations(false);
    }
    if (keySet.contains(RelationMentionsAnnotation.class)) {
      if (!builder.getHasRelationAnnotations()) { throw new IllegalStateException("Registered entity mentions without relation mentions"); }
      for (RelationMention relation : getAndRegister(sentence, keysToSerialize, RelationMentionsAnnotation.class)) {
        builder.addRelation(toProto(relation));
      }
    }
    // add each of the mentions in the List<Mentions> for this sentence
    if (keySet.contains(CorefMentionsAnnotation.class)) {
      builder.setHasCorefMentionsAnnotation(true);
      for (Mention m : sentence.get(CorefMentionsAnnotation.class)) {
        builder.addMentionsForCoref(toProto(m));
      }
      keysToSerialize.remove(CorefMentionsAnnotation.class);
    }
    // Entity mentions
    if (keySet.contains(MentionsAnnotation.class)) {
      for (CoreMap mention : sentence.get(MentionsAnnotation.class)) {
        builder.addMentions(toProtoMention(mention));
      }
      keysToSerialize.remove(MentionsAnnotation.class);

View on GitHub (pinned to 1b7edd19c4)