stanfordnlp/CoreNLP · error · IllegalArgumentException

Only operates on CoreLabels

Error message

Only operates on CoreLabels

What it means

replacePOSTag requires the node's own label to be a CoreLabel so it can cast and mutate the POS tag. If the Tree carries a different Label implementation (e.g. StringLabel, Tag, or a custom label), the library throws IllegalArgumentException rather than doing an unsafe cast. Note the same message is also thrown for the child word node (error 1332).

Solutions

  1. Build/read trees with a CoreLabel factory, e.g. new LabeledScoredTreeReaderFactory() paired with CoreLabel.factory() or FrenchTreeReaderFactory which uses CoreLabels
  2. Convert labels before normalizing: t.setLabel(CoreLabel.fromString(label.value()))
  3. Run trees through an earlier pipeline stage (e.g. TreeToText / TreeCoreAnnotations) that guarantees CoreLabel labels
  4. Check t.label() instanceof CoreLabel before calling the normalizer

Example fix

// before
Tree t = new LabeledScoredTreeNode(new StringLabel("NN"), kids);
normalizer.normalizePreterminal(t, morpho); // throws

// after
CoreLabel cl = CoreLabel.fromString("NN");
Tree t = new LabeledScoredTreeNode(cl, kids);
normalizer.normalizePreterminal(t, morpho); // ok
Defensive patterns

Strategy: validation

Validate before calling

if (!(tree.label() instanceof CoreLabel))
  throw new IllegalArgumentException("POS label must be CoreLabel; got " + tree.label().getClass());

Type guard

static boolean hasCoreLabel(Tree t) {
  return t.label() instanceof CoreLabel;
}

Try / catch

try {
  normalizer.normalizePreterminal(tree, morpho);
} catch (IllegalArgumentException e) {
  // convert label and retry once
  tree.setLabel(CoreLabel.fromString(tree.label().value()));
  normalizer.normalizePreterminal(tree, morpho);
}

Prevention

When it happens

Trigger: Passing a preterminal Tree whose label() is not a CoreLabel to normalizePreterminal/replacePOSTag — typically when trees were read with a non-CoreLabel TreeReader or Label factory (e.g. LabeledScoredTreeReaderFactory with StringLabel).

Common situations: Mixing tree sources: trees parsed with default readers then fed to the French normalizer that expects CoreLabels; constructing trees manually with new StringLabel("NN"); swapping TreeReaderFactory in a pipeline but keeping the normalizer stage.

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/7ef215c3b53c1561. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/trees/international/french/FrenchTreeNormalizer.java:74

  @Override
  public String normalizeTerminal(String terminal) {
    if(terminal == null) return terminal;

    return super.normalizeTerminal(terminal).intern();
  }

  @Override
  public String normalizeNonterminal(String category) {
    return super.normalizeNonterminal(category).intern();
  }

  private static void replacePOSTag(Tree t, MorphoFeatureSpecification morpho) {
    if (!t.isPreTerminal()) {
      throw new IllegalArgumentException("Can only operate on preterminals");
    }

    if (!(t.label() instanceof CoreLabel)) {
      throw new IllegalArgumentException("Only operates on CoreLabels");
    }
    CoreLabel label = (CoreLabel) t.label();

    Tree child = t.children()[0];
    if (!(child.label() instanceof CoreLabel)) {
      throw new IllegalArgumentException("Only operates on CoreLabels");
    }
    CoreLabel childLabel = (CoreLabel) child.label();

    // Morphological Analysis
    String morphStr = childLabel.originalText();
    if (morphStr == null || morphStr.equals("")) {
      morphStr = label.value();
      // POS subcategory
      String subCat = childLabel.category();
      if (subCat != null && subCat != "") {
        morphStr += "-" + subCat + "--";
      } else {

View on GitHub (pinned to 1b7edd19c4)