stanfordnlp/CoreNLP · error · RuntimeException

Error -- attempted to conditionalize a GeneralizedCounter…

Error message

Error -- attempted to conditionalize a GeneralizedCounter of depth depth() on a vector of length n

What it means

conditionalize(List l) conditions a GeneralizedCounter on a sequence of context keys; the list length must be strictly less than the counter's depth, since each conditioned key consumes one level of nesting and at least one level must remain. If n >= depth(), the operation cannot leave a usable sub-counter and a RuntimeException reporting both depth and list size is thrown.

Solutions

  1. Trim the context list so its size is strictly less than depth(): l = l.subList(0, depth() - 1) or fewer
  2. Check l.size() < gc.depth() before calling
  3. Use conditionalizeOnce for single-key conditioning and verify depth afterwards

Example fix

// before
List<String> ctx = Arrays.asList("w1", "w2"); // depth-2 gc
GeneralizedCounter<String> sub = gc.conditionalize(ctx); // n == depth -> throws
// after
List<String> ctx = Arrays.asList("w2"); // n < depth
GeneralizedCounter<String> sub = gc.conditionalize(ctx);
Defensive patterns

Strategy: validation

Validate before calling

if (l.size() >= gc.depth()) {
  throw new IllegalArgumentException("context length " + l.size() + " must be < depth " + gc.depth());
}

Try / catch

try {
  GeneralizedCounter<K> sub = gc.conditionalize(l);
} catch (RuntimeException e) {
  if (e.getMessage().contains("on a vector of length")) { /* trim context list and retry */ }
}

Prevention

When it happens

Trigger: Calling gc.conditionalize(list) where list.size() >= gc.depth(), e.g. passing a full context of length equal to the table's order into a trigram (depth 3) table.

Common situations: Off-by-one in n-gram code: passing the full n-gram history including the predicted word instead of only its preceding context; reusing one conditionalize call count across tables of different depth.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/stats/GeneralizedCounter.java:409

      if (next == null) // adds a new GeneralizedCounter if needed
      {
        map.put(o, (next = new GeneralizedCounter<>(depth - 1)));
      }
      return next;
    } else {
      throw new RuntimeException("Error -- can't conditionalize a distribution of depth 1");
    }
  }

  /**
   * returns a GeneralizedCounter conditioned on the objects in the
   * {@link List} argument. The length of the argument {@link List}
   * must be less than the depth of the GeneralizedCounter.
   */
  public GeneralizedCounter<K> conditionalize(List<K> l) {
    int n = l.size();
    if (n >= depth()) {
      throw new RuntimeException("Error -- attempted to conditionalize a GeneralizedCounter of depth " + depth() + " on a vector of length " + n);
    } else {
      GeneralizedCounter<K> next = this;
      for (K o: l) {
        next = next.conditionalizeHelper(o);
      }
      return next;
    }
  }

  /**
   * Returns a GeneralizedCounter conditioned on the given top level object.
   * This is just shorthand (and more efficient) for <code>conditionalize(new Object[] { o })</code>.
   */
  public GeneralizedCounter<K> conditionalizeOnce(K o) {
    if (depth() < 1) {
      throw new RuntimeException("Error -- attempted to conditionalize a GeneralizedCounter of depth " + depth());
    } else {
      return conditionalizeHelper(o);

View on GitHub (pinned to 1b7edd19c4)