stanfordnlp/CoreNLP · error · RuntimeException
Error: Attempted to run Tregex on sentence without a…
Error message
Error: Attempted to run Tregex on sentence without a constituency parse. To use this method you must annotate the document with a constituency parse using the 'parse' annotator.
What it means
CoreSentence.tregexResultTrees(TregexPattern) needs the sentence's constituency parse tree; tregex operates on Tree objects. If constituencyParse() returns null (the 'parse' annotator was not run) a RuntimeException is thrown explaining that the 'parse' annotator is required.
Solutions
- Add the 'parse' annotator to the pipeline's annotators list.
- If constituency parsing is too slow, configure a fast constituency model or accept depparse and rewrite queries against dependency output.
- Guard calls with sentence.constituencyParse() != null before running Tregex.
- Use CoreMapExpressionExtractor / dependency-based alternatives if only dependency structure is needed.
Example fix
// before
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner");
Tree t = sentence.tregexResultTrees("NP < NN").get(0);
// after
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse");
Tree t = sentence.tregexResultTrees("NP < NN").get(0); Defensive patterns
Strategy: type-guard
Validate before calling
if (sentence.constituencyParse() == null) { throw new IllegalStateException("Run pipeline with the 'parse' annotator before Tregex queries"); } Type guard
Optional<Tree> parse = Optional.ofNullable(sentence.constituencyParse()); parse.ifPresent(t -> runTregex(t));
Try / catch
try { trees = sentence.tregexResultTrees(pattern); } catch (RuntimeException e) { if (e.getMessage().startsWith("Error: Attempted to run Tregex")) { trees = Collections.emptyList(); log.warn("No constituency parse; skipping Tregex"); } else { throw e; } } Prevention
- Always include 'parse' in annotators when using Tregex on CoreSentences.
- Check constituencyParse() != null before tregex calls.
- Prefer depparse + dependency queries when constituency parsing is too expensive.
When it happens
Trigger: Calling sentence.tregexResultTrees(pattern) (or the String-pattern overload, and helpers like sentenceParseMatches) on a pipeline that lacks the 'parse' (or a constituency-providing) annotator in its annotator list.
Common situations: Building a pipeline with only tokenize/ssplit/pos/lemma/ner and then running Tregex queries; switching from a dependency-only (depparse) pipeline to constituency-based code without adding 'parse'; memory-constrained setups where 'parse' was removed.
Related errors
- Error: cannot process tregex operations with no…
- format error in embeddings
- format error unexpected featureFactory line:
- Need to supply a parser model with -model
- Need to supply an output filename with -output
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/72015921b0d4487d.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/pipeline/CoreSentence.java:105
/** list of ner tags **/
public List<String> nerTags() { return tokens().stream().map(token -> token.ner()).collect(Collectors.toList()); }
/** constituency parse **/
public Tree constituencyParse() {
return sentenceCoreMap.get(TreeCoreAnnotations.TreeAnnotation.class);
}
/** Tregex - find subtrees of interest with a general Tregex pattern **/
public List<Tree> tregexResultTrees(String s) {
// the patterns are cached by computeIfAbsent, so we don't wastefully recompile a TregexPattern every sentence
return tregexResultTrees(patternCache.computeIfAbsent(s, compilePattern));
}
public List<Tree> tregexResultTrees(TregexPattern p) {
// throw a RuntimeException if no constituency parse available to signal to user to use "parse" annotator
if (constituencyParse() == null)
throw new RuntimeException("Error: Attempted to run Tregex on sentence without a constituency parse. " +
"To use this method you must annotate the document with a constituency parse using the 'parse' " +
"annotator.");
List<Tree> results = new ArrayList<>();
TregexMatcher matcher = p.matcher(constituencyParse());
while (matcher.find()) {
results.add(matcher.getMatch());
}
return results;
}
public List<String> tregexResults(TregexPattern p) {
return tregexResultTrees(p).stream().map(treeToSpanString).collect(Collectors.toList());
}
public List<String> tregexResults(String s) {
return tregexResultTrees(s).stream().map(treeToSpanString).collect(Collectors.toList());
}
View on GitHub (pinned to 1b7edd19c4)