stanfordnlp/CoreNLP · error · RuntimeException
Bad pattern
Error message
Bad pattern: <pattern>
What it means
During GrammaticalRelation construction, each target pattern string is compiled with the Tregex compiler. If a pattern is not valid Tregex syntax, TregexParseException is caught and rethrown as RuntimeException('Bad pattern: ' + pattern, pe). This means one of the relation's target tregex expressions cannot be parsed.
Solutions
- Fix the tregex pattern syntax reported in the message; verify against the Tregex grammar for your library version
- Quote regex label values inside patterns (e.g. /^NN/ usage rules) and balance all brackets/angle brackets
- Compile the pattern standalone with a TregexPatternCompiler to get the full TregexParseException detail (also in the cause chain)
- Check release notes for tregex syntax changes if the relation worked on an older CoreNLP version
Example fix
// before
targetPatterns = new String[]{ "VP < (NP $-- ADJP" }; // unbalanced paren
// after
targetPatterns = new String[]{ "VP < (NP $-- ADJP)" }; Defensive patterns
Strategy: validation
Validate before calling
TregexPatternCompiler compiler = new TregexPatternCompiler(headFinder);
for (String p : targetPatterns) {
try { compiler.compile(p); } catch (TregexParseException e) {
throw new IllegalArgumentException("Invalid tregex: " + p, e);
}
} Try / catch
try {
GrammaticalRelation r = new GrammaticalRelation(lang, name, longName, src, Arrays.asList(targetPatterns), ...);
} catch (RuntimeException e) {
if (e.getMessage().startsWith("Bad pattern:")) {
TregexParseException cause = (TregexParseException) e.getCause(); // inspect exact syntax error
} else throw e;
} Prevention
- Pre-compile all tregex patterns with TregexPatternCompiler in unit tests
- Keep bracket/paren balance by formatting patterns across multiple lines
- Quote regex labels per tregex rules
- Re-run pattern tests when upgrading CoreNLP versions
When it happens
Trigger: Constructing a GrammaticalRelation whose targetPatterns list contains a string that fails Tregex parsing (bad node descriptions, unbalanced brackets, unsupported operators, missing quotes around regex labels).
Common situations: Writing custom relation definitions with hand-written tregex; version changes where tregex grammar became stricter; typos such as unbalanced '>' or '[ ]' in patterns like 'NP < NN'.
Related errors
- Bad pattern
- Coordination node must have at least 2 children.
- Expected lines of the format original (tab) replacement. …
- Node cannot be both negated and optional.
- Unknown multi relation
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/f9c3318e2f7a942f.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/GrammaticalRelation.java:307
parent.addChild(this);
}
if (sourcePattern != null) {
try {
this.sourcePattern = Pattern.compile(sourcePattern);
} catch (java.util.regex.PatternSyntaxException e) {
throw new RuntimeException("Bad pattern: " + sourcePattern);
}
} else {
this.sourcePattern = null;
}
for (String pattern : targetPatterns) {
try {
TregexPattern p = tregexCompiler.compile(pattern);
this.targetPatterns.add(p);
} catch (edu.stanford.nlp.trees.tregex.TregexParseException pe) {
throw new RuntimeException("Bad pattern: " + pattern, pe);
}
}
GrammaticalRelation previous;
synchronized (stringsToRelations) {
Map<String, GrammaticalRelation> sToR = stringsToRelations.get(language);
if (sToR == null) {
sToR = Generics.newHashMap();
stringsToRelations.put(language, sToR);
}
previous = sToR.put(toString(), this);
}
if (previous != null) {
if ( ! previous.isFromString() && ! isFromString()) {
throw new IllegalArgumentException("There is already a relation named " + this + '!');
} else {
/* We get here if we previously just built a fake relation from a string
* we previously read in from a file.View on GitHub (pinned to 1b7edd19c4)