{"record":{"id":"d7ec7ca8ea1a2dec","repo":"stanfordnlp/CoreNLP","slug":"expected-tree-labels-to-be-corelabel","errorCode":null,"errorMessage":"Expected tree labels to be CoreLabel","messagePattern":"Expected tree labels to be CoreLabel","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/shiftreduce/CreateTransitionSequence.java","lineNumber":75,"sourceCode":"        createTransitionSequenceHelper(transitions, tree, compoundUnary, rootOnlyStates);\n        transitions.add(new CompoundUnaryTransition(labels, isRoot));\n      } else {\n        createTransitionSequenceHelper(transitions, tree.children()[0], compoundUnary, rootOnlyStates);\n        transitions.add(new UnaryTransition(tree.label().value(), isRoot));\n      }\n    } else if (tree.children().length == 2) {\n      createTransitionSequenceHelper(transitions, tree.children()[0], compoundUnary, rootOnlyStates);\n      createTransitionSequenceHelper(transitions, tree.children()[1], compoundUnary, rootOnlyStates);\n\n      // This is the tricky part... need to decide if the binary\n      // transition is a left or right transition.  This is done by\n      // looking at the existing heads of this node and its two\n      // children.  The expectation is that the tree already has heads\n      // assigned; otherwise, exception is thrown\n      if (!(tree.label() instanceof CoreLabel) || \n          !(tree.children()[0].label() instanceof CoreLabel) ||\n          !(tree.children()[1].label() instanceof CoreLabel)) {\n        throw new IllegalArgumentException(\"Expected tree labels to be CoreLabel\");\n      }\n      CoreLabel label = (CoreLabel) tree.label();\n      CoreLabel leftLabel = (CoreLabel) tree.children()[0].label();\n      CoreLabel rightLabel = (CoreLabel) tree.children()[1].label();\n      CoreLabel head = label.get(TreeCoreAnnotations.HeadWordLabelAnnotation.class);\n      CoreLabel leftHead = leftLabel.get(TreeCoreAnnotations.HeadWordLabelAnnotation.class);\n      CoreLabel rightHead = rightLabel.get(TreeCoreAnnotations.HeadWordLabelAnnotation.class);\n      if (head == null || leftHead == null || rightHead == null) {\n        throw new IllegalArgumentException(\"Expected tree labels to have their heads assigned.  Failed at: \" + tree);\n      }\n      boolean isRoot = rootOnlyStates.contains(tree.label().value());\n      if (head == leftHead) {\n        transitions.add(new BinaryTransition(tree.label().value(), BinaryTransition.Side.LEFT, isRoot));\n      } else if (head == rightHead) {\n        transitions.add(new BinaryTransition(tree.label().value(), BinaryTransition.Side.RIGHT, isRoot));\n      } else {\n        throw new IllegalArgumentException(\"Heads were incorrectly assigned: tree's head is not matched to either the right or left head\");\n      }","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/shiftreduce/CreateTransitionSequence.java#L57-L93","documentation":"CreateTransitionSequence.createTransitionSequenceHelper() derives the head words of a binarized tree node and its two children to emit BinaryTransitions; it requires all three labels to be CoreLabel instances. If any label is another Label implementation, it throws this IllegalArgumentException, because only CoreLabel can carry the HeadWordLabelAnnotation it reads next.","triggerScenarios":"Calling createTransitionSequence on trees not preprocessed into CoreLabel-backed binarized form — e.g. raw trees from a different parser or trees whose children array has fewer than 2 non-null children (children()[1] null).","commonSituations":"Training the shift-reduce parser with a custom treebank loaded without the parser's tree normalization, or passing non-binarized/unheaded trees directly to the transition-sequence generator.","solutions":["Preprocess trees with the parser's TreeBinarizer/CoreLabel conversion (as ShiftReduceParser.train does) before generating transition sequences.","Ensure the tree is binarized so children()[0] and children()[1] exist.","Use Trees.toTree / the same TreebankLangParserParams pipeline that produced the training data.","In unit tests, build trees with CoreLabelFactory-labeled nodes."],"exampleFix":"// before\nList<Transition> trans = CreateTransitionSequence.createTransitionSequence(rawTree, false);\n// after\nTree binarized = Binarizer.binarez(rawTree); // or run parser's preprocessing\nList<Transition> trans = CreateTransitionSequence.createTransitionSequence(binarized, false);","handlingStrategy":"validation","validationCode":"boolean ok = tree.label() instanceof CoreLabel\n    && tree.children().length >= 2\n    && tree.children()[0].label() instanceof CoreLabel\n    && tree.children()[1].label() instanceof CoreLabel;\nif (!ok) throw new IllegalArgumentException(\"Tree must be binarized with CoreLabel nodes\");","typeGuard":"boolean isBinarizedCoreLabelTree(Tree t) {\n    return t.label() instanceof CoreLabel\n        && t.children().length == 2\n        && t.children()[0].label() instanceof CoreLabel\n        && t.children()[1].label() instanceof CoreLabel;\n}","tryCatchPattern":"try {\n    List<Transition> trans = CreateTransitionSequence.createTransitionSequence(tree, false);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().equals(\"Expected tree labels to be CoreLabel\")) {\n        // binarize + convert labels, then retry\n    }\n}","preventionTips":["Binarize trees before generating transition sequences.","Ensure the tree pipeline produces CoreLabel-backed nodes.","Reuse the trainer's preprocessing rather than calling the helper directly."],"tags":["java","type-mismatch","training","nlp"],"backgroundTag":"type-mismatch","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"}