stanfordnlp/CoreNLP · error · IllegalArgumentException

Stack should have CoreLabel nodes

Error message

Stack should have CoreLabel nodes

What it means

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.

Solutions

  1. Run input trees through the same tree preprocessing the parser uses (convert labels to CoreLabel, assign head word annotations).
  2. Use ShiftReduceParser.parse()/apply on states produced by the parser itself instead of hand-built states.
  3. In tests, build stack nodes with CoreLabel instances and set TreeCoreAnnotations.HeadWordLabelAnnotation.
  4. Verify the trees come from a TreebankLangParserParams pipeline that yields CoreLabel-backed trees.

Example fix

// before
Tree node = treeFactory.newTreeNode("NP", children); // StringLabel
// after
CoreLabel lbl = new CoreLabel(); lbl.setValue("NP");
Tree node = treeFactory.newTreeNode(lbl, children);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(head.label() instanceof CoreLabel))
    throw new IllegalArgumentException("Stack head must be CoreLabel before applying binary transition");

Type guard

boolean stackHasCoreLabels(State s) {
    return s.stack.peek().label() instanceof edu.stanford.nlp.ling.CoreLabel;
}

Try / catch

try {
    transition.apply(state);
} catch (IllegalArgumentException e) {
    if (e.getMessage().equals("Stack should have CoreLabel nodes")) {
        // re-preprocess trees to CoreLabel and rebuild state
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/811ec2d1fbfa43f8. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/parser/shiftreduce/BinaryTransition.java:203

    Tree right = stack.peek();
    stack = stack.pop();
    Tree left = stack.peek();
    stack = stack.pop();

    Tree head;
    switch(side) {
    case LEFT:
      head = left;
      break;
    case RIGHT:
      head = right;
      break;
    default:
      throw new IllegalArgumentException("Unknown side " + side);
    }

    if (!(head.label() instanceof CoreLabel)) {
      throw new IllegalArgumentException("Stack should have CoreLabel nodes");
    }
    CoreLabel headLabel = (CoreLabel) head.label();

    CoreLabel production = new CoreLabel();
    production.setValue(label);
    production.set(TreeCoreAnnotations.HeadWordLabelAnnotation.class, headLabel.get(TreeCoreAnnotations.HeadWordLabelAnnotation.class));
    production.set(TreeCoreAnnotations.HeadTagLabelAnnotation.class, headLabel.get(TreeCoreAnnotations.HeadTagLabelAnnotation.class));
    Tree newTop = new LabeledScoredTreeNode(production);
    newTop.addChild(left);
    newTop.addChild(right);

    stack = stack.push(newTop);

    return new State(stack, state.transitions.push(this), state.separators, state.sentence, state.tokenPosition, state.score + scoreDelta, false);
  }

  @Override
  public boolean equals(Object o) {

View on GitHub (pinned to 1b7edd19c4)