stanfordnlp/CoreNLP · error · IllegalArgumentException

Expected a tree

Error message

Expected a tree

What it means

LexicalizedParserClient.getTree() deserializes the server's response object and verifies it is a Tree. If the object read from the socket is not an instance of Tree, it throws this IllegalArgumentException, indicating the server sent something other than a parsed tree (e.g. an error string or wrong response type).

Solutions

  1. Ensure the server request actually asks for a parse (e.g. 'parse') before calling getTree().
  2. Match client and server CoreNLP versions so serialized response types agree.
  3. Log the received object's class to see what the server actually returned.
  4. Use the client's token/query methods for non-parse requests instead.

Example fix

// before
Tree tree = client.getTree("parse the sentence");
// after
String resp = client.query("parse", "the sentence"); // inspect response first
Tree tree = client.getTree("parse", "the sentence");
Defensive patterns

Strategy: try-catch

Type guard

Object o = response; boolean isTree = (o instanceof edu.stanford.nlp.trees.Tree);

Try / catch

try {
    Tree t = client.getTree("parse", sentence);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("Expected a tree")) {
        // server returned a non-Tree payload; check server version/request
    }
}

Prevention

When it happens

Trigger: The client sent a request the server answered with a non-Tree object, the server is a different/incompatible version, or the request command returned an error payload instead of a parse tree.

Common situations: Client/server version mismatch between CoreNLP jars, sending a tokenize-only or non-parse command but calling getTree(), or the server replying with an exception message object.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/server/LexicalizedParserClient.java:147

   */
  public Tree getTree(String query) 
    throws IOException
  {
    Socket socket = new Socket(host, port);

    Writer out = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
    out.write("tree " + query + "\n");
    out.flush();

    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    Object o;
    try {
      o = ois.readObject();
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
    if (!(o instanceof Tree)) {
      throw new IllegalArgumentException("Expected a tree");
    }
    Tree tree = (Tree) o;

    socket.close();
    return tree;
  }

  /**
   * Tell the server to exit
   */
  public void sendQuit() 
    throws IOException
  {
    Socket socket = new Socket(host, port);
    
    Writer out = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
    out.write("quit\n");
    out.flush();    

View on GitHub (pinned to 1b7edd19c4)