{"record":{"id":"971e9532973b4369","repo":"stanfordnlp/CoreNLP","slug":"expected-a-tree","errorCode":null,"errorMessage":"Expected a tree","messagePattern":"Expected a tree","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/server/LexicalizedParserClient.java","lineNumber":147,"sourceCode":"   */\n  public Tree getTree(String query) \n    throws IOException\n  {\n    Socket socket = new Socket(host, port);\n\n    Writer out = new OutputStreamWriter(socket.getOutputStream(), \"utf-8\");\n    out.write(\"tree \" + query + \"\\n\");\n    out.flush();\n\n    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());\n    Object o;\n    try {\n      o = ois.readObject();\n    } catch (ClassNotFoundException e) {\n      throw new RuntimeException(e);\n    }\n    if (!(o instanceof Tree)) {\n      throw new IllegalArgumentException(\"Expected a tree\");\n    }\n    Tree tree = (Tree) o;\n\n    socket.close();\n    return tree;\n  }\n\n  /**\n   * Tell the server to exit\n   */\n  public void sendQuit() \n    throws IOException\n  {\n    Socket socket = new Socket(host, port);\n    \n    Writer out = new OutputStreamWriter(socket.getOutputStream(), \"utf-8\");\n    out.write(\"quit\\n\");\n    out.flush();    ","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/server/LexicalizedParserClient.java#L129-L165","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the server request actually asks for a parse (e.g. 'parse') before calling getTree().","Match client and server CoreNLP versions so serialized response types agree.","Log the received object's class to see what the server actually returned.","Use the client's token/query methods for non-parse requests instead."],"exampleFix":"// before\nTree tree = client.getTree(\"parse the sentence\");\n// after\nString resp = client.query(\"parse\", \"the sentence\"); // inspect response first\nTree tree = client.getTree(\"parse\", \"the sentence\");","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"Object o = response; boolean isTree = (o instanceof edu.stanford.nlp.trees.Tree);","tryCatchPattern":"try {\n    Tree t = client.getTree(\"parse\", sentence);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Expected a tree\")) {\n        // server returned a non-Tree payload; check server version/request\n    }\n}","preventionTips":["Only call getTree for requests that return parse trees.","Keep client and server CoreNLP versions in sync.","Probe with query() first when integrating against an unfamiliar server."],"tags":["java","network","type-mismatch","nlp"],"backgroundTag":"unexpected-response-shape","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"}