stanfordnlp/CoreNLP · error · UnsupportedOperationException
Argument array lengths differ
Error message
Argument array lengths differ: <keys> vs. <values>
What it means
CoreLabel.initFromStrings throws UnsupportedOperationException when the keys and values string arrays passed to the constructor have different lengths. The label format requires a 1:1 pairing of annotation keys and values.
Solutions
- Verify the keys and values arrays come from the same split and have equal length before constructing.
- Normalize the input line's delimiter (e.g. split on \t consistently, trim trailing separators).
- Pad or trim one array to match the other only if the format genuinely allows missing columns.
- Log both Arrays.toString(keys) and Arrays.toString(values) to find which column is missing/extra.
Example fix
// before
String[] parts = line.split("\\t");
new CoreLabel(keys, parts); // lengths may differ
// after
String[] parts = line.split("\\t", -1);
if (keys.length != parts.length)
throw new IllegalArgumentException("Expected " + keys.length + " columns, got " + parts.length + ": " + line);
new CoreLabel(keys, parts); Defensive patterns
Strategy: validation
Validate before calling
if (keys == null || values == null || keys.length != values.length) {
throw new IllegalArgumentException("keys and values must be non-null and equal length");
}
new CoreLabel(keys, values); Try / catch
try {
return new CoreLabel(keys, values);
} catch (UnsupportedOperationException e) {
if (e.getMessage().startsWith("Argument array lengths differ")) {
throw new IllegalArgumentException("Malformed label record: " + e.getMessage(), e);
}
throw e;
} Prevention
- Split TSV lines with split("\\t", -1) and validate the column count against the header.
- Derive keys and values from the same single split operation.
- Unit-test the loader with lines containing missing and extra columns.
When it happens
Trigger: Calling new CoreLabel(keys, values) (or the constructor variants that delegate to initFromStrings) with keys.length != values.length, e.g. malformed CoNLL/TSV line splits where a column was dropped or an extra tab added.
Common situations: Loading labels from CSV/TSV files with inconsistent column counts; splitting a line on a wrong delimiter so a value containing the delimiter shifts columns; constructing labels programmatically with partially filled arrays.
Understand the failure class
Background: "must be a positive integer", "cannot be empty", "invalid argument": how invalid-argument errors work across open-source libraries — this error's family across 33 libraries.
Related errors
- Unknown key
- CORE: CoreLabel.initFromStrings: Bad type for
- Bad data format:
- Cannot find matching labelled span for
- ERROR: typed SINGLETON feature.
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/a59ccf58e82a5d93.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/CoreLabel.java:177
cl.setValue(word);
return cl;
}
/**
* Class that all "generic" annotations extend.
* This allows you to read in arbitrary values from a file as features, for example.
*/
public interface GenericAnnotation<T> extends CoreAnnotation<T> { }
public static final Map<String, Class<? extends GenericAnnotation<String>>> genericKeys = Generics.newHashMap();
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) {View on GitHub (pinned to 1b7edd19c4)