{"record":{"id":"dd5f90d20834aac8","repo":"stanfordnlp/CoreNLP","slug":"span-is-out-of-range","errorCode":null,"errorMessage":"Span is out of range: ","messagePattern":"Span is out of range: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/simple/SentenceAlgorithms.java","lineNumber":245,"sourceCode":"   */\n  public List<String> keyphrases() {\n    return keyphrases(Sentence::words);\n  }\n\n  /**\n   * Get the index of the head word for a given span, based off of the dependency parse.\n   *\n   * @param tokenSpan The span of tokens we are finding the head of.\n   * @return The head index of the given span of tokens.\n   */\n  public int headOfSpan(Span tokenSpan) {\n    // Error checks\n    if (tokenSpan.size() == 0) {\n      throw new IllegalArgumentException(\"Cannot find head word of empty span!\");\n    }\n    List<Optional<Integer>> governors = sentence.governors();\n    if (tokenSpan.start() >= governors.size()) {\n      throw new IllegalArgumentException(\"Span is out of range: \" + tokenSpan + \"; sentence: \" + sentence);\n    }\n    if (tokenSpan.end() > governors.size()) {\n      throw new IllegalArgumentException(\"Span is out of range: \" + tokenSpan + \"; sentence: \" + sentence);\n    }\n\n    // Find where to start searching up the dependency tree\n    int candidateStart = tokenSpan.end() - 1;\n    Optional<Integer> parent;\n    while ( !(parent = governors.get(candidateStart)).isPresent() ) {\n      candidateStart -= 1;\n      if (candidateStart < tokenSpan.start()) {\n        // Case: nothing in this span has a head. Default to right-most element.\n        return tokenSpan.end() - 1;\n      }\n    }\n    int candidate = candidateStart;\n\n    // Search up the dependency tree","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/simple/SentenceAlgorithms.java#L227-L263","documentation":"headOfSpan validates the span against the sentence length via the governors list; if tokenSpan.start() >= governors.size() the span begins beyond the sentence, so IllegalArgumentException('Span is out of range') is thrown. It guards against spans referencing tokens that don't exist in this sentence.","triggerScenarios":"Calling headOfSpan with a Span whose start index is >= the number of tokens in the sentence — typically a span from a different sentence, from stale indices after re-tokenization, or constructed with 1-based offsets by mistake.","commonSituations":"Mixing spans computed on an older tokenization of the sentence with a re-created Sentence; sharing spans across sentences in a document loop; off-by-one from passing character offsets instead of token indices.","solutions":["Verify the span belongs to this exact Sentence (0-based token indices within the sentence length)","Recompute spans after any re-tokenization instead of reusing old ones","Check tokenSpan.start() < sentence.length() before calling"],"exampleFix":"// before\nint head = algorithms.headOfSpan(otherSentenceSpan);\n// after\nif (span.start() >= sentence.length() || span.end() > sentence.length()) {\n  throw new IllegalArgumentException(\"span does not belong to sentence\");\n}\nint head = algorithms.headOfSpan(span);","handlingStrategy":"validation","validationCode":"if (span.start() >= 0 && span.start() < sentence.length()) {\n  int head = sentence.algorithms().headOfSpan(span);\n}","typeGuard":"boolean spanInSentence(Span s, Sentence sentence) {\n  return s.start() >= 0 && s.end() <= sentence.length();\n}","tryCatchPattern":"try {\n  int head = algorithms.headOfSpan(span);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"Span is out of range\")) {\n    head = -1;\n  } else { throw e; }\n}","preventionTips":["Keep spans bound to the sentence they were built from","Rebuild spans after retokenization","Use 0-based token indices, never character offsets"],"tags":["corenlp","span","index-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}