{"record":{"id":"55ef910f13840fc1","repo":"stanfordnlp/CoreNLP","slug":"can-only-set-spans-on-trees-which-use-corelabel","errorCode":null,"errorMessage":"Can only set spans on trees which use CoreLabel","messagePattern":"Can only set spans on trees which use CoreLabel","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Tree.java","lineNumber":552,"sourceCode":"  public Set<Constituent> constituents(ConstituentFactory cf, boolean charLevel, Predicate<Tree> filter) {\n    Set<Constituent> constituentsSet = Generics.newHashSet();\n    constituents(constituentsSet, 0, cf, charLevel, filter, -1, 0);\n    return constituentsSet;\n  }\n\n  /**\n   * Same as int constituents but just puts the span as an IntPair\n   * in the CoreLabel of the nodes.\n   *\n   * @param left The left position to begin labeling from\n   * @return The index of the right frontier of the constituent\n   */\n  private int constituentsNodes(int left) {\n    if (isLeaf()) {\n      if (label() instanceof CoreLabel) {\n        ((CoreLabel) label()).set(CoreAnnotations.SpanAnnotation.class, new IntPair(left, left));\n      } else {\n        throw new UnsupportedOperationException(\"Can only set spans on trees which use CoreLabel\");\n      }\n      return (left + 1);\n    }\n    int position = left;\n\n    // enumerate through daughter trees\n    Tree[] kids = children();\n    for (Tree kid : kids)\n      position = kid.constituentsNodes(position);\n\n    //Parent span\n    if (label() instanceof CoreLabel) {\n      ((CoreLabel) label()).set(CoreAnnotations.SpanAnnotation.class, new IntPair(left, position - 1));\n    } else {\n      throw new UnsupportedOperationException(\"Can only set spans on trees which use CoreLabel\");\n    }\n\n    return position;","sourceCodeStart":534,"sourceCodeEnd":570,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Tree.java#L534-L570","documentation":"Tree.setSpans() annotates every node with its token span via CoreAnnotations.SpanAnnotation, which requires each node's label to be a CoreLabel. In constituentsNodes (leaf case) a label that is not a CoreLabel cannot hold the annotation, so the library throws UnsupportedOperationException.","triggerScenarios":"Calling tree.setSpans() on a tree whose leaf labels are not CoreLabel instances (e.g. StringLabel, WordTag, IndexedWord), which reaches the leaf branch of constituentsNodes.","commonSituations":"Parsing trees from string formats with a default (non-CoreLabel) TreeReader/label factory, then calling span-dependent methods; mixing trees built with TreeFactory(StringLabel) into CoreNLP annotation pipelines.","solutions":["Rebuild or re-label the tree using CoreLabel instances: use a TreeFactory with CoreLabelFactory / LabeledScoredTreeFactory(CoreLabelFactory.instance()) so all labels are CoreLabels","Convert labels via setLabel(new CoreLabel(...)) for every node, copying the value into the CoreLabel, before calling setSpans","Read the tree with TreeReader that specifies CoreLabel as the label type, then call setSpans"],"exampleFix":"// before: StringLabel leaves\nTreeFactory tf = new TreeFactory(StringLabel.factory());\ntree.setSpans(); // throws\n// after\nTreeFactory tf = new TreeFactory(CoreLabel.factory());\ntree.setSpans(); // OK","handlingStrategy":"type-guard","validationCode":"boolean allCoreLabels = true; for (Tree t : tree) { if (!(t.label() instanceof CoreLabel)) { allCoreLabels = false; break; } }","typeGuard":"boolean canSetSpans(Tree t) { return t.getLeaves().stream().allMatch(l -> l.label() instanceof CoreLabel); }","tryCatchPattern":"try { tree.setSpans(); } catch (UnsupportedOperationException e) { tree = relabelWithCoreLabels(tree); tree.setSpans(); }","preventionTips":["Read trees with CoreLabel-producing TreeReaders","Convert labels at tree-construction boundaries, not at annotation time","Assert all labels are CoreLabel in a pipeline entry point"],"tags":["unsupported-operation","nlp","corelabel"],"backgroundTag":"unsupported-operation","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"}