stanfordnlp/CoreNLP · error · IllegalArgumentException
CoreMap is actually a CoreLabel
Error message
CoreMap is actually a CoreLabel
What it means
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.
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
Example fix
// before builder = serializer.toProtoBuilder(token, keys); // token is a CoreLabel -> throws // after CoreNLPProtos.Token tb = serializer.toProto(token, Collections.emptySet());
Defensive patterns
Strategy: type-guard
Validate before calling
if (obj instanceof CoreLabel) { serializer.toProto((CoreLabel) obj, Collections.emptySet()); } else { serializer.toProto((CoreMap) obj); } Type guard
boolean isToken(Object o) { return o instanceof CoreLabel; } Try / catch
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; } Prevention
- Use the public typed toProto overloads; avoid calling protected builder methods
- Keep token vs sentence variables clearly named to avoid mixups
When it happens
Trigger: 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.
Common situations: 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.
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
- RuntimeIOException wrapping IOException
- Failed to save classifier
- ERROR: Invalid dependency node line
- ERROR: Invalid format for dependency graph
- ERROR: Incorrect format for the serialized coref graph
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/60e00a9346d2b648.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java:500
}
return builder.build();
}
/**
* <p>
* The method to extend by subclasses of the Protobuf Annotator if custom additions are added to Tokens.
* In contrast to {@link ProtobufAnnotationSerializer#toProto(edu.stanford.nlp.ling.CoreLabel)}, this function
* returns a builder that can be extended.
* </p>
*
* @param sentence The sentence to save to a protocol buffer
* @param keysToSerialize A set tracking which keys have been saved. It's important to remove any keys added to the proto
* from this set, as the code tracks annotations to ensure lossless serialization.
*/
@SuppressWarnings("deprecation")
protected CoreNLPProtos.Sentence.Builder toProtoBuilder(CoreMap sentence, Set<Class<?>> keysToSerialize) {
// Error checks
if (sentence instanceof CoreLabel) { throw new IllegalArgumentException("CoreMap is actually a CoreLabel"); }
CoreNLPProtos.Sentence.Builder builder = CoreNLPProtos.Sentence.newBuilder();
// Remove items serialized elsewhere from the required list
keysToSerialize.remove(TextAnnotation.class);
keysToSerialize.remove(NumerizedTokensAnnotation.class);
// Required fields
builder.setTokenOffsetBegin(getAndRegister(sentence, keysToSerialize, TokenBeginAnnotation.class));
builder.setTokenOffsetEnd(getAndRegister(sentence, keysToSerialize, TokenEndAnnotation.class));
// Get key set of CoreMap
Set<Class<?>> keySet;
if (sentence instanceof ArrayCoreMap) {
keySet = ((ArrayCoreMap) sentence).keySetNotNull();
} else {
keySet = new IdentityHashSet<>(sentence.keySet());
}
// Tokens
if (sentence.containsKey(TokensAnnotation.class)) {
int tokenIndex = 0;
for (CoreLabel tok : sentence.get(TokensAnnotation.class)) {View on GitHub (pinned to 1b7edd19c4)