{"record":{"id":"a1b901b96ec0ccd9","repo":"stanfordnlp/CoreNLP","slug":"required-a-tree-with-corelabels","errorCode":null,"errorMessage":"Required a tree with CoreLabels","messagePattern":"Required a tree with CoreLabels","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/sentiment/SentimentPipeline.java","lineNumber":81,"sourceCode":"  private SentimentPipeline() {} // static methods\n\n  /**\n   * Sets the labels on the tree (except the leaves) to be the integer\n   * value of the sentiment prediction.  Makes it easy to print out\n   * with Tree.toString()\n   */\n  private static void setSentimentLabels(Tree tree) {\n    if (tree.isLeaf()) {\n      return;\n    }\n\n    for (Tree child : tree.children()) {\n      setSentimentLabels(child);\n    }\n\n    Label label = tree.label();\n    if (!(label instanceof CoreLabel)) {\n      throw new IllegalArgumentException(\"Required a tree with CoreLabels\");\n    }\n    CoreLabel cl = (CoreLabel) label;\n    cl.setValue(Integer.toString(RNNCoreAnnotations.getPredictedClass(tree)));\n  }\n\n  /**\n   * Sets the labels on the tree to be the indices of the nodes.\n   * Starts counting at the root and does a postorder traversal.\n   */\n  private static int setIndexLabels(Tree tree, int index) {\n    if (tree.isLeaf()) {\n      return index;\n    }\n\n    tree.label().setValue(Integer.toString(index));\n    index++;\n    for (Tree child : tree.children()) {\n      index = setIndexLabels(child, index);","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/sentiment/SentimentPipeline.java#L63-L99","documentation":"SentimentPipeline.setSentimentLabels recursively annotates a tree with predicted sentiment classes and requires every node's Label to be an instance of CoreLabel. If any node carries a plain StringLabel/LabeledWord or another Label implementation, the cast would be unsafe, so an IllegalArgumentException is thrown instead. This is an input precondition failure: the tree given to the pipeline is not backed by the expected label type.","triggerScenarios":"Calling setSentimentLabels (directly or via outputTree in SentimentPipeline) with a Tree whose nodes were built by a parser or TreeReader that produces StringLabel or other non-CoreLabel Label implementations instead of CoreLabel.","commonSituations":"Loading trees from a plain-text .mrg/.tree file with a generic TreeReader; combining trees produced by an older parser setup with the sentiment model; programmatically constructing trees with Tree.valueOf() without forcing CoreLabel backing; swapping tokenizer/parser props so downstream trees lose CoreLabel annotation.","solutions":["Ensure the input trees go through a pipeline (StanfordCoreNLP tokenize/parse) that produces CoreLabel-backed nodes before sentiment labeling.","If reading trees from text, re-label them, e.g. with a TreeNormalizer/TreeTransformer that replaces each node label with a CoreLabel carrying the same value.","Check which TreeReader/parser model was used; switch to one configured with CoreLabel factory (e.g. via ParserOptions/parser props).","As a last resort, convert labels manually: clone the tree and for each node setLabel(new CoreLabel()) copying the original value before calling the pipeline."],"exampleFix":"// before\nTree t = Tree.valueOf(\"(ROOT (S (NP I) (VP (V like) (NP it))))\");\nSentimentPipeline.setSentimentLabels(t); // StringLabel nodes -> IllegalArgumentException\n\n// after\nTree read = treeReader.readTree(...); // reader configured with CoreLabel factory\n// or rebuild labels:\nfor (Tree node : t) {\n  CoreLabel cl = new CoreLabel();\n  cl.setValue(node.label().value());\n  node.setLabel(cl);\n}","handlingStrategy":"type-guard","validationCode":"boolean hasCoreLabels(Tree t) {\n  for (Tree node : t) {\n    if (!(node.label() instanceof CoreLabel)) return false;\n  }\n  return true;\n}\n// call before pipeline: if (!hasCoreLabels(tree)) rebuildLabels(tree);","typeGuard":"static boolean isCoreLabelTree(Tree t) {\n  if (t == null || !(t.label() instanceof CoreLabel)) return false;\n  for (Tree child : t.children()) {\n    if (!isCoreLabelTree(child)) return false;\n  }\n  return true;\n}","tryCatchPattern":"try {\n  SentimentPipeline.setSentimentLabels(tree);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"CoreLabels\")) {\n    tree = relabelAsCoreLabels(tree); // fallback conversion\n    SentimentPipeline.setSentimentLabels(tree);\n  } else throw e;\n}","preventionTips":["Always obtain trees from a StanfordCoreNLP/lexparser setup configured for CoreLabel output.","Write a unit test asserting every node label of your input trees is a CoreLabel.","When reading trees from text, apply a relabeling TreeTransformer immediately after parsing.","Avoid mixing trees from different parser versions/configurations in one pipeline."],"tags":["java","input-validation","nlp","type-mismatch"],"backgroundTag":"incompatible-source-type","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"}