stanfordnlp/CoreNLP · error · IllegalArgumentException

Expected a RerankingParserQuery

Error message

Expected a RerankingParserQuery

What it means

ParseAndPrintMatrices.main() requires the parser's ParserQuery to be a RerankingParserQuery so it can extract deep trees and print matrices. If the parser was loaded without DV reranker options, parserQuery() returns a plain ParserQuery and this IllegalArgumentException is thrown before parsing the sentence.

Solutions

  1. Load a DVParser-serialized model with LexicalizedParser.loadModel(modelPath, newArgs) so reranker options apply
  2. Confirm the model was produced by DVParser (it stores the reranker configuration)
  3. If parsing arbitrary text, ensure the whole pipeline (options) that attaches the DVModelReranker is preserved

Example fix

// before
LexicalizedParser parser = LexicalizedParser.loadModel("englishPCFG.ser.gz");
// after
LexicalizedParser parser = LexicalizedParser.loadModel("dvparser.ser.gz", newArgs);
Defensive patterns

Strategy: validation

Validate before calling

ParserQuery pq = parser.parserQuery();
if (!(pq instanceof RerankingParserQuery)) {
  throw new IllegalStateException("Parser not configured with DV reranker; load DVParser-serialized model with newArgs");
}

Type guard

boolean isRerankingQuery(ParserQuery pq) {
  return pq instanceof RerankingParserQuery;
}

Try / catch

try {
  processSentences(parser, inputPath);
} catch (IllegalArgumentException e) {
  System.err.println("Model lacks reranker: " + e.getMessage());
}

Prevention

When it happens

Trigger: Running main() with a LexicalizedParser loaded from a plain (non-DV) model, so pq instanceof RerankingParserQuery is false for the first sentence from DocumentPreprocessor.

Common situations: Pointing -model at englishPCFG.ser.gz instead of a DVParser-serialized model; dropping the unused-args pass-through to loadModel; editing the tool to load a different model type.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/dvparser/ParseAndPrintMatrices.java:112

    }

    String[] newArgs = unusedArgs.toArray(new String[unusedArgs.size()]);
    LexicalizedParser parser = LexicalizedParser.loadModel(modelPath, newArgs);
    DVModel model = DVParser.getModelFromLexicalizedParser(parser);

    File outputFile = new File(outputPath);
    FileSystem.checkNotExistsOrFail(outputFile);
    FileSystem.mkdirOrFail(outputFile);

    int count = 0;
    if (inputPath != null) {
      Reader input = new BufferedReader(new FileReader(inputPath));
      DocumentPreprocessor processor = new DocumentPreprocessor(input);
      for (List<HasWord> sentence : processor) {
        count++; // index from 1
        ParserQuery pq = parser.parserQuery();
        if (!(pq instanceof RerankingParserQuery)) {
          throw new IllegalArgumentException("Expected a RerankingParserQuery");
        }
        RerankingParserQuery rpq = (RerankingParserQuery) pq;
        if (!rpq.parse(sentence)) {
          throw new RuntimeException("Unparsable sentence: " + sentence);
        }
        RerankerQuery reranker = rpq.rerankerQuery();
        if (!(reranker instanceof DVModelReranker.Query)) {
          throw new IllegalArgumentException("Expected a DVModelReranker");
        }
        DeepTree deepTree = ((DVModelReranker.Query) reranker).getDeepTrees().get(0);
        IdentityHashMap<Tree, SimpleMatrix> vectors = deepTree.getVectors();

        for (Map.Entry<Tree, SimpleMatrix> entry : vectors.entrySet()) {
          log.info(entry.getKey() + "   " +  entry.getValue());
        }

        FileWriter fout = new FileWriter(outputPath + File.separator + "sentence" + count + ".txt");
        BufferedWriter bout = new BufferedWriter(fout);

View on GitHub (pinned to 1b7edd19c4)