stanfordnlp/CoreNLP · error · UnsupportedOperationException
Unknown key
Error message
Unknown key <key>
What it means
CoreLabel.initFromStrings throws UnsupportedOperationException when a key string cannot be mapped to a known CoreAnnotation class via AnnotationLookup.toCoreKey and the key is non-null. Only annotation names in the lookup table are accepted.
Solutions
- Use exact registered key names (e.g. "Text", "PartOfSpeech", "Lemma", "NamedEntityTag", "Word", "Tag", "Answer").
- Check the typo against AnnotationLookup's key table in the version you are using.
- Pre-map custom column names to known keys before constructing the CoreLabel.
- If the key is genuinely new, add a CoreAnnotation class registered in AnnotationLookup or set the value via label.set after construction.
Example fix
// before
new CoreLabel(new String[]{"pos"}, new String[]{"NN"}); // Unknown key pos
// after
new CoreLabel(new String[]{"PartOfSpeech"}, new String[]{"NN"}); Defensive patterns
Strategy: validation
Validate before calling
for (String key : keys) {
if (key != null && AnnotationLookup.toCoreKey(key) == null) {
throw new IllegalArgumentException("Unrecognized annotation key: " + key);
}
} Try / catch
try {
return new CoreLabel(keys, values);
} catch (UnsupportedOperationException e) {
if (e.getMessage().startsWith("Unknown key")) {
log.warn("Skipping unknown key; available keys are AnnotationLookup entries");
return null; // or remap and retry
}
throw e;
} Prevention
- Use exact CoreNLP key names (Text, Word, PartOfSpeech, Lemma, NamedEntityTag, Index, ...).
- Maintain a mapping from your data format's column names to CoreNLP keys.
- Pin the CoreNLP version and check AnnotationLookup when upgrading, since key tables can change.
When it happens
Trigger: Constructing CoreLabel from strings with a key like "pos-tag" or a custom column name that is not a registered annotation key; typos in standard keys (e.g. "PartOfSpeach"); feeding serialized label maps from a different CoreNLP version with renamed keys.
Common situations: Custom TSV/CoNLL headers not matching CoreNLP's expected key names; migrating label data between library versions where annotation names changed; hand-written key arrays using lowercase or shorthand names.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Unexpected failure to instantiate - is your key class fancy?
- Argument array lengths differ
- CORE: CoreLabel.initFromStrings: Can't handle
- CORE: CoreLabel.initFromStrings: Bad type for
- Unknown annotation key:
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/fbb4466a327b1194.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/CoreLabel.java:188
public static final Map<Class<? extends GenericAnnotation<String>>, String> genericValues = Generics.newHashMap();
@SuppressWarnings({"unchecked", "rawtypes"})
private void initFromStrings(String[] keys, String[] values) {
if (keys.length != values.length) {
throw new UnsupportedOperationException("Argument array lengths differ: " +
Arrays.toString(keys) + " vs. " + Arrays.toString(values));
}
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
String value = values[i];
Class coreKeyClass = AnnotationLookup.toCoreKey(key);
//now work with the key we got above
if (coreKeyClass == null) {
if (key != null) {
throw new UnsupportedOperationException("Unknown key " + key);
}
} else {
try {
Class<?> valueClass = AnnotationLookup.getValueType(coreKeyClass);
if(valueClass.equals(String.class)) {
this.set(coreKeyClass, values[i]);
} else if(valueClass == Integer.class) {
this.set(coreKeyClass, Integer.parseInt(values[i]));
} else if(valueClass == Double.class) {
this.set(coreKeyClass, Double.parseDouble(values[i]));
} else if(valueClass == Long.class) {
this.set(coreKeyClass, Long.parseLong(values[i]));
} else if (valueClass == Boolean.class) {
this.set(coreKeyClass, Boolean.parseBoolean(values[i]));
} else if (coreKeyClass == CoreAnnotations.CoNLLUFeats.class) {
this.set(coreKeyClass, new CoNLLUFeatures(values[i]));
} else {
throw new UnsupportedOperationException("CORE: CoreLabel.initFromStrings: " +View on GitHub (pinned to 1b7edd19c4)