stanfordnlp/CoreNLP · error · RuntimeException
Cannot find matching labelled span for
Error message
Cannot find matching labelled span for
What it means
getLabelledSpans builds spans from bracket-style CoNLL labels (e.g. coref and NER columns). While scanning a token's label set against the currently open spans, a close marker arrived whose label does not match any open span, and openSpans ran out without finding a match. This means the bracket annotations in the corpus column are unbalanced or mismatched, so the library throws a RuntimeException.
Solutions
- Validate the annotation file: every opening label like (LABEL must have a matching closing LABEL) in the same column
- Regenerate the file from the original CoNLL-2012 distribution rather than hand-editing
- Check that your preprocessing did not merge or reorder tokens within a sentence, breaking bracket pairing
- Log the sentence and column (concatField) at failure to locate the offending line before fixing it
Example fix
// before (hand-edited line) token (ARG0) token ) token ARG0) <- closing without open // after token (ARG0 token ARG0) <- balanced brackets in the coref column
Defensive patterns
Strategy: validation
Validate before calling
// Pre-check a coref/NER column for balanced brackets
static boolean bracketsBalanced(String col) { int opens=0; for (String tok : col.split("\\s+")) { for (String part : tok.split("(?=\\()|(?<=\\))")) { if (part.startsWith("(")) opens++; if (part.endsWith("))")) opens-=2; else if (part.endsWith(")")) opens--; if (opens<0) return false; } } return opens==0; } Try / catch
try { spans = reader.getLabelledSpans(...); } catch (RuntimeException e) { if (e.getMessage().startsWith("Cannot find matching labelled span")) { throw new CorruptAnnotationException(e.getMessage() + " — fix brackets in the column", e); } throw e; } Prevention
- Validate CoNLL files with the official scripts before loading
- Never hand-edit bracket annotations
- Keep tokens and their annotations aligned when preprocessing
- Diff against original gold files after any transformation
When it happens
Trigger: A token carries a closing label (or an Openify/Closeify mismatch) such that popping openSpans never yields the expected label s; i.e. malformed coref or NER bracket encoding in a line of the CoNLL file.
Common situations: Hand-edited or truncated CoNLL files where an opening bracket was deleted; column-shifting edits (tabs/space changed) so labels are read from the wrong column; custom-preprocessed corpora that closed spans out of order.
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
- Error extracting labelled spans for column :
- Unexpected number of field , expected >= for line (,):
- INVALID LINE: "${line}"
- ERROR: Invalid dependency node line: ${line}
- ERROR: Invalid format for dependency graph: ${line}
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/57ca0615306e13e0.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/coref/docreader/CoNLLDocumentReader.java:426
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)