{"record":{"id":"ed746936f8108ab1","repo":"stanfordnlp/CoreNLP","slug":"expected-corelabels-in-the-trees","errorCode":null,"errorMessage":"Expected CoreLabels in the trees","messagePattern":"Expected CoreLabels in the trees","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Tree.java","lineNumber":1158,"sourceCode":"    } else {\n      Tree head = hf.determineHead(this);\n      if (head != null) {\n        return head.headPreTerminal(hf);\n      }\n      log.info(\"Head preterminal is null: \" + this);\n      return null;\n    }\n  }\n\n  /**\n   * Finds the head words of each tree and assigns\n   * HeadWordLabelAnnotation on each node pointing to the correct\n   * CoreLabel.  This relies on the nodes being CoreLabels, so it\n   * throws an IllegalArgumentException if this is ever not true.\n   */\n  public void percolateHeadAnnotations(HeadFinder hf) {\n    if (!(label() instanceof CoreLabel)) {\n      throw new IllegalArgumentException(\"Expected CoreLabels in the trees\");\n    }\n    CoreLabel nodeLabel = (CoreLabel) label();\n\n    if (isLeaf()) {\n      return;\n    }\n\n    if (isPreTerminal()) {\n      nodeLabel.set(TreeCoreAnnotations.HeadWordLabelAnnotation.class, (CoreLabel) children()[0].label());\n      nodeLabel.set(TreeCoreAnnotations.HeadTagLabelAnnotation.class, nodeLabel);\n      return;\n    }\n\n    for (Tree kid : children()) {\n      kid.percolateHeadAnnotations(hf);\n    }\n\n    final Tree head = hf.determineHead(this);","sourceCodeStart":1140,"sourceCodeEnd":1176,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Tree.java#L1140-L1176","documentation":"percolateHeadAnnotations(HeadFinder) stores a HeadWordLabelAnnotation on each node pointing to the head word's CoreLabel; this requires every node label to be a CoreLabel. When the node's label is not a CoreLabel it throws IllegalArgumentException, as documented in its javadoc.","triggerScenarios":"Calling percolateHeadAnnotations(hf) on trees whose labels are not CoreLabel (StringLabel/WordTag trees), typically on the root — the check runs before recursion.","commonSituations":"Semantic/parsing pipelines needing head-word features (e.g. for ancestor/head-word models) fed with trees loaded via default readers using simple labels; mixing tree sources from different parsers.","solutions":["Load/build the trees with CoreLabel labels (CoreLabel-based TreeFactory or reader option) before percolating annotations","Recursively convert all node labels to CoreLabels (copying value and, if needed, word/tag), then call percolateHeadAnnotations","Verify with a pre-check that every label() instanceof CoreLabel before invoking"],"exampleFix":"// before\ntree.percolateHeadAnnotations(hf); // StringLabel tree -> throws\n// after\nTreeFactory tf = new TreeFactory(CoreLabel.factory());\nTree coreTree = rebuildWithCoreLabels(tree, tf);\ncoreTree.percolateHeadAnnotations(hf);","handlingStrategy":"type-guard","validationCode":"if (!(tree.label() instanceof CoreLabel)) throw new IllegalStateException(\"tree labels must be CoreLabels for percolateHeadAnnotations\");","typeGuard":"boolean allCoreLabels(Tree t) { return t.subTrees().stream().allMatch(n -> n.label() instanceof CoreLabel); }","tryCatchPattern":"try { tree.percolateHeadAnnotations(hf); } catch (IllegalArgumentException e) { tree = relabelWithCoreLabels(tree); tree.percolateHeadAnnotations(hf); }","preventionTips":["Load trees with CoreLabel labels from the start","Convert labels at ingestion boundaries before annotation passes","Assert label types at pipeline entry points"],"tags":["validation","nlp","corelabel"],"backgroundTag":"invalid-argument-value","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"}