stanfordnlp/CoreNLP · error · RuntimeException
Tree.valueOf() tree construction failed
Error message
Tree.valueOf() tree construction failed
What it means
Tree.valueOf(str, trf) parses a bracketed tree string using the given TreeReaderFactory. If the underlying reader throws IOException during parsing, it is rethrown as a RuntimeException with this message, meaning the tree string could not be read/constructed as a valid Tree.
Solutions
- Validate/repair the tree string: balanced parentheses, non-empty, one root node (pennPrint-style format)
- Print the offending string; check for truncation upstream in your pipeline
- Use the correct TreeReaderFactory for your format, or the default Tree.valueOf(str) for standard PTB bracketed format
- Wrap valueOf in try-catch for RuntimeException to handle dirty inputs gracefully
Example fix
// before
Tree t = Tree.valueOf(brokenLine); // IOException wrapped -> RuntimeException
// after
String s = brokenLine.trim();
if (s.isEmpty() || !balanced(s)) { skip(s); }
Tree t = Tree.valueOf(s); Defensive patterns
Strategy: validation
Validate before calling
String s = str == null ? null : str.trim();
if (s == null || s.isEmpty() || !balancedBrackets(s)) throw new IllegalArgumentException("bad tree string"); Type guard
boolean looksLikeTree(String s) { return s != null && s.trim().startsWith("(") && s.trim().endsWith(")") && balancedBrackets(s.trim()); } Try / catch
try { Tree t = Tree.valueOf(str); } catch (RuntimeException e) { log.warn("Unparseable tree: {}", str); } Prevention
- Validate bracket balance and non-empty input before valueOf
- Check upstream pipeline output for truncation
- Match TreeReaderFactory to your tree format
- Catch RuntimeException around valueOf for dirty data
When it happens
Trigger: Passing a malformed Penn Treebank string (unbalanced brackets, missing labels, empty input, null) to Tree.valueOf; a TreeReaderFactory whose readTree fails on the format.
Common situations: Trees copied from papers/logs with mangled brackets or line breaks; truncated output from a previous parse step; wrong TreeReaderFactory for the input format.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- Bad number put into wordToNumber. Word is: \"" + input +…
- Error in wordToNumber function.
- Bad number put into wordToNumber. Word is: \"" + curPart +…
- Doesn't do k best yet
- Doesn't do best parses yet
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/8fd75e4523e0c56a.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/Tree.java:2412
return valueOf(str, new LabeledScoredTreeReaderFactory());
}
/**
* This gives you a tree from a String representation (as a
* bracketed Tree, of the kind produced by {@code toString()},
* {@code pennPrint()}, or as in the Penn Treebank.
* It's not the most efficient thing to do for heavy duty usage.
*
* @param str The tree as a bracketed list in a String.
* @param trf The TreeFactory used to make the new Tree
* @return The Tree
* @throws RuntimeException If the Tree format is not valid
*/
public static Tree valueOf(String str, TreeReaderFactory trf) {
try {
return trf.newTreeReader(new StringReader(str)).readTree();
} catch (IOException ioe) {
throw new RuntimeException("Tree.valueOf() tree construction failed", ioe);
}
}
/**
* Return the child at some daughter index. The children are numbered
* starting with an index of 0.
*
* @param i The daughter index
* @return The tree at that daughter index
*/
public Tree getChild(int i) {
Tree[] kids = children();
return kids[i];
}
/**
* Destructively removes the child at some daughter index and returns it.View on GitHub (pinned to 1b7edd19c4)