stanfordnlp/CoreNLP · error · RuntimeException

Shouldn't happen:

Error message

Shouldn't happen:  

What it means

CorefMentionFinder.findPartialSpan walks a parse tree looking for the node whose index span covers the requested start position. If no child of the root contains the start index, it throws RuntimeException("Shouldn't happen: " + start + " " + root), signaling a broken assumption about the tree structure rather than an expected failure.

Solutions

  1. Check the parse tree passed to coref is well-formed and its leaf index annotations cover all token positions
  2. Verify the mention start index passed in comes from the same tokenization/parse as the tree (no off-by-one or stale offsets)
  3. Update the CoreNLP version — some tree/annotation alignment bugs have been patched
  4. If it occurs on malformed input, catch RuntimeException around coref postprocessing and skip the document

Example fix

// before
throw new RuntimeException("Shouldn't happen: " + start + " " + root);
// after
// fix root cause: ensure tree children cover 'start'; optionally log state
throw new RuntimeException("No child of tree covers start index " + start + "; tree: " + root);
Defensive patterns

Strategy: try-catch

Validate before calling

// validate tokens/parse alignment before coref
for (CoreMap sent : doc.get(CoreAnnotations.SentencesAnnotation.class)) {
  Tree t = sent.get(TreeCoreAnnotations.TreeAnnotation.class);
  int nLeaves = t.getLeaves().size();
  int nTokens = sent.get(CoreAnnotations.TokensAnnotation.class).size();
  if (nLeaves != nTokens) throw new IllegalStateException("parse/token mismatch");
}

Type guard

static boolean covers(Tree root, int start) {
  for (Tree kid : root.getChildrenAsList()) {
    Integer b = kid.label().get(CoreAnnotations.BeginIndexAnnotation.class);
    Integer e = kid.label().get(CoreAnnotations.EndIndexAnnotation.class);
    if (b != null && e != null && b <= start && start < e) return true;
  }
  return false;
}

Try / catch

try {
  runCoref(doc);
} catch (RuntimeException e) {
  if (e.getMessage() != null && e.getMessage().startsWith("Shouldn't happen:")) {
    log.warn("Skipping malformed parse for coref: " + e.getMessage());
  } else throw e;
}

Prevention

When it happens

Trigger: findPartialSpan(root, start) is called with a start offset that is not contained within any child node's [BeginIndexAnnotation, EndIndexAnnotation) span — i.e. the root tree's children do not cover the token index passed in.

Common situations: Corrupted or unusually-shaped constituency parses fed to coref (e.g. from a malformed parser model or custom annotator output); token offsets not aligned with tree leaf positions after pre-processing or sentence-splitting changes.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/coref/md/CorefMentionFinder.java:596

    return endLeaf;
  }

  /** Find the tree that covers the portion of interest. */
  private static Tree findPartialSpan(final Tree root, final int start) {
    CoreLabel label = (CoreLabel) root.label();
    int startIndex = label.get(CoreAnnotations.BeginIndexAnnotation.class);
    if (startIndex == start) {
      return root;
    }
    for (Tree kid : root.children()) {
      CoreLabel kidLabel = (CoreLabel) kid.label();
      int kidStart = kidLabel.get(CoreAnnotations.BeginIndexAnnotation.class);
      int kidEnd = kidLabel.get(CoreAnnotations.EndIndexAnnotation.class);
      if (kidStart <= start && kidEnd > start) {
        return findPartialSpan(kid, start);
      }
    }
    throw new RuntimeException("Shouldn't happen: " + start + " " + root);
  }

  private static Tree funkyFindLeafWithApproximateSpan(Tree root, String token, int index, int approximateness) {
    // log.info("Searching " + root + "\n  for " + token + " at position " + index + " (plus up to " + approximateness + ")");
    List<Tree> leaves = root.getLeaves();
    for (Tree leaf : leaves) {
      CoreLabel label = CoreLabel.class.cast(leaf.label());
      Integer indexInteger = label.get(CoreAnnotations.IndexAnnotation.class);
      if (indexInteger == null) continue;
      int ind = indexInteger - 1;
      if (token.equals(leaf.value()) && ind >= index && ind <= index + approximateness) {
        return leaf;
      }
    }
    // this shouldn't happen
    //    throw new RuntimeException("RuleBasedCorefMentionFinder: ERROR: Failed to find head token");
    Redwood.log("RuleBasedCorefMentionFinder: Failed to find head token:\n" +
        "Tree is: " + root + "\n" +

View on GitHub (pinned to 1b7edd19c4)