stanfordnlp/CoreNLP · error · IllegalStateException

Bad tree state:

Error message

Bad tree state: 

What it means

Thrown by Trees.toStructureDebugString when a subtree's state does not fit the expected cases the debug walker understands (e.g. a subtree that is neither a leaf with a simple label nor a node whose children classes are consistent). It indicates an internal tree-state assumption was violated while generating a structural description.

Solutions

  1. Inspect the offending subtree t to see why it doesn't match expected leaf/internal shapes
  2. Build trees with standard factories (LabeledScoredTreeFactory) before calling debug utilities
  3. Validate the tree (e.g. check children non-null for internal nodes) before invoking toStructureDebugString

Example fix

// before
String dbg = Trees.toStructureDebugString(customTree);
// after
for (Tree st : customTree) {
  if (!st.isLeaf() && (st.children() == null || st.children().length == 0)) {
    throw new IllegalStateException("malformed internal node: " + st);
  }
}
String dbg = Trees.toStructureDebugString(customTree);
Defensive patterns

Strategy: validation

Validate before calling

boolean wellFormed(Tree t) {
  for (Tree st : t) {
    if (!st.isLeaf() && (st.children() == null || st.children().length == 0)) return false;
  }
  return true;
}

Type guard

boolean isDebuggable(Tree t) {
  return t != null && t.label() != null && wellFormed(t);
}

Try / catch

try {
  String dbg = Trees.toStructureDebugString(t);
} catch (IllegalStateException e) {
  logger.error("bad tree state for {}: {}", t, e.getMessage());
}

Prevention

When it happens

Trigger: Calling toStructureDebugString on a Tree whose internal structure violates the method's assumptions, such as a non-leaf node whose children list is null or of unexpected class, or a tree built by a custom TreeFactory producing inconsistent node types.

Common situations: Debugging trees constructed by third-party or hand-rolled TreeFactory implementations; trees mutated concurrently or corrupted by custom transformations before inspection.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/130f421419f7452b. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/trees/Trees.java:493

        }
        if (tagLabels == null) {
          tagLabels = StringUtils.getShortClassName(slCl);
        } else if ( ! tagLabels.equals(slCl)) {
          tagLabels = "mixed";
        }
      } else if (st.isLeaf()) {
        if (leaves == null) {
          leaves = stCl;
        } else if ( ! leaves.equals(stCl)) {
          leaves = "mixed";
        }
        if (leafLabels == null) {
          leafLabels = slCl;
        } else if ( ! leafLabels.equals(slCl)) {
          leafLabels = "mixed";
        }
      } else {
        throw new IllegalStateException("Bad tree state: " + t);
      }
    } // end for Tree st : this
    StringBuilder sb = new StringBuilder();
    sb.append("Tree with root of class ").append(tCl).append(" and factory ").append(tfCl);
    sb.append(" and root label class ").append(lCl).append(" and factory ").append(lfCl);
    if ( ! otherClasses.isEmpty()) {
      sb.append(" and the following classes also found within the tree: ").append(otherClasses);
      return " with " + nodes + " interior nodes and " + leaves +
        " leaves, and " + phraseLabels + " phrase labels, " +
        tagLabels + " tag labels, and " + leafLabels + " leaf labels.";
    } else {
      sb.append(" (and uniform use of these Tree and Label classes throughout the tree).");
    }
    return sb.toString();
  }


  /** Turns a sentence into a flat phrasal tree.

View on GitHub (pinned to 1b7edd19c4)