stanfordnlp/CoreNLP · error · UnsupportedOperationException

Sorry, but parsers with rerankers cannot be saved to text…

Error message

Sorry, but parsers with rerankers cannot be saved to text file

What it means

saveParserToTextFile writes the grammar in a text format that cannot represent a reranker component. If the parser instance has a non-null reranker (loaded from a reranking model), saving to text is unsupported and an UnsupportedOperationException is thrown; serialization via saveParserToSerializedFile still works.

Solutions

  1. Use saveParserToSerializedFile instead, which preserves the reranker.
  2. Load/save a base (non-reranking) parser if a text grammar export is required.
  3. Strip the reranker (null it out) before text export only if you accept losing reranking functionality.

Example fix

// before
parser.saveParserToTextFile("out.txt.gz"); // parser has a reranker
// after
parser.saveParserToSerializedFile("out.ser.gz");
Defensive patterns

Strategy: fallback

Validate before calling

if (parser.reranker != null) { /* use saveParserToSerializedFile instead */ }

Try / catch

try { parser.saveParserToTextFile(f); } catch (UnsupportedOperationException e) { parser.saveParserToSerializedFile(f + ".ser.gz"); }

Prevention

When it happens

Trigger: Calling saveParserToTextFile (or main's -textGrammar output path / testGrammarCompaction) on a parser loaded with a reranker attached.

Common situations: Loading a reranking parser model and attempting to export it in text grammar format for inspection or retraining.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

    try {
      log.info("Writing parser in serialized format to file " + filename + ' ');
      ObjectOutputStream out = IOUtils.writeStreamFromString(filename);
      out.writeObject(this);
      out.close();
      log.info("done.");
    } catch (IOException ioe) {
      throw new RuntimeIOException(ioe);
    }
  }

  /**
   * Saves the parser defined by pd to the given filename.
   * If there is an error, a RuntimeIOException is thrown.
   */
  // todo: [cdm 2015] This doesn't use character encoding and it should!
  public void saveParserToTextFile(String filename) {
    if (reranker != null) {
      throw new UnsupportedOperationException("Sorry, but parsers with rerankers cannot be saved to text file");
    }
    try {
      log.info("Writing parser in text grammar format to file " + filename);
      OutputStream os;
      if (filename.endsWith(".gz")) {
        // it's faster to do the buffering _outside_ the gzipping as here
        os = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(filename)));
      } else {
        os = new BufferedOutputStream(new FileOutputStream(filename));
      }
      PrintWriter out = new PrintWriter(os);
      String prefix = "BEGIN ";

      out.println(prefix + "OPTIONS");
      op.writeData(out);
      out.println();
      log.info(".");

View on GitHub (pinned to 1b7edd19c4)