stanfordnlp/CoreNLP · error · UnsupportedOperationException
CORE: CoreLabel.initFromStrings: Can't handle
Error message
CORE: CoreLabel.initFromStrings: Can't handle <valueClass> (key <key>)
What it means
CoreLabel.initFromStrings throws UnsupportedOperationException when the key's declared value type is not one of the handled types (String, Integer, Long, Double, Boolean, or the special CoNLLUFeats case). The value string cannot be coerced into the annotation's type.
Solutions
- Only pass string keys whose value type is a scalar (String/Integer/Long/Double/Boolean) through the string constructor.
- Set complex annotations directly with label.set(CoreAnnotations.X.class, value) instead of via initFromStrings.
- Convert the complex value to a supported scalar representation before constructing.
- Check AnnotationLookup.getValueType(key) beforehand to know which types the constructor can handle.
Example fix
// before
new CoreLabel(new String[]{"Tokens"}, new String[]{"the|the"}); // value type is List, can't handle
// after
CoreLabel l = new CoreLabel(new String[]{"Text"}, new String[]{"the"});
l.set(CoreAnnotations.TokensAnnotation.class, tokenList); Defensive patterns
Strategy: validation
Validate before calling
Class<?> vc = AnnotationLookup.getValueType(AnnotationLookup.toCoreKey(key));
boolean scalar = vc.equals(String.class) || vc.equals(Integer.class)
|| vc.equals(Long.class) || vc.equals(Double.class) || vc.equals(Boolean.class);
if (!scalar) {
throw new IllegalArgumentException("Key " + key + " has non-scalar type " + vc + "; set it via label.set() instead");
} Try / catch
try {
return new CoreLabel(keys, values);
} catch (UnsupportedOperationException e) {
if (e.getMessage().contains("Can't handle")) {
// fall back to setting complex annotations individually
}
throw e;
} Prevention
- Check AnnotationLookup.getValueType before passing a key through the string constructor.
- Set complex-typed annotations directly with CoreLabel.set instead of string-based construction.
- Restrict serialized label formats to scalar annotation types.
When it happens
Trigger: Providing a key whose AnnotationLookup value type is e.g. a List or a Class (like CoreAnnotations.CoNLLUFeatures or other complex annotations) with a plain string value that initFromStrings has no conversion branch for.
Common situations: Feeding serialized labels containing complex annotations (lists of tokens, sentences, or machine-generated value types) back through the string-based CoreLabel constructor; custom annotations registered with non-scalar types.
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
- Unexpected failure to instantiate - is your key class fancy?
- Unknown key
- CORE: CoreLabel.initFromStrings: Bad type for
- CORE: CoreLabel.initFromStrings: Bad type for…
- Cannot process a single key/value from annotation as it is…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/4cd1f6cd17c08a3e.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/CoreLabel.java:206
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: " +
"Can't handle " + valueClass + " (key " + key + ")");
}
} catch (NumberFormatException e) {
// unexpected value type
throw new UnsupportedOperationException("CORE: CoreLabel.initFromStrings: "
+ "Bad type for " + key
+ ". Value was: " + value
+ "; expected "+AnnotationLookup.getValueType(coreKeyClass), e);
}
}
}
}
@SuppressWarnings("rawtypes")
public static Class[] parseStringKeys(String[] keys) {
Class[] classes = new Class[keys.length];
for (int i = 0; i < keys.length; i++) {
String key = keys[i];View on GitHub (pinned to 1b7edd19c4)