stanfordnlp/CoreNLP · error · RuntimeException
Bad line:
Error message
Bad line:
What it means
MalletReaderAndWriter reads CRF training/test data in Mallet's 'word TAB label' format. For each non-blank line it takes the last space as the word/label separator; if a line contains no space at all it cannot be split into a token and a label, so it throws this RuntimeException. It is a data-format error in the input file, not a library bug.
Solutions
- Fix the offending input line so it contains 'word label' separated by a space (the LAST space splits word from label).
- Remove or blank out empty/junk lines that contain non-whitespace characters but no space.
- Verify the reader matches your file's delimiter; if your file is tab-separated, pre-convert tabs to spaces or use a reader configured for your format.
- Wrap the read loop and log the failing line to locate it, then correct it in the source corpus.
Example fix
// before (bad input line) Hello // after Hello O
Defensive patterns
Strategy: validation
Validate before calling
for (String line : lines) {
if (line.trim().isEmpty()) continue;
if (line.lastIndexOf(" ") < 0) {
throw new IllegalArgumentException("Input line has no 'word label' separator: " + line);
}
} Type guard
boolean isMalletLine(String line) { return line != null && line.trim().length() > 0 && line.lastIndexOf(" ") >= 0; } Try / catch
try {
reader.apply(iter);
} catch (RuntimeException e) {
if (e.getMessage() != null && e.getMessage().startsWith("Bad line:")) {
log.error("Fix CRF input format: " + e.getMessage());
} else throw e;
} Prevention
- Pre-validate every corpus line matches 'word label' with at least one space before feeding the reader.
- Strip or blank junk lines in a preprocessing pass.
- Keep delimiters consistent (spaces, not tabs) across corpus-generation scripts.
When it happens
Trigger: Calling MalletReaderAndWriter.apply() (via a ColumnDocumentReaderAndWriter-style pipeline) with an input line that has no space character, e.g. a single-token line like 'Hello' instead of 'Hello O', or a line separated by tabs instead of spaces.
Common situations: Converting Mallet or custom corpora to Stanford NLP CRF format; lines with only a word and no gold label; trailing junk lines in a .txt training file; using TAB separators after a script changed the delimiter.
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
- after W derivative, index() != x.length()
- : Entry has multiple types for : . Taking type to be
- conditionalLogProbGivenFirst requires of one less than…
- conditionalLogProbGivenNext requires given one less than…
- conditionalLogProbGivenPrevious requires given one less…
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/cd32c7e1f2cbc606.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/sequences/MalletReaderAndWriter.java:67
int num = 0;
private class MalletDocParser implements Serializable, Function<String,List<CoreLabel>> {
private static final long serialVersionUID = -6211332661459630572L;
@Override
public List<CoreLabel> apply(String doc) {
if (num % 1000 == 0) { log.info("["+num+"]"); }
num++;
List<CoreLabel> words = new ArrayList<>();
String[] lines = doc.split("\n");
for (String line : lines) {
if (line.trim().length() < 1)
continue;
int idx = line.lastIndexOf(" ");
if (idx < 0)
throw new RuntimeException("Bad line: " + line);
CoreLabel wi = new CoreLabel();
wi.setWord(line.substring(0, idx));
wi.set(CoreAnnotations.AnswerAnnotation.class, line.substring(idx + 1));
wi.set(CoreAnnotations.GoldAnswerAnnotation.class, line.substring(idx + 1));
words.add(wi);
}
return words;
}
}
@Override
public void printAnswers(List<CoreLabel> doc, PrintWriter out) {
for (CoreLabel wi : doc) {
String answer = wi.get(CoreAnnotations.AnswerAnnotation.class);
String goldAnswer = wi.get(CoreAnnotations.GoldAnswerAnnotation.class);
out.println(wi.word() + "\t" + goldAnswer + "\t" + answer);
}
out.println();View on GitHub (pinned to 1b7edd19c4)