stanfordnlp/CoreNLP · error · IllegalArgumentException

Expected labels to have indices

Error message

Expected labels to have indices

What it means

When printing typed dependencies, TreePrint.printTreeInternal needs the index() of both the dependent and governor labels to sort/emit dependencies. If either label does not implement HasIndex (i.e. words lack sentence indices), it throws IllegalArgumentException.

Solutions

  1. Use CoreLabel-based trees with indices set (run the indexer or build via the standard pipeline).
  2. Ensure labels implement HasIndex and have index() populated before printing.
  3. Switch the output format to one that does not require indices (e.g. penn) if dependencies are not needed.

Example fix

// before
TreePrint tp = new TreePrint("typedDependencies");
tp.printTree(stringLabelTree, pw); // StringLabel leaves
// after
Tree t = ...; // tree with CoreLabel leaves and indices
tp.printTree(t, pw);
Defensive patterns

Strategy: type-guard

Validate before calling

for (Label w : tree.yield()) { if (!(w instanceof HasIndex) || ((HasIndex) w).index() < 0) { /* run indexing or fail fast */ } }

Type guard

boolean indexed(Tree t) { return t.yield().stream().allMatch(l -> l instanceof HasIndex && ((HasIndex) l).index() >= 0); }

Try / catch

try { tp.printTree(tree, pw); } catch (IllegalArgumentException e) { pw.print(tree.pennString()); }

Prevention

When it happens

Trigger: Requesting dependency output formats (typedDependencies, conll, etc.) on trees whose labels are plain StringLabel/Word rather than indexed labels like CoreLabel with index() set.

Common situations: Printing dependencies from trees built manually without running indexing; using a custom label type; forgetting to run the SentenceIndexCorpusTreeProcessor / index steps in the pipeline before output.

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

Appendix: source

Thrown at src/edu/stanford/nlp/trees/TreePrint.java:567

      if (formats.containsKey("conll2007")) {
        // CoNLL-X 2007 format: http://ilk.uvt.nl/conll/#dataformat
        // wsg: This code should be retained (and not subsumed into EnglishGrammaticalStructure) so
        //      that dependencies for other languages can be printed.
        // wsg2011: This code currently ignores the dependency label since the present implementation
        //          of mapDependencies() returns UnnamedDependency objects.
        // TODO: if there is a GrammaticalStructureFactory available, use that instead of mapDependencies
        Tree it = outputTree.deepCopy(outputTree.treeFactory(), CoreLabel.factory());
        it.indexLeaves();

        List<CoreLabel> tagged = it.taggedLabeledYield();
        List<Dependency<Label, Label, Object>> sortedDeps = getSortedDeps(it, Filters.acceptFilter());

        for (Dependency<Label, Label, Object> d : sortedDeps) {
          if (!dependencyFilter.test(d)) {
            continue;
          }
          if (!(d.dependent() instanceof HasIndex) || !(d.governor() instanceof HasIndex)) {
            throw new IllegalArgumentException("Expected labels to have indices");
          }
          HasIndex dep = (HasIndex) d.dependent();
          HasIndex gov = (HasIndex) d.governor();

          int depi = dep.index();
          int govi = gov.index();

          CoreLabel w = tagged.get(depi - 1);

          // Used for both course and fine POS tag fields
          String tag = PTBTokenizer.ptbToken2Text(w.tag());

          String word = PTBTokenizer.ptbToken2Text(w.word());
          String lemma = "_";
          String feats = "_";
          String pHead = "_";
          String pDepRel = "_";
          String depRel;

View on GitHub (pinned to 1b7edd19c4)