{"record":{"id":"811ec2d1fbfa43f8","repo":"stanfordnlp/CoreNLP","slug":"stack-should-have-corelabel-nodes","errorCode":null,"errorMessage":"Stack should have CoreLabel nodes","messagePattern":"Stack should have CoreLabel nodes","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/shiftreduce/BinaryTransition.java","lineNumber":203,"sourceCode":"    Tree right = stack.peek();\n    stack = stack.pop();\n    Tree left = stack.peek();\n    stack = stack.pop();\n\n    Tree head;\n    switch(side) {\n    case LEFT:\n      head = left;\n      break;\n    case RIGHT:\n      head = right;\n      break;\n    default:\n      throw new IllegalArgumentException(\"Unknown side \" + side);\n    }\n\n    if (!(head.label() instanceof CoreLabel)) {\n      throw new IllegalArgumentException(\"Stack should have CoreLabel nodes\");\n    }\n    CoreLabel headLabel = (CoreLabel) head.label();\n\n    CoreLabel production = new CoreLabel();\n    production.setValue(label);\n    production.set(TreeCoreAnnotations.HeadWordLabelAnnotation.class, headLabel.get(TreeCoreAnnotations.HeadWordLabelAnnotation.class));\n    production.set(TreeCoreAnnotations.HeadTagLabelAnnotation.class, headLabel.get(TreeCoreAnnotations.HeadTagLabelAnnotation.class));\n    Tree newTop = new LabeledScoredTreeNode(production);\n    newTop.addChild(left);\n    newTop.addChild(right);\n\n    stack = stack.push(newTop);\n\n    return new State(stack, state.transitions.push(this), state.separators, state.sentence, state.tokenPosition, state.score + scoreDelta, false);\n  }\n\n  @Override\n  public boolean equals(Object o) {","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/shiftreduce/BinaryTransition.java#L185-L221","documentation":"After choosing the head in BinaryTransition.apply(), the code requires the head node's label to be a CoreLabel because it reads HeadWordLabelAnnotation from it. If the stack's top node label is some other Label implementation (e.g. a plain StringLabel or TagLabel), this IllegalArgumentException is thrown.","triggerScenarios":"Running the shift-reduce parser on a state whose stack trees were not built with CoreLabel nodes — e.g. feeding trees labeled with StringLabel/LabeledWord, or trees not preprocessed by the parser's tree-normalization pipeline.","commonSituations":"Custom transition states in unit tests built from unparsed/raw Trees, or bypassing ShiftReduceParser's preprocessing (which normally converts labels to CoreLabel and assigns heads).","solutions":["Run input trees through the same tree preprocessing the parser uses (convert labels to CoreLabel, assign head word annotations).","Use ShiftReduceParser.parse()/apply on states produced by the parser itself instead of hand-built states.","In tests, build stack nodes with CoreLabel instances and set TreeCoreAnnotations.HeadWordLabelAnnotation.","Verify the trees come from a TreebankLangParserParams pipeline that yields CoreLabel-backed trees."],"exampleFix":"// before\nTree node = treeFactory.newTreeNode(\"NP\", children); // StringLabel\n// after\nCoreLabel lbl = new CoreLabel(); lbl.setValue(\"NP\");\nTree node = treeFactory.newTreeNode(lbl, children);","handlingStrategy":"type-guard","validationCode":"if (!(head.label() instanceof CoreLabel))\n    throw new IllegalArgumentException(\"Stack head must be CoreLabel before applying binary transition\");","typeGuard":"boolean stackHasCoreLabels(State s) {\n    return s.stack.peek().label() instanceof edu.stanford.nlp.ling.CoreLabel;\n}","tryCatchPattern":"try {\n    transition.apply(state);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().equals(\"Stack should have CoreLabel nodes\")) {\n        // re-preprocess trees to CoreLabel and rebuild state\n    }\n}","preventionTips":["Always derive parser states from ShiftReduceParser's own preprocessing.","In custom code, label stack nodes with CoreLabel.","Assign HeadWordLabelAnnotation before transitions touch the trees."],"tags":["java","type-mismatch","internal","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"}