stanfordnlp/CoreNLP · error · IllegalArgumentException
Invalid tag pattern: " + pattern + " for annotation key " +…
Error message
Invalid tag pattern: " + pattern + " for annotation key " + annoKeyString
What it means
After resolving the annotation key, the pattern part of the config must match TAG_ATTR_PATTERN (tag or tag/attribute). When it does not match and attrOnly is set (attribute required), addAnnotationPatterns throws IllegalArgumentException because it cannot build a tag/attribute matcher.
Solutions
- Rewrite the pattern to the expected form "annotationKey,tagRegex[/attrRegex]" so it matches TAG_ATTR_PATTERN.
- If you only need attribute matching, supply a valid attribute specification with a non-empty tag part.
- Escape regex metacharacters so the tag portion parses as a single tag pattern.
Example fix
// before
props.setProperty("clean.xmltokenannotationpatterns", "word,/speaker=*");
// after
props.setProperty("clean.xmltokenannotationpatterns", "word, speaker|.*"); Defensive patterns
Strategy: validation
Validate before calling
String[] parts = patternSpec.split(",", 2);
if (parts.length < 2 || !parts[1].matches("[^/]+(/[^/]*)?")) throw new IllegalArgumentException("Bad CleanXml pattern: " + patternSpec); Try / catch
try { annotator.setTokenAnnotationPatterns(specs); } catch (IllegalArgumentException e) { if (e.getMessage().startsWith("Invalid tag pattern")) { log.error("Malformed tag pattern: " + e.getMessage()); } else { throw e; } } Prevention
- Follow the documented format annotationKey,tagRegex[/attrRegex] exactly.
- Escape regex metacharacters in tag names before embedding them in patterns.
- Test each pattern against sample XML in a unit test.
When it happens
Trigger: Calling setDocAnnotationPatterns/setTokenAnnotationPatterns/setSectionAnnotationPatterns with a pattern string whose tag portion fails TAG_ATTR_PATTERN when an attribute is required (attrOnly mode), e.g. a malformed "word,/type=x" style pattern or empty/invalid tag regex.
Common situations: Misplaced separators in the comma-separated config; attribute-only patterns supplied where the tag part is empty; regexes containing characters that break the expected tag[/attr] structure.
Related errors
- Invalid annotation to tag pattern: " + annoPatternString
- Cannot resolve annotation key " + annoKeyString
- Got a close tag </" + tag.name + "> which does not match…
- Mismatched tags: </" + tag.name + "> closed a <" + lastTag…
- format error in embeddings
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/8090f50b6c43ceb0.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/pipeline/CleanXmlAnnotator.java:346
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));
}
}
}
}
/**
* Helper method to set the TokenBeginAnnotation and TokenEndAnnotation of every token.
*/
public void setTokenBeginTokenEnd(List<CoreLabel> tokensList) {
int tokenIndex = 0;
for (CoreLabel token : tokensList) {
token.set(CoreAnnotations.TokenBeginAnnotation.class, tokenIndex);
token.set(CoreAnnotations.TokenEndAnnotation.class, tokenIndex+1);
tokenIndex++;
}View on GitHub (pinned to 1b7edd19c4)