stanfordnlp/CoreNLP · error · IllegalArgumentException

Cannot resolve annotation key " + annoKeyString

Error message

Cannot resolve annotation key " + annoKeyString

What it means

CleanXmlAnnotator accepts string configs (xmlTags, sentenceEndingTags, etc.) where each pattern's first part must name a CoreMap annotation key (e.g. "word", "lemma"). addAnnotationPatterns resolves that name via EnvLookup.lookupAnnotationKeyWithClassname; if the name is not a known annotation class/field it cannot map the config to an annotation Class and throws IllegalArgumentException.

Solutions

  1. Fix the annotation key string to a valid CoreAnnotations key (e.g. word, tag, lemma, ner).
  2. Verify the class name spelling and that any custom annotation class is fully qualified and on the classpath.
  3. Check the expected pattern format: "annotationKey,tagPattern[/attrPattern]" as split on comma.

Example fix

// before
annotatorProps.setProperty("clean.xmltokenannotationpatterns", "tok,.*");
// after
annotatorProps.setProperty("clean.xmltokenannotationpatterns", "word,.*");
Defensive patterns

Strategy: validation

Validate before calling

Class<?> key = EnvLookup.lookupAnnotationKeyWithClassname(null, annoKeyString);
if (key == null) throw new IllegalArgumentException("Unknown annotation key: " + annoKeyString);

Try / catch

try { annotator.setDocAnnotationPatterns(patterns); } catch (IllegalArgumentException e) { if (e.getMessage().startsWith("Cannot resolve annotation key")) { log.error("Fix the annotation key in CleanXml patterns: " + e.getMessage()); } else { throw e; } }

Prevention

When it happens

Trigger: Calling setDocAnnotationPatterns / setTokenAnnotationPatterns / setSectionAnnotationPatterns (or setting properties like clean.xmltokenannotationpatterns) with a pattern string whose annotation part is misspelled or not a valid CoreAnnotation class name, e.g. "tok,.*" instead of "word,.*".

Common situations: Typo in the annotation key portion of a comma-separated pattern; using a custom annotation whose class is not on the classpath or not registered; copying patterns from docs that use different key names across Stanford CoreNLP versions.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/pipeline/CleanXmlAnnotator.java:336

  public void setSectionAnnotationPatterns(String conf) {
    sectionAnnotationPatterns.clear();
    addAnnotationPatterns(sectionAnnotationPatterns, conf, false);
  }

  private static final Pattern TAG_ATTR_PATTERN = Pattern.compile("(.*)\\[(.*)\\]");

  private static void addAnnotationPatterns(CollectionValuedMap<Class, Pair<Pattern,Pattern>> annotationPatterns, String conf, boolean attrOnly) {
    String[] annoPatternStrings = conf == null ? StringUtils.EMPTY_STRING_ARRAY : conf.trim().split("\\s*,\\s*");
    for (String annoPatternString:annoPatternStrings) {
      String[] annoPattern = annoPatternString.split("\\s*=\\s*", 2);
      if (annoPattern.length != 2) {
        throw new IllegalArgumentException("Invalid annotation to tag pattern: " + annoPatternString);
      }
      String annoKeyString = annoPattern[0];
      String pattern = annoPattern[1];
      Class annoKey = EnvLookup.lookupAnnotationKeyWithClassname(null, annoKeyString);
      if (annoKey == null) {
        throw new IllegalArgumentException("Cannot resolve annotation key " + annoKeyString);
      }
      Matcher m = TAG_ATTR_PATTERN.matcher(pattern);
      if (m.matches()) {
        Pattern tagPattern = toCaseInsensitivePattern(m.group(1));
        Pattern attrPattern = toCaseInsensitivePattern(m.group(2));
        annotationPatterns.add(annoKey, Pair.makePair(tagPattern, attrPattern));
      } else {
        if (attrOnly) {
          // attribute is require
          throw new IllegalArgumentException("Invalid tag pattern: " + pattern + " for annotation key " + annoKeyString);
        } else {
          Pattern tagPattern = toCaseInsensitivePattern(pattern);
          annotationPatterns.add(annoKey, Pair.makePair(tagPattern, null));
        }
      }
    }
  }

View on GitHub (pinned to 1b7edd19c4)