stanfordnlp/CoreNLP · error · IllegalArgumentException

Need to specify -model

Error message

Need to specify -model

What it means

FindNearestNeighbors locates similar phrase vectors using a trained DVModel, which is only available inside a serialized DVParser model file. main() throws this IllegalArgumentException when -model is absent. It is the first of three mandatory-option checks.

Solutions

  1. Pass -model <path-to-serialized-DVParser> on the command line
  2. Verify the file is a DVParser model (saved by DVParser), not a plain LexicalizedParser
  3. Check flag spelling: it must be exactly -model

Example fix

// before
java edu.stanford.nlp.parser.dvparser.FindNearestNeighbors -testTreebank tb -output out.txt
// after
java edu.stanford.nlp.parser.dvparser.FindNearestNeighbors -model dvparser.ser.gz -testTreebank tb -output out.txt
Defensive patterns

Strategy: validation

Validate before calling

if (!Arrays.asList(args).contains("-model")) {
  throw new IllegalArgumentException("FindNearestNeighbors requires -model <serializedDVParser>");
}

Try / catch

try {
  FindNearestNeighbors.main(args);
} catch (IllegalArgumentException e) {
  System.err.println("Missing required option: " + e.getMessage());
}

Prevention

When it happens

Trigger: Running FindNearestNeighbors.main() without the -model flag, so modelPath stays null after argument parsing.

Common situations: Forgetting that the tool operates on a trained DV model, not a plain parser model; running with only -testTreebank and -output; scripting examples that assumed a default model path.

Understand the failure class

Background: "Must pass :limit option" / "Missing required option" — required option errors explained — this error's family across 41 libraries.

Related errors


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

Appendix: source

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

    for (int argIndex = 0; argIndex < args.length; ) {
      if (args[argIndex].equalsIgnoreCase("-model")) {
        modelPath = args[argIndex + 1];
        argIndex += 2;
      } else if (args[argIndex].equalsIgnoreCase("-testTreebank")) {
        Pair<String, FileFilter> treebankDescription = ArgUtils.getTreebankDescription(args, argIndex, "-testTreebank");
        argIndex = argIndex + ArgUtils.numSubArgs(args, argIndex) + 1;
        testTreebankPath = treebankDescription.first();
        testTreebankFilter = treebankDescription.second();
      } else if (args[argIndex].equalsIgnoreCase("-output")) {
        outputPath = args[argIndex + 1];
        argIndex += 2;
      } else {
        unusedArgs.add(args[argIndex++]);
      }
    }

    if (modelPath == null) {
      throw new IllegalArgumentException("Need to specify -model");
    }
    if (testTreebankPath == null) {
      throw new IllegalArgumentException("Need to specify -testTreebank");
    }
    if (outputPath == null) {
      throw new IllegalArgumentException("Need to specify -output");
    }

    String[] newArgs = unusedArgs.toArray(new String[unusedArgs.size()]);

    LexicalizedParser lexparser = LexicalizedParser.loadModel(modelPath, newArgs);

    Treebank testTreebank = null;
    if (testTreebankPath != null) {
      log.info("Reading in trees from " + testTreebankPath);
      if (testTreebankFilter != null) {
        log.info("Filtering on " + testTreebankFilter);
      }

View on GitHub (pinned to 1b7edd19c4)