stanfordnlp/CoreNLP · error · IllegalStateException

Model has not been loaded or trained

Error message

Model has not been loaded or trained

What it means

initialize(verbose) builds the transition system (ArcStandard) from knownLabels after a model is loaded or training finished. If knownLabels is null, no model was loaded and training never ran, so IllegalStateException is thrown. This guards against parsing with an empty/uninitialized parser state.

Solutions

  1. Call DependencyParser.train(...) for training use cases before initialize/parse
  2. Load a saved model with DependencyParser.load(modelFile) or loadModelFile before parsing
  3. Check that a prior load/train didn't throw and was caught, leaving the parser uninitialized

Example fix

// before
DependencyParser parser = new DependencyParser(config);
parser.predict(sentence); // knownLabels == null -> throws
// after
DependencyParser parser = DependencyParser.load("model.txt.gz");
parser.predict(sentence);
Defensive patterns

Strategy: try-catch

Validate before calling

DependencyParser parser = DependencyParser.load(modelPath); // initializes knownLabels and system
parser.parse(sentence);

Try / catch

try {
  parser.parse(sentence);
} catch (IllegalStateException e) {
  parser = DependencyParser.load(modelPath);
  parser.parse(sentence);
}

Prevention

When it happens

Trigger: Calling parse()/predict paths that trigger initialize() on a DependencyParser constructed with `new DependencyParser(config)` where neither train(...) nor loadModel(...) was ever called.

Common situations: Creating a parser via constructor and calling parse without train/load; train or model-load failing silently earlier (exception swallowed) leaving knownLabels null; calling internal APIs out of order.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/nndep/DependencyParser.java:1195

      for (TypedDependency dep : deps)
        output.println(dep);
      output.println();

      numSentences++;
    }

    long millis = timer.stop();
    double seconds = millis / 1000.0;
    log.info(String.format("Parsed %d sentences in %.2f seconds (%.2f sents/sec).%n",
        numSentences, seconds, numSentences / seconds));
  }

  /**
   * Prepare for parsing after a model has been loaded.
   */
  private void initialize(boolean verbose) {
    if (knownLabels == null)
      throw new IllegalStateException("Model has not been loaded or trained");

    // NOTE: remove -NULL-, and then pass the label set to the ParsingSystem
    List<String> lDict = new ArrayList<>(knownLabels);
    lDict.remove(0);

    system = new ArcStandard(config.tlp, lDict, verbose);

    // Pre-compute matrix multiplications
    if (config.numPreComputed > 0) {
      classifier.preCompute();
    }
  }

  /**
   * Explicitly specifies the number of arguments expected with
   * particular command line options.
   */
  private static final Map<String, Integer> numArgs = new HashMap<>();

View on GitHub (pinned to 1b7edd19c4)