stanfordnlp/CoreNLP · error · RuntimeException
Cannot sum attribute " + key + ", object of type: " +…
Error message
Cannot sum attribute " + key + ", object of type: " + obj.getClass()
What it means
CoreMapAttributeAggregator's SUM aggregator iterates values for the attribute key and only knows how to convert Number and String values to doubles. Any other object type (or an unparseable case handled upstream) triggers a RuntimeException because summation is undefined for it.
Solutions
- Use an aggregator key whose values are numeric (Numbers or numeric Strings).
- Extract the numeric field yourself before aggregating (map values to Double first).
- Choose a different aggregation (e.g. a custom Aggregator) that handles the actual value type.
Example fix
// before Double total = aggregator.aggregate(coremaps, CoreAnnotations.TextAnnotation.class); // Text is String, ok, but a List value would fail // after Double total = coremaps.stream().map(m -> m.get(CoreAnnotations.OrdinalAnnotation.class)).filter(Objects::nonNull).mapToDouble(n -> ((Number) n).doubleValue()).sum();
Defensive patterns
Strategy: type-guard
Validate before calling
Object v = map.get(key);
if (v != null && !(v instanceof Number) && !(v instanceof String)) throw new IllegalStateException("Non-numeric value under key"); Type guard
static boolean isSummable(Object o) { return o instanceof Number || (o instanceof String && isNumeric((String) o)); } Try / catch
try { sum = aggregator.aggregate(maps, key); } catch (RuntimeException e) { if (e.getMessage().startsWith("Cannot sum attribute")) { sum = manualNumericSum(maps, key); } else { throw e; } } Prevention
- Only attach Number or numeric-String values to attributes you plan to sum.
- Unwrap lists/custom objects into scalars before aggregating.
- Document which keys are aggregation-safe in your pipeline code.
When it happens
Trigger: Calling aggregate with the SUM aggregator over a CoreMap attribute whose stored value is neither Number nor String, e.g. a List<Integer>, Boolean, or custom object stored under that key.
Common situations: Aggregating tokens/sentences where a key holds composite values (lists of spans, Trees, CoreLabels); expecting aggregation to descend into lists which it does not.
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
- Cannot get max of attribute " + key + ", object of type: "…
- Cannot get min of attribute " + key + ", object of type: "…
- CoreLabels required!
- Expected parsers with DVModel embedded
- This parser does not contain a DVModel reranker
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/4979a6f9e387bcd2.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/pipeline/CoreMapAttributeAggregator.java:182
public static final CoreMapAttributeAggregator CONCAT_TEXT = new ConcatTextAggregator(" ");
public static final CoreMapAttributeAggregator COUNT = new CoreMapAttributeAggregator() {
public Object aggregate(Class key, List<? extends CoreMap> in) {
return in.size();
}
};
public static final CoreMapAttributeAggregator SUM = new CoreMapAttributeAggregator() {
public Object aggregate(Class key, List<? extends CoreMap> in) {
if (in == null) return null;
double sum = 0;
for (CoreMap cm:in) {
Object obj = cm.get(key);
if (obj != null) {
if (obj instanceof Number) {
sum += ((Number) obj).doubleValue();
} else if (obj instanceof String) {
sum += Double.parseDouble((String) obj);
} else {
throw new RuntimeException("Cannot sum attribute " + key + ", object of type: " + obj.getClass());
}
}
}
return sum;
}
};
public static final CoreMapAttributeAggregator MIN = new CoreMapAttributeAggregator() {
public Object aggregate(Class key, List<? extends CoreMap> in) {
if (in == null) return null;
Comparable min = null;
for (CoreMap cm:in) {
Object obj = cm.get(key);
if (obj != null) {
if (obj instanceof Comparable) {
Comparable c = (Comparable) obj;
if (min == null) {
min = c;
} else if (c.compareTo(min) < 0) {View on GitHub (pinned to 1b7edd19c4)