{"record":{"id":"4a19086da2dd9441","repo":"stanfordnlp/CoreNLP","slug":"sorry-but-parsers-with-rerankers-cannot-be-saved","errorCode":null,"errorMessage":"Sorry, but parsers with rerankers cannot be saved to text file","messagePattern":"Sorry, but parsers with rerankers cannot be saved to text file","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java","lineNumber":437,"sourceCode":"    try {\n      log.info(\"Writing parser in serialized format to file \" + filename + ' ');\n      ObjectOutputStream out = IOUtils.writeStreamFromString(filename);\n      out.writeObject(this);\n      out.close();\n      log.info(\"done.\");\n    } catch (IOException ioe) {\n      throw new RuntimeIOException(ioe);\n    }\n  }\n\n  /**\n   * Saves the parser defined by pd to the given filename.\n   * If there is an error, a RuntimeIOException is thrown.\n   */\n  // todo: [cdm 2015] This doesn't use character encoding and it should!\n  public void saveParserToTextFile(String filename) {\n    if (reranker != null) {\n      throw new UnsupportedOperationException(\"Sorry, but parsers with rerankers cannot be saved to text file\");\n    }\n    try {\n      log.info(\"Writing parser in text grammar format to file \" + filename);\n      OutputStream os;\n      if (filename.endsWith(\".gz\")) {\n        // it's faster to do the buffering _outside_ the gzipping as here\n        os = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(filename)));\n      } else {\n        os = new BufferedOutputStream(new FileOutputStream(filename));\n      }\n      PrintWriter out = new PrintWriter(os);\n      String prefix = \"BEGIN \";\n\n      out.println(prefix + \"OPTIONS\");\n      op.writeData(out);\n      out.println();\n      log.info(\".\");\n","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java#L419-L455","documentation":"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.","triggerScenarios":"Calling saveParserToTextFile (or main's -textGrammar output path / testGrammarCompaction) on a parser loaded with a reranker attached.","commonSituations":"Loading a reranking parser model and attempting to export it in text grammar format for inspection or retraining.","solutions":["Use saveParserToSerializedFile instead, which preserves the reranker.","Load/save a base (non-reranking) parser if a text grammar export is required.","Strip the reranker (null it out) before text export only if you accept losing reranking functionality."],"exampleFix":"// before\nparser.saveParserToTextFile(\"out.txt.gz\"); // parser has a reranker\n// after\nparser.saveParserToSerializedFile(\"out.ser.gz\");","handlingStrategy":"fallback","validationCode":"if (parser.reranker != null) { /* use saveParserToSerializedFile instead */ }","typeGuard":null,"tryCatchPattern":"try { parser.saveParserToTextFile(f); } catch (UnsupportedOperationException e) { parser.saveParserToSerializedFile(f + \".ser.gz\"); }","preventionTips":["Prefer serialized output for any parser with a reranker","Check reranker presence before requesting text grammar export"],"tags":["serialization","reranker","unsupported-operation","parser"],"backgroundTag":"unsupported-operation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}