stanfordnlp/CoreNLP · error · IllegalArgumentException

Invalid txtGraph

Error message

Invalid txtGraph 

What it means

Alignment.makeFromIndexArray throws IllegalArgumentException("Invalid txtGraph " + txtGraph) when the source (text) SemanticGraph is null or empty. An alignment requires a non-empty text graph to map hyp words onto.

Solutions

  1. Ensure the text graph was built successfully and sg.size() > 0 before creating an Alignment
  2. Skip empty sentences upstream instead of passing empty graphs
  3. Check for null return from the graph-building factory call

Example fix

// before
Alignment a = Alignment.makeFromIndexArray(txtGraph, hypGraph, idx, 1.0, "j");
// after
if (txtGraph != null && txtGraph.size() > 0) {
  Alignment a = Alignment.makeFromIndexArray(txtGraph, hypGraph, idx, 1.0, "j");
}
Defensive patterns

Strategy: validation

Validate before calling

if (txtGraph != null && txtGraph.size() > 0 && hypGraph != null && hypGraph.size() > 0) { Alignment a = Alignment.makeFromIndexArray(txtGraph, hypGraph, indexes, score, just); }

Type guard

boolean validGraph = g != null && !g.isEmpty() && g.size() > 0;

Try / catch

try { a = Alignment.makeFromIndexArray(txt, hyp, idx, score, just); } catch (IllegalArgumentException e) { log.warn("Alignment skipped: " + e.getMessage()); }

Prevention

When it happens

Trigger: Calling makeFromIndexArray with txtGraph == null, or with a SemanticGraph of size 0 (no vertices) — e.g. a graph built from an empty/degenerate parse.

Common situations: Pipelines that skip sentence construction for empty inputs and pass an unpopulated graph downstream; null graph propagation from a failed earlier parse step.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/semgraph/semgrex/Alignment.java:160

    return new Alignment(patchedMap, score, justification);
  }


  /**
   * Constructs and returns a new Alignment from the given hypothesis
   * {@code SemanticGraph} to the given text (passage) SemanticGraph, using
   * the given array of indexes.  The i'th node of the array should contain the
   * index of the node in the text (passage) SemanticGraph to which the i'th
   * node in the hypothesis SemanticGraph is aligned, or -1 if it is aligned to
   * NO_WORD.
   */
  public static Alignment makeFromIndexArray(SemanticGraph txtGraph,
                                             SemanticGraph hypGraph,
                                             int[] indexes,
                                             double score,
                                             String justification) {
    if (txtGraph == null || txtGraph.isEmpty())
      throw new IllegalArgumentException("Invalid txtGraph " + txtGraph);
    if (hypGraph == null || hypGraph.isEmpty())
      throw new IllegalArgumentException("Invalid hypGraph " + hypGraph);
    if (indexes == null)
      throw new IllegalArgumentException("Null index array");
    if (indexes.length != hypGraph.size())
      throw new IllegalArgumentException("Index array length " + indexes.length +
                                         " does not match hypGraph size " + hypGraph.size());
    Map<IndexedWord, IndexedWord> map =
      Generics.newHashMap();
    for (int i = 0; i < indexes.length; i++) {
      IndexedWord hypNode = hypGraph.getNodeByIndex(i);
      IndexedWord txtNode = IndexedWord.NO_WORD;
      if (indexes[i] >= 0)
        txtNode = txtGraph.getNodeByIndex(indexes[i]);
      map.put(hypNode, txtNode);
    }
    return new Alignment(map, score, justification);
  }

View on GitHub (pinned to 1b7edd19c4)