stanfordnlp/CoreNLP · error · UnsupportedOperationException

FastFactoredParser: cannot provide k good parses.

Error message

FastFactoredParser: cannot provide k good parses.

What it means

FastFactoredParser.getKGoodParses(k) returns the first k of its cached nGoodTrees list. If more than the available number of good parses are requested, it throws UnsupportedOperationException since it cannot compute additional good parses on demand.

Solutions

  1. Cap k to the number of available good parses (check nGoodTrees.size() or use a smaller k).
  2. Catch UnsupportedOperationException and fall back to the available parses.
  3. Use ExhaustivePCFGParser.getKBestParses(k) if the full k-best list is genuinely needed.

Example fix

// before
List<ScoredObject<Tree>> parses = pq.getKGoodParses(20);
// after
List<ScoredObject<Tree>> parses;
try {
  parses = pq.getKGoodParses(20);
} catch (UnsupportedOperationException e) {
  parses = pq.getKGoodParses(1); // only the best is guaranteed
}
Defensive patterns

Strategy: try-catch

Validate before calling

int available = nGoodTreesSize(query); // reflectively or via API
int safeK = Math.min(k, available);
if (safeK < k) log.warning("requested " + k + " good parses, only " + safeK + " available");

Try / catch

try { parses = pq.getKGoodParses(k); } catch (UnsupportedOperationException e) { parses = pq.getKGoodParses(Math.max(1, k / 4)); }

Prevention

When it happens

Trigger: Calling getKGoodParses(k) where k > nGoodTrees.size(), i.e. requesting more good parses than the parser retained (bounded by nGoodTrees size / parser settings).

Common situations: Requesting e.g. getKGoodParses(20) when the factored parser only keeps a handful of good parses; generic code assuming k is always satisfiable.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/lexparser/FastFactoredParser.java:88


  private List<ScoredObject<Tree>> nGoodTrees = new ArrayList<>();



  /**
   * Return the list of N "good" parses of the sentence most recently parsed.
   * (The first is guaranteed to be the best, but later ones are only
   * guaranteed the best subject to the possibilities that disappear because
   * the PCFG/Dep charts only store the best over each span.)
   *
   * @return The list of N best trees
   */
  public List<ScoredObject<Tree>> getKGoodParses(int k) {
    if (k <= nGoodTrees.size()) {
      return nGoodTrees.subList(0, k);
    } else {
      throw new UnsupportedOperationException("FastFactoredParser: cannot provide " + k + " good parses.");
    }
  }


  /** Use the DependencyGrammar to score the tree.
   *
   * @param tr A binarized tree (as returned by the PCFG parser
   * @return The score for the tree according to the grammar
   */
  private double depScoreTree(Tree tr) {
    // log.info("Here's our tree:");
    // tr.pennPrint();
    // log.info(Trees.toDebugStructureString(tr));
    Tree cwtTree = tr.deepCopy(new LabeledScoredTreeFactory(), new CategoryWordTagFactory());
    cwtTree.percolateHeads(binHeadFinder);
    // log.info("Here's what it went to:");
    // cwtTree.pennPrint();
    List<IntDependency> deps = MLEDependencyGrammar.treeToDependencyList(cwtTree, wordIndex, tagIndex);

View on GitHub (pinned to 1b7edd19c4)