{"record":{"id":"e3dd5ba462e7621c","repo":"stanfordnlp/CoreNLP","slug":"shouldn-t-happen","errorCode":null,"errorMessage":"Shouldn't happen:  ","messagePattern":"Shouldn't happen:  ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/coref/md/CorefMentionFinder.java","lineNumber":596,"sourceCode":"    return endLeaf;\n  }\n\n  /** Find the tree that covers the portion of interest. */\n  private static Tree findPartialSpan(final Tree root, final int start) {\n    CoreLabel label = (CoreLabel) root.label();\n    int startIndex = label.get(CoreAnnotations.BeginIndexAnnotation.class);\n    if (startIndex == start) {\n      return root;\n    }\n    for (Tree kid : root.children()) {\n      CoreLabel kidLabel = (CoreLabel) kid.label();\n      int kidStart = kidLabel.get(CoreAnnotations.BeginIndexAnnotation.class);\n      int kidEnd = kidLabel.get(CoreAnnotations.EndIndexAnnotation.class);\n      if (kidStart <= start && kidEnd > start) {\n        return findPartialSpan(kid, start);\n      }\n    }\n    throw new RuntimeException(\"Shouldn't happen: \" + start + \" \" + root);\n  }\n\n  private static Tree funkyFindLeafWithApproximateSpan(Tree root, String token, int index, int approximateness) {\n    // log.info(\"Searching \" + root + \"\\n  for \" + token + \" at position \" + index + \" (plus up to \" + approximateness + \")\");\n    List<Tree> leaves = root.getLeaves();\n    for (Tree leaf : leaves) {\n      CoreLabel label = CoreLabel.class.cast(leaf.label());\n      Integer indexInteger = label.get(CoreAnnotations.IndexAnnotation.class);\n      if (indexInteger == null) continue;\n      int ind = indexInteger - 1;\n      if (token.equals(leaf.value()) && ind >= index && ind <= index + approximateness) {\n        return leaf;\n      }\n    }\n    // this shouldn't happen\n    //    throw new RuntimeException(\"RuleBasedCorefMentionFinder: ERROR: Failed to find head token\");\n    Redwood.log(\"RuleBasedCorefMentionFinder: Failed to find head token:\\n\" +\n        \"Tree is: \" + root + \"\\n\" +","sourceCodeStart":578,"sourceCodeEnd":614,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/coref/md/CorefMentionFinder.java#L578-L614","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the parse tree passed to coref is well-formed and its leaf index annotations cover all token positions","Verify the mention start index passed in comes from the same tokenization/parse as the tree (no off-by-one or stale offsets)","Update the CoreNLP version — some tree/annotation alignment bugs have been patched","If it occurs on malformed input, catch RuntimeException around coref postprocessing and skip the document"],"exampleFix":"// before\nthrow new RuntimeException(\"Shouldn't happen: \" + start + \" \" + root);\n// after\n// fix root cause: ensure tree children cover 'start'; optionally log state\nthrow new RuntimeException(\"No child of tree covers start index \" + start + \"; tree: \" + root);","handlingStrategy":"try-catch","validationCode":"// validate tokens/parse alignment before coref\nfor (CoreMap sent : doc.get(CoreAnnotations.SentencesAnnotation.class)) {\n  Tree t = sent.get(TreeCoreAnnotations.TreeAnnotation.class);\n  int nLeaves = t.getLeaves().size();\n  int nTokens = sent.get(CoreAnnotations.TokensAnnotation.class).size();\n  if (nLeaves != nTokens) throw new IllegalStateException(\"parse/token mismatch\");\n}","typeGuard":"static boolean covers(Tree root, int start) {\n  for (Tree kid : root.getChildrenAsList()) {\n    Integer b = kid.label().get(CoreAnnotations.BeginIndexAnnotation.class);\n    Integer e = kid.label().get(CoreAnnotations.EndIndexAnnotation.class);\n    if (b != null && e != null && b <= start && start < e) return true;\n  }\n  return false;\n}","tryCatchPattern":"try {\n  runCoref(doc);\n} catch (RuntimeException e) {\n  if (e.getMessage() != null && e.getMessage().startsWith(\"Shouldn't happen:\")) {\n    log.warn(\"Skipping malformed parse for coref: \" + e.getMessage());\n  } else throw e;\n}","preventionTips":["Keep tokenization and parse trees from the same CoreNLP pipeline run","Avoid hand-editing or post-processing trees before coref","Pin CoreNLP versions in build files to avoid parser model/annotation drift","Validate tree leaf count equals token count per sentence before coref"],"tags":["java","corenlp","parse-tree","invariant-violation"],"backgroundTag":"internal-invariant-violation","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"}