{"record":{"id":"0dcb5d2969b35aa8","repo":"stanfordnlp/CoreNLP","slug":"expected-labels-to-have-indices","errorCode":null,"errorMessage":"Expected labels to have indices","messagePattern":"Expected labels to have indices","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/TreePrint.java","lineNumber":567,"sourceCode":"      if (formats.containsKey(\"conll2007\")) {\n        // CoNLL-X 2007 format: http://ilk.uvt.nl/conll/#dataformat\n        // wsg: This code should be retained (and not subsumed into EnglishGrammaticalStructure) so\n        //      that dependencies for other languages can be printed.\n        // wsg2011: This code currently ignores the dependency label since the present implementation\n        //          of mapDependencies() returns UnnamedDependency objects.\n        // TODO: if there is a GrammaticalStructureFactory available, use that instead of mapDependencies\n        Tree it = outputTree.deepCopy(outputTree.treeFactory(), CoreLabel.factory());\n        it.indexLeaves();\n\n        List<CoreLabel> tagged = it.taggedLabeledYield();\n        List<Dependency<Label, Label, Object>> sortedDeps = getSortedDeps(it, Filters.acceptFilter());\n\n        for (Dependency<Label, Label, Object> d : sortedDeps) {\n          if (!dependencyFilter.test(d)) {\n            continue;\n          }\n          if (!(d.dependent() instanceof HasIndex) || !(d.governor() instanceof HasIndex)) {\n            throw new IllegalArgumentException(\"Expected labels to have indices\");\n          }\n          HasIndex dep = (HasIndex) d.dependent();\n          HasIndex gov = (HasIndex) d.governor();\n\n          int depi = dep.index();\n          int govi = gov.index();\n\n          CoreLabel w = tagged.get(depi - 1);\n\n          // Used for both course and fine POS tag fields\n          String tag = PTBTokenizer.ptbToken2Text(w.tag());\n\n          String word = PTBTokenizer.ptbToken2Text(w.word());\n          String lemma = \"_\";\n          String feats = \"_\";\n          String pHead = \"_\";\n          String pDepRel = \"_\";\n          String depRel;","sourceCodeStart":549,"sourceCodeEnd":585,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/TreePrint.java#L549-L585","documentation":"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.","triggerScenarios":"Requesting dependency output formats (typedDependencies, conll, etc.) on trees whose labels are plain StringLabel/Word rather than indexed labels like CoreLabel with index() set.","commonSituations":"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.","solutions":["Use CoreLabel-based trees with indices set (run the indexer or build via the standard pipeline).","Ensure labels implement HasIndex and have index() populated before printing.","Switch the output format to one that does not require indices (e.g. penn) if dependencies are not needed."],"exampleFix":"// before\nTreePrint tp = new TreePrint(\"typedDependencies\");\ntp.printTree(stringLabelTree, pw); // StringLabel leaves\n// after\nTree t = ...; // tree with CoreLabel leaves and indices\ntp.printTree(t, pw);","handlingStrategy":"type-guard","validationCode":"for (Label w : tree.yield()) { if (!(w instanceof HasIndex) || ((HasIndex) w).index() < 0) { /* run indexing or fail fast */ } }","typeGuard":"boolean indexed(Tree t) { return t.yield().stream().allMatch(l -> l instanceof HasIndex && ((HasIndex) l).index() >= 0); }","tryCatchPattern":"try { tp.printTree(tree, pw); } catch (IllegalArgumentException e) { pw.print(tree.pennString()); }","preventionTips":["Use CoreLabel leaves with sentence/word indices set.","Run indexing steps in the pipeline before dependency output.","Fall back to penn format when indices are unavailable."],"tags":["nlp","dependencies","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}