stanfordnlp/CoreNLP · error · RuntimeException

No roots in graph: + this + Find where this graph was…

Error message

No roots in graph:
 + this + 
Find where this graph was created and make sure you're adding roots.

What it means

getFirstRoot() throws a RuntimeException when the graph has no root vertices at all. A SemanticGraph is expected to always have at least one root; an empty root set means the graph was built incorrectly (vertices added without calling addRoot or from an empty/degenerate parse).

Solutions

  1. Ensure every graph-producing path calls addRoot for the root vertex (or uses a builder that does).
  2. Guard the call: check graph.isEmpty() / graph.getRoots().isEmpty() before getFirstRoot().
  3. If the sentence may be empty, skip processing rather than forcing a root lookup.

Example fix

// before
IndexedWord root = sg.getFirstRoot();
// after
if (sg.getRoots().isEmpty()) {
  return; // or handle empty graph
}
IndexedWord root = sg.getFirstRoot();
Defensive patterns

Strategy: validation

Validate before calling

if (sg == null || sg.isEmpty() || sg.getRoots().isEmpty()) { /* skip sentence */ return; }

Try / catch

try { IndexedWord root = sg.getFirstRoot(); } catch (RuntimeException e) { log.warn("graph without roots skipped"); }

Prevention

When it happens

Trigger: Calling getFirstRoot() on a SemanticGraph constructed manually with addVertex() but no addRoot(), on a graph deserialized from an empty annotation, or on a graph whose roots were never populated by the producing parser.

Common situations: Parsing empty or whitespace-only sentences; using Semgrex or dependency conversion pipelines that produce empty graphs; building SemanticGraphs programmatically and forgetting addRoot; sentence filtered so heavily that all roots were removed.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/semgraph/SemanticGraph.java:832

   *
   * @return A list of root nodes or an empty list.
   */
  private List<IndexedWord> getVerticesWithoutParents() {
    List<IndexedWord> result = new ArrayList<>();
    for (IndexedWord v : vertexSet()) {
      int inDegree = inDegree(v);
      if (inDegree == 0) {
        result.add(v);
      }
    }
    Collections.sort(result);
    return result;
  }

  /** Returns the (first) root of this SemanticGraph. */
  public IndexedWord getFirstRoot() {
    if (roots.isEmpty())
      throw new RuntimeException("No roots in graph:\n" + this
          + "\nFind where this graph was created and make sure you're adding roots.");
    return roots.iterator().next();
  }

  public void addRoot(IndexedWord root) {
    addVertex(root);
    roots.add(root);
  }

  /**
   * This method should not be used if possible. TODO: delete it
   *
   * Recomputes the roots, based of actual candidates. This is done to
   * ensure a rooted tree after a sequence of edits. If the none of the vertices
   * can act as a root (due to a cycle), keep old rootset, retaining only the
   * existing vertices on that list.
   *
   * TODO: this cannot deal with "Hamburg is a city which everyone likes", as

View on GitHub (pinned to 1b7edd19c4)