stanfordnlp/CoreNLP · error · IllegalArgumentException

Unknown label

Error message

Unknown label ${label}

What it means

Thrown by LinearClassifier.getLabelIndices when a requested label is not present in the classifier's labelIndex. The classifier can only score labels seen during training; the caller asked for an unknown label's index.

Solutions

  1. Use only labels from classifier.labels()
  2. Check for label spelling/casing mismatches
  3. Retrain the model if the label should exist
  4. Filter the requested label set against labelIndex before lookup
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at src/edu/stanford/nlp/classify/LinearClassifier.java:387 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at src/edu/stanford/nlp/classify/LinearClassifier.java:387

    // NB: this duplicate method is needed so it calls the scoresOf method
    // with an RVFDatum signature!!  Don't remove it!
    // JLS: type resolution of method parameters is static
    Counter<L> scores = scoresOf(example);
    Counters.logNormalizeInPlace(scores);
    return scores;
  }

  /**
   * Returns indices of labels
   * @param labels - Set of labels to get indices
   * @return Set of indices
   */
  protected Set<Integer> getLabelIndices(Set<L> labels) {
    Set<Integer> iLabels = Generics.newHashSet();
    for (L label:labels) {
      int iLabel = labelIndex.indexOf(label);
      iLabels.add(iLabel);
      if (iLabel < 0) throw new IllegalArgumentException("Unknown label " + label);
    }
    return iLabels;
  }

  /**
   * Returns number of features with weight above a certain threshold
   * (across all labels).
   *
   * @param threshold  Threshold above which we will count the feature
   * @param useMagnitude Whether the notion of "large" should ignore
   *                     the sign of the feature weight.
   * @return number of features satisfying the specified conditions
   */
  public int getFeatureCount(double threshold, boolean useMagnitude)
  {
    int n = 0;
    for (double[] weightArray : weights) {
      for (double weight : weightArray) {

View on GitHub (pinned to 1b7edd19c4)