stanfordnlp/CoreNLP · error · RuntimeException
expecting BEGIN block; got
Error message
expecting BEGIN block; got
What it means
confirmBeginBlock validates that the current line of a text-format parser grammar file begins a BEGIN block. If the file ends before any line is read (line == null) or the line does not start with "BEGIN", a RuntimeException is thrown naming the grammar file and the offending line. This means the grammar text file is malformed or is not a text grammar at all.
Solutions
- Check the file at the path printed in the exception: it must be a text grammar whose sections start with BEGIN lines (e.g. BEGIN_GENERIC, BEGIN_STATE)
- If you actually have a serialized model, load it with getParserFromSerializedFile / LexicalizedParser.loadModel instead of the text-file path
- Re-download or restore the original grammar file — it may be truncated or corrupted
- Ensure the file is plain UTF-8 with no BOM and no stray leading blank/garbage line before the first BEGIN
- Enable parser verbosity (-vv or set op.testOptions.verbose) and compare with a known-good shipped grammar like englishPCFG.ser.gz format
Example fix
// before
LexicalizedParser lp = LexicalizedParser.loadModel("myGrammar.txt"); // binary file read as text grammar
// after
LexicalizedParser lp = LexicalizedParser.loadModel("englishPCFG.ser.gz"); // correct serialized model Defensive patterns
Strategy: validation
Validate before calling
// Java: sanity-check the file looks like a text grammar before loading
BufferedReader r = Files.newBufferedReader(Paths.get(path), StandardCharsets.UTF_8);
String first = r.readLine();
r.close();
if (first == null || !first.startsWith("BEGIN")) throw new IllegalArgumentException("not a text grammar: " + path); Prevention
- Only pass text grammars (BEGIN/END sectioned) to the text-file loader; use loadModel for .ser/.ser.gz
- Verify checksums of downloaded grammar files to catch truncation
- Strip BOMs and normalize line endings on edited grammar files
When it happens
Trigger: Calling LexicalizedParser.getParserFromTextFile (via loadParserFromTextFile / the main pd path after a serialized load fails) on a file whose structure is not the expected BEGIN/END sectioned grammar text format.
Common situations: Passing a serialized (binary) parser model path or an unrelated text file as a text grammar; a grammar file that was truncated during download/copy; editing a grammar file and deleting or renaming the BEGIN header; CRLF or BOM corruption changing the first characters of the header line.
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
- Unexpected number of field , expected >= for line (,):
- First line of input file should be header definition
- format error in embeddings
- format error unexpected featureFactory line:
- weights format error
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/162c2fd667f90e76.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java:508
dg.writeData(out);
}
out.println();
log.info(".");
out.flush();
out.close();
log.info("done.");
} catch (IOException e) {
log.info("Trouble saving parser data to ASCII format.");
throw new RuntimeIOException(e);
}
}
private static void confirmBeginBlock(String file, String line) {
if (line == null) {
throw new RuntimeException(file + ": expecting BEGIN block; got end of file.");
} else if (! line.startsWith("BEGIN")) {
throw new RuntimeException(file + ": expecting BEGIN block; got " + line);
}
}
protected static LexicalizedParser getParserFromTextFile(String textFileOrUrl, Options op) {
try (BufferedReader in = IOUtils.readerFromString(textFileOrUrl)) {
Timing tim = new Timing();
String line = in.readLine();
confirmBeginBlock(textFileOrUrl, line);
op.readData(in);
line = in.readLine();
confirmBeginBlock(textFileOrUrl, line);
Index<String> stateIndex = HashIndex.loadFromReader(in);
line = in.readLine();
confirmBeginBlock(textFileOrUrl, line);
Index<String> wordIndex = HashIndex.loadFromReader(in);View on GitHub (pinned to 1b7edd19c4)