stanfordnlp/CoreNLP · error · IllegalArgumentException

Must supply a target label to compute precision and recall…

Error message

Must supply a target label to compute precision and recall against

What it means

Classifier.evaluatePrecisionAndRecall computes precision/recall for a specific target label, which must be non-null since all statistics are keyed on comparisons against it. The library throws IllegalArgumentException immediately if the targetLabel argument is null, because no meaningful precision/recall can be computed without it.

Solutions

  1. Pass the actual label instance you want precision/recall for (it must equal a gold label in the dataset)
  2. Verify the label constant is not null before calling (e.g. read from properties with a non-null default)
  3. For full multi-class evaluation without a target label, use evaluateAccuracy instead

Example fix

// before
Pair<Double, Double> pr = classifier.evaluatePrecisionAndRecall(testData, null);
// after
L target = myTargetLabel; // e.g. dataset.labelIndex().get("RELATION_A")
Pair<Double, Double> pr = classifier.evaluatePrecisionAndRecall(testData, target);
Defensive patterns

Strategy: validation

Validate before calling

if (targetLabel == null) throw new IllegalArgumentException("targetLabel required before evaluatePrecisionAndRecall");

Type guard

boolean hasLabel(L l) { return l != null; }

Try / catch

try { return clf.evaluatePrecisionAndRecall(test, target); } catch (IllegalArgumentException e) { log.error("Null/misconfigured target label", e); return Pair.makePair(0.0, 0.0); }

Prevention

When it happens

Trigger: Calling evaluatePrecisionAndRecall(testData, null) directly, or via pr()/dumpAccuracy() where the target label variable was never initialized or was read from a missing config/property.

Common situations: Programmatic classifier evaluation where the target label is loaded from properties that omitted the relevant key; refactors that changed label types; generic evaluation loops passing null for labels not present in the dataset.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/classify/Classifier.java:40

 */

public interface Classifier<L, F> extends Serializable {
  public L classOf(Datum<L, F> example);

  public Counter<L> scoresOf(Datum<L, F> example);

  public Collection<L> labels();

  /**
   * Evaluates the precision and recall of this classifier against a dataset, and the target label.
   *
   * @param testData The dataset to evaluate the classifier on.
   * @param targetLabel The target label (e.g., for relation extraction, this is the relation we're interested in).
   * @return A pair of the precision (first) and recall (second) of the classifier on the target label.
   */
  public default Pair<Double, Double> evaluatePrecisionAndRecall(GeneralDataset<L, F> testData, L targetLabel) {
    if (targetLabel == null) {
      throw new IllegalArgumentException("Must supply a target label to compute precision and recall against");
    }
    // Variables to count
    int numCorrectAndTarget = 0;
    int numTargetGuess = 0;
    int numTargetGold = 0;
    // Iterate over dataset
    for (RVFDatum<L, F> datum : testData) {
      // Get the gold label
      L label = datum.label();
      if (label == null) {
        throw new IllegalArgumentException("Cannot compute precision and recall on unlabelled dataset. Offending datum: " + datum);
      }
      // Get the guess label
      L guess = classOf(datum);
      // Compute statistics on datum
      if (label.equals(targetLabel)) {
        numTargetGold += 1;
      }

View on GitHub (pinned to 1b7edd19c4)