{"record":{"id":"1abf4b0b502d8a42","repo":"stanfordnlp/CoreNLP","slug":"expected-leaves-to-be-corelabels","errorCode":null,"errorMessage":"Expected leaves to be CoreLabels","messagePattern":"Expected leaves to be CoreLabels","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Tree.java","lineNumber":1019,"sourceCode":"   */\n  public String pennString() {\n    StringWriter sw = new StringWriter();\n    pennPrint(new PrintWriter(sw));\n    return sw.toString();\n  }\n\n  /**\n   * Return String of leaves spanned by this tree assuming they are CoreLabel's\n   * Throws an IllegalArgumentException if the leaves are not CoreLabels that contain\n   * text info as in the typical use case of a Tree generated by a pipeline\n   *\n   * @return The text of the span of this Tree\n   */\n  public String spanString() {\n    // check this Tree supports this method by having properly populated CoreLabel's\n    List<Tree> leaves = this.getLeaves();\n    if (!(leaves.get(0).label() instanceof CoreLabel)) {\n      throw new IllegalArgumentException(\"Expected leaves to be CoreLabels\");\n    } else if (((CoreLabel) leaves.get(0).label()).word() == null) {\n      throw new IllegalArgumentException(\"Expected CoreLabel's to have text\");\n    } else if (((CoreLabel) leaves.get(0).label()).after() == null) {\n      throw new IllegalArgumentException(\"Expected CoreLabel's to have after() text\");\n    }\n    List<CoreLabel> coreLabels = this.getLeaves().stream().map(l -> ((CoreLabel) l.label())).collect(Collectors.toList());\n    // reconstruct original String from CoreLabel fields\n    String spanString = coreLabels.subList(0, Math.max(0, coreLabels.size()-1)).stream().map(\n            cl -> cl.word()+cl.after()).collect(Collectors.joining(\"\"));\n    // don't add the after of the last word\n    spanString += coreLabels.get(coreLabels.size()-1).word();\n    return spanString;\n  }\n\n\n  /**\n   * Print the tree as done in Penn Treebank merged files.\n   * The formatting should be exactly the same, but we don't print the","sourceCodeStart":1001,"sourceCodeEnd":1037,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Tree.java#L1001-L1037","documentation":"Tree.spanString() reconstructs the original text covered by the tree from its leaves, which must be CoreLabels carrying word and after() (following whitespace) data. If the first leaf's label is not a CoreLabel, the method cannot read the needed fields and throws IllegalArgumentException immediately.","triggerScenarios":"Calling spanString() on a tree whose leaf labels are not CoreLabel (e.g. StringLabel/WordLabel trees loaded from a reader or built with a non-CoreLabel factory).","commonSituations":"Trees parsed from PTB files with default label factories, trees returned from third-party parsers, or trees built in unit tests with simple labels, then trying to extract surface text via spanString().","solutions":["Rebuild the tree with CoreLabel leaves (CoreLabel-based TreeFactory) so word()/after() are populated","Convert leaf labels to CoreLabels copying word and after values before calling spanString()","Read trees via a TreeReader configured to produce CoreLabel leaves"],"exampleFix":"// before\nString s = stringLabeledTree.spanString();\n// after\nCoreLabel cl = new CoreLabel(); cl.setWord(t.label().value());\nleaf.setLabel(cl);\nString s = tree.spanString();","handlingStrategy":"type-guard","validationCode":"if (!(tree.getLeaves().get(0).label() instanceof CoreLabel)) throw new IllegalStateException(\"leaves are not CoreLabels; cannot call spanString\");","typeGuard":"boolean hasCoreLabelLeaves(Tree t) { return !t.getLeaves().isEmpty() && t.getLeaves().get(0).label() instanceof CoreLabel; }","tryCatchPattern":"try { return tree.spanString(); } catch (IllegalArgumentException e) { return joinLeafValues(tree); }","preventionTips":["Ensure tree leaves are CoreLabel with populated word/after","Validate leaf label type before text-extraction calls","Standardize on CoreLabel factories across the pipeline"],"tags":["validation","nlp","corelabel"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}