stanfordnlp/CoreNLP · error · IllegalArgumentException

Type mismatch on arg0: Cannot apply

Error message

Type mismatch on arg0: Cannot apply ${this} to ${in}

What it means

GET_ANNOTATION_VALUE requires arg0 to be a CoreMap or a List<CoreMap>; when the first argument is any other type (String, Number, null-backed value, etc.) this IllegalArgumentException is thrown after the annotation field lookup succeeded.

Solutions

  1. Ensure arg0 is a CoreMap (single annotation) or List<CoreMap> before applying the function.
  2. Wrap the target in the appropriate value: use a CoreMap expression referencing the matched annotation.
  3. Guard with instanceof checks on value.get() in custom code before invoking the function.

Example fix

// before
Value v = getFn.apply(env, Arrays.asList(Expressions.createValue("not-a-coremap"), field));
// after
CoreMap cm = sentence;
Value v = getFn.apply(env, Arrays.asList(new Expressions.PrimitiveValue<>("COREMAP", cm), field));
Defensive patterns

Strategy: type-guard

Validate before calling

Object v0 = args.get(0).get();
if (!(v0 instanceof CoreMap || v0 instanceof List)) throw new IllegalArgumentException("arg0 must be CoreMap or List<CoreMap>");

Type guard

boolean isCoreMapValue(Value v) {
    Object o = v != null ? v.get() : null;
    return o instanceof CoreMap || o instanceof List;
}

Try / catch

try {
    v = getFn.apply(env, args);
} catch (IllegalArgumentException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Type mismatch on arg0")) {
        v = null; // non-CoreMap input
    } else throw e;
}

Prevention

When it happens

Trigger: Calling GET_ANNOTATION_VALUE with a primitive/string as the first argument, or a Value whose get() is neither CoreMap nor List<CoreMap>, e.g. applying the function to a token's text instead of a CoreMap.

Common situations: Rules applying the annotation function to intermediate string values from earlier functions, pipeline stages producing non-CoreMap values, confusion between Tokens and CoreMaps.

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


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/1ce4f0f1d5cf1e0b. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/types/ValueFunctions.java:918

        Object obj = cm.get(annotationFieldClass);
        return Expressions.createValue(annotationFieldClass.getName(), obj);
      } else if (cmv.get() instanceof List) {
        List<CoreMap> cmList = (List<CoreMap>) cmv.get();
        if (in.size() >= 3) {
          Value v = in.get(2);
          Object annotationObject = (v != null)? v.get():null;
          for (CoreMap cm:cmList) {
            cm.set(annotationFieldClass, annotationObject);
          }
        }
        List<Object> list = new ArrayList<>();
        Value res = new Expressions.PrimitiveValue(Expressions.TYPE_LIST, list);
        for (CoreMap cm:cmList) {
          list.add(cm.get(annotationFieldClass));
        }
        return res;
      } else {
        throw new IllegalArgumentException("Type mismatch on arg0: Cannot apply " + this + " to " + in);
      }
    }
  };

  public static final ValueFunction GET_ANNOTATION_TAG_FUNCTION = new NamedValueFunction("GET_ANNOTATION_TAG") {
    @Override
    public String getParamDesc() {
      return "CoreMap or List<CoreMap>,String tag";
    }

    // First argument is what (CoreMap or List<CoreMap>) to tag
    // Second argument is tag
    @Override
    public boolean checkArgs(List<Value> in) {
      if (in.size() != 2) {
        return false;
      }
      if (in.get(0) == null || in.get(0).get() == null) return true; // Allow for NULL

View on GitHub (pinned to 1b7edd19c4)