{"record":{"id":"72015921b0d4487d","repo":"stanfordnlp/CoreNLP","slug":"error-attempted-to-run-tregex-on-sentence-without","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/pipeline/CoreSentence.java","lineNumber":105,"sourceCode":"\n  /** list of ner tags **/\n  public List<String> nerTags() { return tokens().stream().map(token -> token.ner()).collect(Collectors.toList()); }\n\n  /** constituency parse **/\n  public Tree constituencyParse() {\n    return sentenceCoreMap.get(TreeCoreAnnotations.TreeAnnotation.class);\n  }\n\n  /** Tregex - find subtrees of interest with a general Tregex pattern **/\n  public List<Tree> tregexResultTrees(String s) {\n    // the patterns are cached by computeIfAbsent, so we don't wastefully recompile a TregexPattern every sentence\n    return tregexResultTrees(patternCache.computeIfAbsent(s, compilePattern));\n  }\n\n  public List<Tree> tregexResultTrees(TregexPattern p) {\n    // throw a RuntimeException if no constituency parse available to signal to user to use \"parse\" annotator\n    if (constituencyParse() == null)\n      throw new RuntimeException(\"Error: Attempted to run Tregex on sentence without a constituency parse.  \" +\n              \"To use this method you must annotate the document with a constituency parse using the 'parse' \" +\n              \"annotator.\");\n    List<Tree> results = new ArrayList<>();\n    TregexMatcher matcher = p.matcher(constituencyParse());\n    while (matcher.find()) {\n      results.add(matcher.getMatch());\n    }\n    return results;\n  }\n\n  public List<String> tregexResults(TregexPattern p) {\n    return tregexResultTrees(p).stream().map(treeToSpanString).collect(Collectors.toList());\n  }\n\n  public List<String> tregexResults(String s) {\n    return tregexResultTrees(s).stream().map(treeToSpanString).collect(Collectors.toList());\n  }\n","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/pipeline/CoreSentence.java#L87-L123","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nprops.setProperty(\"annotators\", \"tokenize,ssplit,pos,lemma,ner\");\nTree t = sentence.tregexResultTrees(\"NP < NN\").get(0);\n// after\nprops.setProperty(\"annotators\", \"tokenize,ssplit,pos,lemma,ner,parse\");\nTree t = sentence.tregexResultTrees(\"NP < NN\").get(0);","handlingStrategy":"type-guard","validationCode":"if (sentence.constituencyParse() == null) { throw new IllegalStateException(\"Run pipeline with the 'parse' annotator before Tregex queries\"); }","typeGuard":"Optional<Tree> parse = Optional.ofNullable(sentence.constituencyParse());\nparse.ifPresent(t -> runTregex(t));","tryCatchPattern":"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; } }","preventionTips":["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."],"tags":["stanford-corenlp","tregex","constituency-parse","missing-annotator"],"backgroundTag":"missing-required-dependency","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}