stanfordnlp/CoreNLP · error · java.lang.ClassCastException

Wanted LexicalizedParser, got

Error message

Wanted LexicalizedParser, got ${object.getClass()}

What it means

ParserGrammar.loadModelFromZip() throws this ClassCastException-style RuntimeException when the object deserialized from the zip entry is not an instance of ParserGrammar. The message reports the actual class of the loaded object. Historically the expected type was LexicalizedParser, hence the wording.

Solutions

  1. Verify the zip contains a serialized ParserGrammar/LexicalizedParser model
  2. Use the correct loader for the artifact type (e.g. Tagger's loader for tagger models)
  3. Regenerate the model with a compatible version of the parser

Example fix

// before
ParserGrammar pg = ParserGrammar.loadModelFromZip("tagger.zip", "model");
// after
MaxentTagger tagger = new MaxentTagger("tagger.zip"); // correct loader for tagger models
Defensive patterns

Strategy: try-catch

Validate before calling

try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(in))) { Object o = ois.readObject(); if (!(o instanceof ParserGrammar)) throw new IllegalArgumentException("not a parser model: " + o.getClass()); }

Type guard

boolean isParserModel(Object o) { return o instanceof ParserGrammar; }

Try / catch

try { return ParserGrammar.loadModelFromZip(zip, name); } catch (RuntimeException e) { if (e.getMessage().startsWith("Wanted")) { throw new IOException("artifact is not a ParserGrammar; use the right loader", e); } throw e; }

Prevention

When it happens

Trigger: Passing a zip whose entry contains some other serialized object (a different model type, a Pipeline, an arbitrary serialized blob) to loadModelFromZip.

Common situations: Mixing up model files, e.g. trying to load a tagger model zip as a parser; loading a model produced by an incompatible library version whose serialized class changed.

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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/common/ParserGrammar.java:241

            ois.close();
            in.close();
          }
        }
      } else {
        throw new FileNotFoundException("Could not find " + modelName +
                                        " inside " + zipFilename);
      }
    } catch (IOException e) {
      throw new RuntimeIOException(e);
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }

    if (object instanceof ParserGrammar) {
      return (ParserGrammar) object;
    }
    throw new ClassCastException("Wanted LexicalizedParser, got " +
                                 object.getClass());
  }


}

View on GitHub (pinned to 1b7edd19c4)