{"record":{"id":"60e00a9346d2b648","repo":"stanfordnlp/CoreNLP","slug":"coremap-is-actually-a-corelabel","errorCode":null,"errorMessage":"CoreMap is actually a CoreLabel","messagePattern":"CoreMap is actually a CoreLabel","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java","lineNumber":500,"sourceCode":"    }\n    return builder.build();\n  }\n\n  /**\n   * <p>\n   *   The method to extend by subclasses of the Protobuf Annotator if custom additions are added to Tokens.\n   *   In contrast to {@link ProtobufAnnotationSerializer#toProto(edu.stanford.nlp.ling.CoreLabel)}, this function\n   *   returns a builder that can be extended.\n   * </p>\n   *\n   * @param sentence The sentence to save to a protocol buffer\n   * @param keysToSerialize A set tracking which keys have been saved. It's important to remove any keys added to the proto\n   *                        from this set, as the code tracks annotations to ensure lossless serialization.\n   */\n  @SuppressWarnings(\"deprecation\")\n  protected CoreNLPProtos.Sentence.Builder toProtoBuilder(CoreMap sentence, Set<Class<?>> keysToSerialize) {\n    // Error checks\n    if (sentence instanceof CoreLabel) { throw new IllegalArgumentException(\"CoreMap is actually a CoreLabel\"); }\n    CoreNLPProtos.Sentence.Builder builder = CoreNLPProtos.Sentence.newBuilder();\n    // Remove items serialized elsewhere from the required list\n    keysToSerialize.remove(TextAnnotation.class);\n    keysToSerialize.remove(NumerizedTokensAnnotation.class);\n    // Required fields\n    builder.setTokenOffsetBegin(getAndRegister(sentence, keysToSerialize, TokenBeginAnnotation.class));\n    builder.setTokenOffsetEnd(getAndRegister(sentence, keysToSerialize, TokenEndAnnotation.class));\n    // Get key set of CoreMap\n    Set<Class<?>> keySet;\n    if (sentence instanceof ArrayCoreMap) {\n       keySet = ((ArrayCoreMap) sentence).keySetNotNull();\n    } else {\n      keySet = new IdentityHashSet<>(sentence.keySet());\n    }\n    // Tokens\n    if (sentence.containsKey(TokensAnnotation.class)) {\n      int tokenIndex = 0;\n      for (CoreLabel tok : sentence.get(TokensAnnotation.class)) {","sourceCodeStart":482,"sourceCodeEnd":518,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java#L482-L518","documentation":"toProtoBuilder expects a sentence-level CoreMap. Tokens (CoreLabel) are serialized separately by a dedicated method, so passing a CoreLabel here is an API misuse and throws IllegalArgumentException immediately as an error check.","triggerScenarios":"Calling ProtobufAnnotationSerializer.toProtoBuilder(sentence, keysToSerialize) with a CoreLabel instance — e.g. mixing up token and sentence objects, or iterating TokensAnnotation and passing each CoreLabel to the sentence builder.","commonSituations":"Custom serialization code that confuses Annotation (document/sentence) with CoreLabel (token); refactors changing variable types; calling protected builder methods directly instead of the typed toProto overloads.","solutions":["Use serializer.toProto(coreLabel, keysToSkip) for tokens instead of toProtoBuilder","Ensure the object passed to toProtoBuilder is a sentence/document-level CoreMap (Annotation), not a CoreLabel","Call the public toProto overloads rather than the protected builder methods"],"exampleFix":"// before\nbuilder = serializer.toProtoBuilder(token, keys); // token is a CoreLabel -> throws\n// after\nCoreNLPProtos.Token tb = serializer.toProto(token, Collections.emptySet());","handlingStrategy":"type-guard","validationCode":"if (obj instanceof CoreLabel) { serializer.toProto((CoreLabel) obj, Collections.emptySet()); } else { serializer.toProto((CoreMap) obj); }","typeGuard":"boolean isToken(Object o) { return o instanceof CoreLabel; }","tryCatchPattern":"try { serializer.toProtoBuilder(sentence, keys); } catch (IllegalArgumentException e) { if (e.getMessage().equals(\"CoreMap is actually a CoreLabel\")) { serializer.toProto((CoreLabel) sentence, Collections.emptySet()); } else throw e; }","preventionTips":["Use the public typed toProto overloads; avoid calling protected builder methods","Keep token vs sentence variables clearly named to avoid mixups"],"tags":["java","serialization","type-mismatch","corenlp"],"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"}