{"record":{"id":"dd5ddd323502b5e8","repo":"stanfordnlp/CoreNLP","slug":"dependenciestoconllxstring-coremap-does-not-have","errorCode":null,"errorMessage":"dependenciesToCoNLLXString: CoreMap does not have required TokensAnnotation.","messagePattern":"dependenciesToCoNLLXString: CoreMap does not have required TokensAnnotation\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/GrammaticalStructureConversionUtils.java","lineNumber":91,"sourceCode":"   * Returns a dependency tree in CoNNL-X format.\n   * It requires a CoreMap for the sentence with a TokensAnnotation.\n   * Each token has to contain a word and a POS tag.\n   *\n   * @param deps The list of TypedDependency relations.\n   * @param sentence The corresponding CoreMap for the sentence.\n   * @return Dependency tree in CoNLL-X format.\n   */\n  public static String dependenciesToCoNLLXString(Collection<TypedDependency> deps, CoreMap sentence) {\n    StringBuilder bf = new StringBuilder();\n\n    HashMap<Integer, TypedDependency> indexedDeps = new HashMap<>(deps.size());\n    for (TypedDependency dep : deps) {\n      indexedDeps.put(dep.dep().index(), dep);\n    }\n\n    List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);\n    if (tokens == null) {\n      throw new RuntimeException(\"dependenciesToCoNLLXString: CoreMap does not have required TokensAnnotation.\");\n    }\n    int idx = 1;\n\n    for (CoreLabel token : tokens) {\n      String word = token.value();\n      String pos = token.tag();\n      String cPos = (token.get(CoreAnnotations.CoarseTagAnnotation.class) != null) ?\n          token.get(CoreAnnotations.CoarseTagAnnotation.class) : pos;\n      String lemma = token.lemma() != null ? token.lemma() : \"_\";\n      Integer gov = indexedDeps.containsKey(idx) ? indexedDeps.get(idx).gov().index() : 0;\n      String reln = indexedDeps.containsKey(idx) ? indexedDeps.get(idx).reln().toString() : \"erased\";\n      String out = String.format(\"%d\\t%s\\t%s\\t%s\\t%s\\t_\\t%d\\t%s\\t_\\t_\\n\", idx, word, lemma, cPos, pos, gov, reln);\n      bf.append(out);\n      idx++;\n    }\n    return bf.toString();\n  }\n","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/GrammaticalStructureConversionUtils.java#L73-L109","documentation":"dependenciesToCoNLLXString serializes a list of TypedDependencies back to CoNLL-X format, which requires the original token texts, tags, and indices. It reads the tokens from the given CoreMap's TokensAnnotation; if the sentence was never tokenized/annotated, tokens is null and the method throws this RuntimeException.","triggerScenarios":"Calling dependenciesToCoNLLXString(deps, sentence) with a CoreMap (Sentence/Annotation) that lacks CoreAnnotations.TokensAnnotation — e.g. an Annotation created manually without running tokenization, or a Sentence built without tokens.","commonSituations":"Building an Annotation by hand for testing; passing only a parse result without the tokenized document; running the serializer on a pipeline output where the tokenize step was disabled.","solutions":["Run tokenization before serializing: include 'tokenize' in the StanfordCoreNLP pipeline or call the tokenizer so TokensAnnotation is set.","If constructing the Annotation manually, put a List<CoreLabel> under CoreAnnotations.TokensAnnotation.class.","Use the Sentence/CoreMap returned by a full pipeline (e.g. from CoreDocument tokens) rather than an empty shell object."],"exampleFix":"// before\nAnnotation sentence = new Annotation(text);\nString conll = GrammaticalStructureConversionUtils.dependenciesToCoNLLXString(deps, sentence);\n// after\nAnnotation sentence = new Annotation(text);\nnew StanfordCoreNLP(new Properties() {{ setProperty(\"annotators\", \"tokenize,ssplit\"); }}).annotate(sentence);\nString conll = GrammaticalStructureConversionUtils.dependenciesToCoNLLXString(deps, sentence);","handlingStrategy":"validation","validationCode":"List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);\nif (tokens == null || tokens.isEmpty()) {\n  throw new IllegalStateException(\"Sentence must be tokenized before dependenciesToCoNLLXString\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  String conll = dependenciesToCoNLLXString(deps, sentence);\n} catch (RuntimeException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"TokensAnnotation\")) {\n    pipeline.annotate(sentence); // lazily tokenize\n    conll = dependenciesToCoNLLXString(deps, sentence);\n  } else throw e;\n}","preventionTips":["Always run the tokenize/ssplit annotators before any dependency serialization.","Use CoreDocument/CoreSentence or pipeline-produced Annotations instead of hand-built ones.","Assert TokensAnnotation presence in unit tests around serialization helpers."],"tags":["corenlp","annotation","missing-annotation","serialization"],"backgroundTag":"missing-required-argument","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"}