stanfordnlp/CoreNLP · error · IllegalArgumentException

Expected a LexicalizedParser with a DVModel attached

Error message

Expected a LexicalizedParser with a DVModel attached

What it means

Even with a RerankingParserQuery, FindNearestNeighbors specifically requires the reranker to be a DVModelReranker.Query so it can call getDeepTrees(). If the rerankerQuery is some other Reranker implementation, this IllegalArgumentException is thrown. It indicates the attached reranker is not the DV model reranker.

Solutions

  1. Load the DVParser model saved with a DVModelReranker so the query type matches
  2. Do not pass flags that replace the reranker in newArgs to loadModel
  3. Retrain/save via DVParser if the model was produced by another pipeline

Example fix

// before
LexicalizedParser.loadModel("someOtherRerankerModel.ser.gz");
// after
LexicalizedParser.loadModel("dvparserWithDVModel.ser.gz", newArgs); // model with DVModelReranker
Defensive patterns

Strategy: type-guard

Validate before calling

RerankingParserQuery rpq = (RerankingParserQuery) parserQuery;
if (!(rpq.rerankerQuery() instanceof DVModelReranker.Query)) {
  throw new IllegalStateException("Attached reranker is not DVModelReranker; reload with a DV model");
}

Type guard

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

Try / catch

try {
  DeepTree tree = ((DVModelReranker.Query) rpq.rerankerQuery()).getDeepTrees().get(0);
} catch (IllegalArgumentException e) {
  System.err.println("Non-DV reranker attached: " + e.getMessage());
}

Prevention

When it happens

Trigger: Parsing succeeds and parserQuery is RerankingParserQuery, but rpq.rerankerQuery() is not an instance of DVModelReranker.Query — e.g. a different Reranker was attached via options or the model lacks DV vectors.

Common situations: Loading a model trained with a custom/other reranker; options passed to loadModel that override the reranker; mixing model files so the reranker options point at the wrong model.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

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

    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");
      out.write(tree.getTree() + "\n");
      for (int i = 0; i < rootVector.getNumElements(); ++i) {
        out.write("  " + rootVector.get(i));
      }

View on GitHub (pinned to 1b7edd19c4)