stanfordnlp/CoreNLP · error · RuntimeException
Error -- attempt to operate with key of wrong length…
Error message
Error -- attempt to operate with key of wrong length. depth=depth
What it means
GeneralizedCounter tracks keys of a fixed depth; helper wrongDepth() throws when an operation is invoked with a key whose array length does not equal the counter's depth. The library throws this instead of silently returning 0 or misindexing the nested maps, because key length defines which nesting level is accessed.
Solutions
- Check the key array length against counter.depth() (or the constructor argument) before the call and reject/trim/pad keys to the exact depth
- Construct the GeneralizedCounter with the depth that matches your actual key arity
- Use the variable-depth getTotalCount()/incrementCount overloads or a ClassicCounter if key lengths genuinely vary
- Log the offending key and its length next to the expected depth to find where malformed keys are produced
Example fix
// before
String[] key = tokens; // variable length
counter.incrementCount(key, 1.0); // throws if key.length != depth
// after
if (key.length == counter.depth()) {
counter.incrementCount(key, 1.0);
} else {
throw new IllegalArgumentException("key depth " + key.length + " != " + counter.depth());
} Defensive patterns
Strategy: validation
Validate before calling
if (key == null || key.length != gc.depth()) {
throw new IllegalArgumentException("key length " + (key==null?-1:key.length) + " != depth " + gc.depth());
} Type guard
boolean isCorrectDepth(E[] key, GeneralizedCounter<E> gc) { return key != null && key.length == gc.depth(); } Try / catch
try {
gc.incrementCount(key, 1.0);
} catch (RuntimeException e) {
if (e.getMessage() != null && e.getMessage().startsWith("Error -- attempt to operate with key of wrong length")) {
throw new IllegalArgumentException("malformed key: " + Arrays.toString(key), e);
}
throw e;
} Prevention
- Always build keys with the same length as the counter depth
- Use the 1D/2D/3D helper methods that match your key arity
- Assert key.length == depth() in test fixtures
When it happens
Trigger: Calling getCount/getCounts/incrementCount/incrementCount2D/incrementCount3D/incrementCount1D with an Object[] or E[] key whose length != the depth the GeneralizedCounter was constructed with (e.g. a 2-key array on a depth-3 counter, or a depth-1 key on incrementCount3D).
Common situations: Mixing counters of different arity in one code path; building keys dynamically with variable-length lists (String.split results, n-gram windows of varying size); calling incrementCount1D/2D/3D helpers with keys of the wrong arity after changing the depth constructor argument.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Bad arguments: " + x + " and " + lambda
- conditionalLogProbGivenFirst requires of one less than…
- conditionalLogProbGivenNext requires given one less than…
- conditionalLogProbGivenPrevious requires given one less…
- conditionalLogProbsGivenPrevious requires given one less…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/32daa767ca4fc921.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/stats/GeneralizedCounter.java:595
}
return next.map.containsKey(key.get(key.size()-1));
}
public GeneralizedCounter<K> reverseKeys() {
GeneralizedCounter<K> result = new GeneralizedCounter<>();
Set<Map.Entry<List<K>,Double>> entries = entrySet();
for (Map.Entry<List<K>,Double> entry: entries) {
List<K> list = entry.getKey();
double count = entry.getValue();
Collections.reverse(list);
result.incrementCount(list, count);
}
return result;
}
private void wrongDepth() {
throw new RuntimeException("Error -- attempt to operate with key of wrong length. depth=" + depth);
}
/**
* Returns a read-only synchronous view (not a snapshot) of
* {@code this} as a {@link ClassicCounter}. Any calls to
* count-changing or entry-removing operations will result in an
* {@link UnsupportedOperationException}. At some point in the
* future, this view may gain limited writable functionality.
*/
public ClassicCounter<List<K>> counterView() {
return new CounterView();
}
private class CounterView extends ClassicCounter<List<K>> {
/**
*View on GitHub (pinned to 1b7edd19c4)