stanfordnlp/CoreNLP · error · IllegalArgumentException

Invalid annotation to tag pattern: " + annoPatternString

Error message

Invalid annotation to tag pattern: " + annoPatternString

What it means

CleanXmlAnnotator's addAnnotationPatterns parses configuration strings of the form 'AnnotationKey=tagPattern' (comma separated). Each entry must contain a '='; if splitting on '=' yields fewer than two parts, it throws IllegalArgumentException('Invalid annotation to tag pattern: ...').

Solutions

  1. Write each pattern as 'AnnotationKey=regex', e.g. 'doc=<doc>' or 'sentence=<p>'
  2. Check that regexes don't contain commas that split the conf incorrectly (escape or restructure)
  3. Verify the annotation key part resolves to a known CoreAnnotations class (next error otherwise is 'Cannot resolve annotation key')

Example fix

// before
props.setProperty("clean.xmlannotate.docs", "doc");
// after
props.setProperty("clean.xmlannotate.docs", "doc=<doc>");
Defensive patterns

Strategy: validation

Validate before calling

for (String entry : conf.split("\\s*,\\s*")) {
  if (!entry.contains("=")) throw new IllegalArgumentException("Invalid annotation to tag pattern: " + entry);
}

Try / catch

try {
  props.setProperty("clean.xmlannotate.docs", conf);
  new CleanXmlAnnotator(props);
} catch (IllegalArgumentException e) {
  if (e.getMessage().startsWith("Invalid annotation to tag pattern")) {
    // fix conf to 'key=pattern' form
  } else throw e;
}

Prevention

When it happens

Trigger: Setting clean.xmlannotate.docs / .tokens / .sections style properties with a pattern string missing the 'key=value' separator, e.g. 'doc' instead of 'doc=doc'.

Common situations: Hand-edited CleanXML properties; examples copied incompletely; commas used inside a regex splitting the conf into malformed fragments.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

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

  public void setTokenAnnotationPatterns(String conf) {
    tokenAnnotationPatterns.clear();
    // Patterns can only be tag attributes
    addAnnotationPatterns(tokenAnnotationPatterns, conf, true);
  }

  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);

View on GitHub (pinned to 1b7edd19c4)