stanfordnlp/CoreNLP · error · IllegalArgumentException

Cannot get annotation field

Error message

Cannot get annotation field ${field}

What it means

The annotation-value function (GET_ANNOTATION_VALUE style) resolves arg1 to a CoreMap annotation key class; when arg1 is a String that EnvLookup.lookupAnnotationKey cannot resolve to a registered annotation key class, this IllegalArgumentException is thrown. It means the named annotation field is unknown in the current environment.

Solutions

  1. Correct the annotation field name string to a registered key (e.g. CoreAnnotations.TextAnnotation -> "text").
  2. Register the custom annotation key in the Env (env.registerAnnotationKey or equivalent) before evaluating.
  3. Pass the Class object directly as arg1 (field instanceof Class branch) instead of a String.

Example fix

// before
Value v = getFn.apply(env, Arrays.asList(cmValue, Expressions.createValue("Word")));
// after
Value v = getFn.apply(env, Arrays.asList(cmValue, Expressions.createValue(CoreAnnotations.TextAnnotation.class)));
Defensive patterns

Strategy: validation

Validate before calling

if (EnvLookup.lookupAnnotationKey(env, fieldName) == null) {
    throw new IllegalArgumentException("Unknown annotation key: " + fieldName);
}

Type guard

boolean isResolvableField(Env env, Object field) {
    return field instanceof Class || (field instanceof String && EnvLookup.lookupAnnotationKey(env, (String) field) != null);
}

Try / catch

try {
    v = getFn.apply(env, args);
} catch (IllegalArgumentException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Cannot get annotation field")) {
        v = null; // unknown field: treat as missing
    } else throw e;
}

Prevention

When it happens

Trigger: GET_ANNOTATION_VALUE(coremap, "SomeField") where "SomeField" is not a registered annotation key (no matching class in Env's annotation key mapping), case mismatches, or custom annotation keys never registered via env.

Common situations: Typo'd field names in TokensRegex rules, using custom Annotation classes without registering them in the environment, relying on fields from a different CoreNLP version/annotator that hasn't run.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

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

      if (in.get(0) == null ||
              (!(in.get(0).get() instanceof CoreMap) && !(in.get(0).get() instanceof List))) {
        return false;
      }
      if (in.get(1) == null ||
              (!(in.get(1).get() instanceof Class) && !(in.get(1).get() instanceof String))) {
        return false;
      }
      return true;
    }
    @Override
    public Value apply(Env env, List<Value> in) {
      Value cmv = in.get(0);
      Object field = in.get(1).get();
      Class annotationFieldClass = null;
      if (field instanceof String)  {
        annotationFieldClass = EnvLookup.lookupAnnotationKey(env, (String) field);
        if (annotationFieldClass == null) {
          throw new IllegalArgumentException("Cannot get annotation field " + field);
        }
      } else if (field instanceof Class) {
        annotationFieldClass = (Class)  field;
      } else {
        throw new IllegalArgumentException("Type mismatch on arg1: Cannot apply " + this + " to " + in);
      }
      if (cmv.get() instanceof CoreMap) {
        CoreMap cm = (CoreMap) cmv.get();
        if (in.size() >= 3) {
          Value v = in.get(2);
          Object annotationObject = (v != null)? v.get():null;
          cm.set(annotationFieldClass, annotationObject);
        }
        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) {

View on GitHub (pinned to 1b7edd19c4)