stanfordnlp/CoreNLP · error · IllegalArgumentException

Expected a LexicalizedParser with a Reranker attached

Error message

Expected a LexicalizedParser with a Reranker attached

What it means

FindNearestNeighbors expects the loaded LexicalizedParser to have been configured with a Reranker so parsing yields a RerankingParserQuery. If the created ParserQuery is not a RerankingParserQuery, it throws, because the nearest-neighbor logic depends on reranker internals. This usually means the DV model options were not passed through to loadModel.

Solutions

  1. Load the serialized DVParser model produced by DVParser (it embeds the reranker options), not a plain parser model
  2. Pass the original -model args through: LexicalizedParser.loadModel(modelPath, newArgs)
  3. Verify the model file was saved by DVParser with -model output, not LexicalizedParser's save

Example fix

// before
LexicalizedParser lp = LexicalizedParser.loadModel("englishPCFG.ser.gz");
// after
LexicalizedParser lp = LexicalizedParser.loadModel("dvparser.ser.gz", newArgs); // DVParser-saved model with reranker options
Defensive patterns

Strategy: validation

Validate before calling

LexicalizedParser lp = LexicalizedParser.loadModel(modelPath, newArgs);
ParserQuery pq = lp.parserQuery();
if (!(pq instanceof RerankingParserQuery)) {
  throw new IllegalStateException("Model lacks DV reranker options; load a DVParser-serialized model");
}

Type guard

boolean hasDvReranker(ParserQuery pq) {
  return pq instanceof RerankingParserQuery
      && ((RerankingParserQuery) pq).rerankerQuery() instanceof DVModelReranker.Query;
}

Try / catch

try {
  runNearestNeighborLookup();
} catch (IllegalArgumentException e) {
  System.err.println("Wrong parser/model configuration: " + e.getMessage());
}

Prevention

When it happens

Trigger: Calling lexparser.parserQuery() after loading a model without reranker options; the loaded parser's query factory builds a plain ParserQuery instead of RerankingParserQuery — e.g. loading a plain LexicalizedParser file with -model instead of a DVParser model.

Common situations: Passing newArgs (unused args) to LexicalizedParser.loadModel without the DVModelReranker option; loading an English PCFG model instead of the serialized DVParser; version mixes where reranker flags were dropped.

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/3f6320550bb62f3f. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/parser/dvparser/FindNearestNeighbors.java:125

      testTreebank = lexparser.getOp().tlpParams.memoryTreebank();;
      testTreebank.loadPath(testTreebankPath, testTreebankFilter);
      log.info("Read in " + testTreebank.size() + " trees for testing");
    }

    FileWriter out = new FileWriter(outputPath);
    BufferedWriter bout = new BufferedWriter(out);

    log.info("Parsing " + testTreebank.size() + " trees");
    int count = 0;
    List<ParseRecord> records = Generics.newArrayList();
    for (Tree goldTree : testTreebank) {
      List<Word> tokens = goldTree.yieldWords();
      ParserQuery parserQuery = lexparser.parserQuery();
      if (!parserQuery.parse(tokens)) {
        throw new AssertionError("Could not parse: " + tokens);
      }
      if (!(parserQuery instanceof RerankingParserQuery)) {
        throw new IllegalArgumentException("Expected a LexicalizedParser with a Reranker attached");
      }
      RerankingParserQuery rpq = (RerankingParserQuery) parserQuery;
      if (!(rpq.rerankerQuery() instanceof DVModelReranker.Query)) {
        throw new IllegalArgumentException("Expected a LexicalizedParser with a DVModel attached");
      }
      DeepTree tree = ((DVModelReranker.Query) rpq.rerankerQuery()).getDeepTrees().get(0);

      SimpleMatrix rootVector = null;
      for (Map.Entry<Tree, SimpleMatrix> entry : tree.getVectors().entrySet()) {
        if (entry.getKey().label().value().equals("ROOT")) {
          rootVector = entry.getValue();
          break;
        }
      }
      if (rootVector == null) {
        throw new AssertionError("Could not find root nodevector");
      }
      out.write(tokens + "\n");

View on GitHub (pinned to 1b7edd19c4)