stanfordnlp/CoreNLP · error · RuntimeException

Cannot find matching labelled span for

Error message

Cannot find matching labelled span for {s}

What it means

While parsing CoNLL coreference chains, getLabelledSpans matches open span labels to the expected label; if the stack of open spans is exhausted without finding a matching label, it throws RuntimeException indicating the corpus annotations are malformed (unbalanced or mislabeled coref spans).

Solutions

  1. Locate the offending document (the last 'Reading document' log line) and inspect its coref columns
  2. Fix or remove the malformed document from the corpus file list
  3. Regenerate the corpus files from official CoNLL distributions instead of hand edits
  4. Check that you are not mixing CoNLL-2011 and CoNLL-2012 formatted files in one run

Example fix

// before
corpus.list contains: hand-edited_doc.v4_gold_conll  (mismatched coref brackets)
// after
corpus.list contains: official_conll2011_doc.v4_gold_conll  (regenerated, validated brackets)
Defensive patterns

Strategy: validation

Validate before calling

// pre-validate gold_conll coref columns are balanced
long opens = lines.stream().filter(l -> l.contains("(") && !l.contains(")")).count();
long closes = lines.stream().filter(l -> l.contains(")") && !l.contains("(")).count();
if (opens != closes) throw new IllegalStateException("unbalanced coref spans");

Try / catch

try {
  Document d = reader.getNextDocument();
} catch (RuntimeException e) {
  if (e.getMessage().startsWith("Cannot find matching labelled span")) {
    logger.severe("malformed coref annotations; skip this document");
    return null;
  }
  throw e;
}

Prevention

When it happens

Trigger: A CoNLL document contains coref bracket annotations whose labels don't line up — e.g. a closing bracket expecting label s but openSpans contains no span with that label, indicating nested/mismatched coref chains in the source file.

Common situations: Corrupt or hand-edited gold_conll files, files mixing CoNLL-2011 and 2012 annotation formats, custom-preprocessed corpus with broken coref brackets.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/dcoref/CoNLL2011DocumentReader.java:381

                openParenIndex = -1;
              }
              isDelimiter = true;
            }
            if (c == '(') {
              openParenIndex = j;
            } else if (c == ')') {
              Triple<Integer, Integer, String> t = openSpans.pop();
              if (checkEndLabel) {
                // NOTE: end parens may cross (usually because mention either start or end on the same token
                // and it is just an artifact of the ordering
                String s = val.substring(lastDelimiterIndex+1, j);
                if (!s.equals(t.third())) {
                  Stack<Triple<Integer,Integer, String>> saved = new Stack<>();
                  while (!s.equals(t.third())) {
                    // find correct match
                    saved.push(t);
                    if (openSpans.isEmpty()) {
                      throw new RuntimeException("Cannot find matching labelled span for " + s);
                    }
                    t = openSpans.pop();
                  }
                  while (!saved.isEmpty()) {
                    openSpans.push(saved.pop());
                  }
                  assert(s.equals(t.third()));
                }
              }
              t.setSecond(wordPos);
              spans.add(t);
            }
            if (isDelimiter) {
              lastDelimiterIndex = j;
            }
          }
          if (openParenIndex >= 0) {
            String s = val.substring(openParenIndex+1, val.length());

View on GitHub (pinned to 1b7edd19c4)