{"record":{"id":"3f6320550bb62f3f","repo":"stanfordnlp/CoreNLP","slug":"expected-a-lexicalizedparser-with-a-reranker-attac","errorCode":null,"errorMessage":"Expected a LexicalizedParser with a Reranker attached","messagePattern":"Expected a LexicalizedParser with a Reranker attached","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/dvparser/FindNearestNeighbors.java","lineNumber":125,"sourceCode":"      testTreebank = lexparser.getOp().tlpParams.memoryTreebank();;\n      testTreebank.loadPath(testTreebankPath, testTreebankFilter);\n      log.info(\"Read in \" + testTreebank.size() + \" trees for testing\");\n    }\n\n    FileWriter out = new FileWriter(outputPath);\n    BufferedWriter bout = new BufferedWriter(out);\n\n    log.info(\"Parsing \" + testTreebank.size() + \" trees\");\n    int count = 0;\n    List<ParseRecord> records = Generics.newArrayList();\n    for (Tree goldTree : testTreebank) {\n      List<Word> tokens = goldTree.yieldWords();\n      ParserQuery parserQuery = lexparser.parserQuery();\n      if (!parserQuery.parse(tokens)) {\n        throw new AssertionError(\"Could not parse: \" + tokens);\n      }\n      if (!(parserQuery instanceof RerankingParserQuery)) {\n        throw new IllegalArgumentException(\"Expected a LexicalizedParser with a Reranker attached\");\n      }\n      RerankingParserQuery rpq = (RerankingParserQuery) parserQuery;\n      if (!(rpq.rerankerQuery() instanceof DVModelReranker.Query)) {\n        throw new IllegalArgumentException(\"Expected a LexicalizedParser with a DVModel attached\");\n      }\n      DeepTree tree = ((DVModelReranker.Query) rpq.rerankerQuery()).getDeepTrees().get(0);\n\n      SimpleMatrix rootVector = null;\n      for (Map.Entry<Tree, SimpleMatrix> entry : tree.getVectors().entrySet()) {\n        if (entry.getKey().label().value().equals(\"ROOT\")) {\n          rootVector = entry.getValue();\n          break;\n        }\n      }\n      if (rootVector == null) {\n        throw new AssertionError(\"Could not find root nodevector\");\n      }\n      out.write(tokens + \"\\n\");","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/dvparser/FindNearestNeighbors.java#L107-L143","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Load the serialized DVParser model produced by DVParser (it embeds the reranker options), not a plain parser model","Pass the original -model args through: LexicalizedParser.loadModel(modelPath, newArgs)","Verify the model file was saved by DVParser with -model output, not LexicalizedParser's save"],"exampleFix":"// before\nLexicalizedParser lp = LexicalizedParser.loadModel(\"englishPCFG.ser.gz\");\n// after\nLexicalizedParser lp = LexicalizedParser.loadModel(\"dvparser.ser.gz\", newArgs); // DVParser-saved model with reranker options","handlingStrategy":"validation","validationCode":"LexicalizedParser lp = LexicalizedParser.loadModel(modelPath, newArgs);\nParserQuery pq = lp.parserQuery();\nif (!(pq instanceof RerankingParserQuery)) {\n  throw new IllegalStateException(\"Model lacks DV reranker options; load a DVParser-serialized model\");\n}","typeGuard":"boolean hasDvReranker(ParserQuery pq) {\n  return pq instanceof RerankingParserQuery\n      && ((RerankingParserQuery) pq).rerankerQuery() instanceof DVModelReranker.Query;\n}","tryCatchPattern":"try {\n  runNearestNeighborLookup();\n} catch (IllegalArgumentException e) {\n  System.err.println(\"Wrong parser/model configuration: \" + e.getMessage());\n}","preventionTips":["Only load models saved by DVParser for these tools","Pass newArgs (unrecognized options) through to LexicalizedParser.loadModel","Smoke-test the loaded parser's query type before batch processing"],"tags":["java","dvparser","type-mismatch","reranker"],"backgroundTag":"incompatible-source-type","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"}