stanfordnlp/CoreNLP · error · RuntimeException

expecting BEGIN block; got end of file.

Error message

expecting BEGIN block; got end of file.

What it means

confirmBeginBlock validates the first line read from a text-format parser file: text grammars must start with a 'BEGIN' block. A null line means the file ended before any block was found; a non-BEGIN line means the file doesn't start with the expected header. Either way the file is not a valid text grammar.

Solutions

  1. Confirm the file is a text grammar starting with a 'BEGIN' line, not a serialized model; use loadModel for serialized files.
  2. Check the path/URL resolves to a non-empty resource.
  3. Regenerate the text grammar with the same parser version if the format changed.

Example fix

// before
LexicalizedParser.getParserFromTextFile("english.ser.gz", op); // serialized file
// after
LexicalizedParser lp = LexicalizedParser.loadModel("english.ser.gz");
Defensive patterns

Strategy: validation

Validate before calling

String firstLine = new BufferedReader(new InputStreamReader(url.openStream())).readLine();
if (firstLine == null || !firstLine.startsWith("BEGIN")) throw new IllegalArgumentException("Not a text grammar file: " + file);

Try / catch

try { parser = getParserFromTextFile(file, op); } catch (RuntimeException e) { parser = LexicalizedParser.loadModel(file); }

Prevention

When it happens

Trigger: getParserFromTextFile reading an empty file, a URL that returned empty content, or a file whose first line is not 'BEGIN' (e.g. a serialized parser or a text grammar from an incompatible version).

Common situations: Passing a .ser.gz model to a text-grammar loading path, mistyping a URL so it yields empty content, or truncating the grammar file.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java:506

      out.println(prefix + "DEPENDENCY_GRAMMAR");
      if (dg != null) {
        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();

View on GitHub (pinned to 1b7edd19c4)