stanfordnlp/CoreNLP · error · IllegalArgumentException

Label argument lacks IndexAnnotation.

Error message

Label argument lacks IndexAnnotation.

What it means

UnnamedConcreteDependency stores integer head indexes from its labels; it requires the governor (regent) label to implement HasIndex so index() can be read. If the regent label lacks IndexAnnotation (does not implement HasIndex), the constructor throws IllegalArgumentException.

Solutions

  1. Use labels implementing HasIndex (CoreLabel with index set) for both regent and dependent
  2. Set the index on labels before constructing the dependency ((HasIndex) label).setIndex(i))
  3. Fall back to UnnamedDependency if you don't need index information

Example fix

// before
new UnnamedConcreteDependency(new StringLabel("ate"), new StringLabel("dog"));
// after
CoreLabel gov = new CoreLabel(); gov.setWord("ate"); gov.setIndex(2);
CoreLabel dep = new CoreLabel(); dep.setWord("dog"); dep.setIndex(3);
new UnnamedConcreteDependency(gov, dep);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(regent instanceof HasIndex) || !(((HasIndex) regent).index() >= 0)) {
  throw new IllegalArgumentException("regent lacks index annotation");
}

Type guard

boolean isIndexedLabel(Label l) {
  return l instanceof HasIndex && ((HasIndex) l).index() >= 0;
}

Try / catch

try {
  dep = new UnnamedConcreteDependency(regent, dependent);
} catch (IllegalArgumentException e) {
  dep = new UnnamedDependency(regent, dependent); // fallback without indexes
}

Prevention

When it happens

Trigger: Constructing new UnnamedConcreteDependency(regent, dependent) where the regent Label is a plain StringLabel/Word (no index annotation) instead of a CoreLabel or other HasIndex implementation.

Common situations: Creating dependencies from trees parsed with default factories that don't attach index annotations; forgetting to run indexing (e.g. via setSentIndex/word indexing) before building dependency objects.

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

Appendix: source

Thrown at src/edu/stanford/nlp/trees/UnnamedConcreteDependency.java:47

    headIndex = regentIndex;
    depIndex = dependentIndex;
  }
  
  public UnnamedConcreteDependency(Label regent, int regentIndex, Label dependent, int dependentIndex) {
    super(regent, dependent);
    
    headIndex = regentIndex;
    depIndex = dependentIndex;
  }

  public UnnamedConcreteDependency(Label regent, Label dependent) {
    super(regent, dependent);

    if (governor() instanceof HasIndex) {
      headIndex = ((HasIndex) governor()).index();
    } else {
      throw new IllegalArgumentException("Label argument lacks IndexAnnotation.");
    }
    if (dependent() instanceof HasIndex) {
      depIndex = ((HasIndex) dependent()).index();
    } else {
      throw new IllegalArgumentException("Label argument lacks IndexAnnotation.");
    }
  }
  
  public int getGovernorIndex() { return headIndex; }
  
  public int getDependentIndex() { return depIndex; }
  
  @Override
  public int hashCode() {
    return headIndex * (depIndex << 16);
  }

  @Override

View on GitHub (pinned to 1b7edd19c4)