{"record":{"id":"cbca4c2846920fa9","repo":"stanfordnlp/CoreNLP","slug":"registered-entity-mentions-without-relation-mentio","errorCode":null,"errorMessage":"Registered entity mentions without relation mentions","messagePattern":"Registered entity mentions without relation mentions","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java","lineNumber":595,"sourceCode":"    if (keySet.contains(KBPTriplesAnnotation.class)) {\n      // mark that this sentence has kbp triples, potentially empty list\n      builder.setHasKBPTriplesAnnotation(true);\n      // store each of the kbp triples\n      for (RelationTriple triple : getAndRegister(sentence, keysToSerialize, KBPTriplesAnnotation.class)) {\n        builder.addKbpTriple(toProto(triple));\n      }\n    }\n    // Non-default annotators\n    if (keySet.contains(EntityMentionsAnnotation.class)) {\n      builder.setHasRelationAnnotations(true);\n      for (EntityMention entity : getAndRegister(sentence, keysToSerialize, EntityMentionsAnnotation.class)) {\n        builder.addEntity(toProto(entity));\n      }\n    } else {\n      builder.setHasRelationAnnotations(false);\n    }\n    if (keySet.contains(RelationMentionsAnnotation.class)) {\n      if (!builder.getHasRelationAnnotations()) { throw new IllegalStateException(\"Registered entity mentions without relation mentions\"); }\n      for (RelationMention relation : getAndRegister(sentence, keysToSerialize, RelationMentionsAnnotation.class)) {\n        builder.addRelation(toProto(relation));\n      }\n    }\n    // add each of the mentions in the List<Mentions> for this sentence\n    if (keySet.contains(CorefMentionsAnnotation.class)) {\n      builder.setHasCorefMentionsAnnotation(true);\n      for (Mention m : sentence.get(CorefMentionsAnnotation.class)) {\n        builder.addMentionsForCoref(toProto(m));\n      }\n      keysToSerialize.remove(CorefMentionsAnnotation.class);\n    }\n    // Entity mentions\n    if (keySet.contains(MentionsAnnotation.class)) {\n      for (CoreMap mention : sentence.get(MentionsAnnotation.class)) {\n        builder.addMentions(toProtoMention(mention));\n      }\n      keysToSerialize.remove(MentionsAnnotation.class);","sourceCodeStart":577,"sourceCodeEnd":613,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java#L577-L613","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure sentences with RelationMentionsAnnotation also contain EntityMentionsAnnotation (run the entity/mention annotators before relation extraction)","Remove RelationMentionsAnnotation if you only intend to keep entity mentions, or serialize before stripping","Check pipeline ordering: mention annotators must precede relation annotators"],"exampleFix":"// before\nsentence.remove(EntityMentionsAnnotation.class); // relations still present -> throws on serialize\n// after\nsentence.remove(RelationMentionsAnnotation.class); // drop both, or keep both","handlingStrategy":"validation","validationCode":"boolean hasRel = sentence.containsKey(RelationMentionsAnnotation.class); boolean hasEnt = sentence.containsKey(EntityMentionsAnnotation.class); if (hasRel && !hasEnt) { sentence.remove(RelationMentionsAnnotation.class); }","typeGuard":null,"tryCatchPattern":"try { serializer.toProto(sentence); } catch (IllegalStateException e) { if (e.getMessage().contains(\"entity mentions\")) { sentence.remove(RelationMentionsAnnotation.class); serializer.toProto(sentence); } else throw e; }","preventionTips":["Run mention annotators before relation annotators so both key sets are populated","Don't strip EntityMentionsAnnotation while keeping RelationMentionsAnnotation"],"tags":["java","serialization","corenlp","inconsistent-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}