stanfordnlp/CoreNLP · error · UnsupportedOperationException

Can't parse a zero-length sentence!

Error message

Can't parse a zero-length sentence!

What it means

LexicalizedParserQuery.parseInternal rejects an input sentence with zero tokens by setting parseSkipped and throwing UnsupportedOperationException("Can't parse a zero-length sentence!"). A constituency parse requires at least one token, so an empty input can never produce a tree.

Solutions

  1. Check sentence.size() > 0 before calling parse and skip/handle empty inputs yourself
  2. Guard the text before tokenization: trim and test for empty/whitespace-only strings
  3. In batch pipelines, filter out empty sentences produced by the sentence splitter
  4. Catch UnsupportedOperationException around parse if empty input is expected and recoverable

Example fix

// before
parser.parse(sentence); // sentence may be empty
// after
if (!sentence.isEmpty()) {
  parser.parse(sentence);
}
Defensive patterns

Strategy: validation

Validate before calling

// Skip empty inputs before parsing
if (sentence == null || sentence.isEmpty()) return null; // or log & skip

Try / catch

try {
  Tree t = parser.parse(sentence);
} catch (UnsupportedOperationException e) {
  if (e.getMessage().contains("zero-length")) return null;
  throw e;
}

Prevention

When it happens

Trigger: Calling parser.parse(new ArrayList<>()) / parserQuery.parse(Collections.emptyList()) or feeding empty text through a tokenizer that yields no words.

Common situations: Passing whitespace-only or punctuation-stripped text through the tokenizer; processing blank lines in a batch loop; upstream sentence splitting producing empty segments; accidentally clearing the word list before parsing.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/lexparser/LexicalizedParserQuery.java:214

   * @param sentence The sentence to parse
   * @return true Iff the sentence was accepted by the grammar
   * @throws UnsupportedOperationException If the Sentence is too long or
   *                                       of zero length or the parse
   *                                       otherwise fails for resource reasons
   */
  private boolean parseInternal(List<? extends HasWord> sentence) {
    parseSucceeded = false;
    parseNoMemory = false;
    parseUnparsable = false;
    parseSkipped = false;
    parseFallback = false;
    whatFailed = null;
    addedPunct = false;
    originalSentence = sentence;
    int length = sentence.size();
    if (length == 0) {
      parseSkipped = true;
      throw new UnsupportedOperationException("Can't parse a zero-length sentence!");
    }

    List<HasWord> sentenceB;
    if (op.wordFunction != null) {
      sentenceB = Generics.newArrayList();
      for (HasWord word : originalSentence) {
        if (word instanceof Label) {
          Label label = (Label) word;
          Label newLabel = label.labelFactory().newLabel(label);
          if (newLabel instanceof HasWord) {
            sentenceB.add((HasWord) newLabel);
          } else {
            throw new AssertionError("This should have been a HasWord");
          }
        } else if (word instanceof HasTag) {
          TaggedWord tw = new TaggedWord(word.word(), ((HasTag) word).tag());
          sentenceB.add(tw);
        } else {

View on GitHub (pinned to 1b7edd19c4)