stanfordnlp/CoreNLP · error · IllegalStateException

Incorrect function specification: Feature has two values at…

Error message

Incorrect function specification: Feature has two values at one point: " + oldVal + " and " + vals[i]

What it means

The Feature constructor builds an internal map from feature index to value, and the same feature index appearing twice with DIFFERENT values makes the feature function ill-defined. The library throws IllegalStateException because the maxent specification is inconsistent.

Solutions

  1. Deduplicate the (index, value) pairs before constructing the Feature, keeping one value per index
  2. Fix the featurizer so each (x,y) index is emitted at most once
  3. If duplicates are intentional aggregation, sum/merge values beforehand instead of passing conflicting ones

Example fix

// before
new Feature(e, vals, ySize); // e contains duplicate (x,y) with different vals
// after
Map<Integer, Double> merged = new LinkedHashMap<>();
for (int i = 0; i < vals.length; i++) {
  merged.merge(e.get(i)[0] * ySize + e.get(i)[1], vals[i], Double::sum);
}
// build Feature from merged entries
Defensive patterns

Strategy: validation

Validate before calling

Set<Integer> seen = new HashSet<>();
for (int i = 0; i < vals.length; i++) {
  if (vals[i] != 0.0 && !seen.add(e.get(i)[0] * ySize + e.get(i)[1])) {
    throw new IllegalStateException("Duplicate feature index at " + i);
  }
}

Try / catch

try { new Feature(e, vals, ySize); } catch (IllegalStateException ex) { /* dedupe pairs and rebuild */ }

Prevention

When it happens

Trigger: Constructing a Feature where the same (index) position occurs multiple times in the input pairs e with differing values vals[i] (e.g. index (x,y) appearing at both i=0 and i=3 with vals 0.5 and 0.9).

Common situations: Buggy feature-extraction code that emits duplicate (x,y) pairs; concatenating feature lists without deduplication; off-by-one errors in index computation causing accidental collisions.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/maxent/Feature.java:64

  protected Index<IntPair> instanceIndex;

  public Feature() {
  }


  /**
   * This is if we are given an array of double with a value for each training sample in the order of their occurrence.
   */
  public Feature(Experiments e, double[] vals, Index<IntPair> instanceIndex) {
    this.instanceIndex = instanceIndex;
    Map<Integer, Double> setNonZeros = Generics.newHashMap();
    for (int i = 0; i < vals.length; i++) {
      if (vals[i] != 0.0) {
        Integer in = Integer.valueOf(indexOf(e.get(i)[0], e.get(i)[1]));// new Integer(e.get(i)[0]*e.ySize+e.get(i)[1]);
        Double oldVal = setNonZeros.put(in, Double.valueOf(vals[i]));
        if (oldVal != null && oldVal.doubleValue() != vals[i]) {
          throw new IllegalStateException("Incorrect function specification: Feature has two values at one point: " + oldVal + " and " + vals[i]);
        }
      }//if
    }// for
    
    indexedValues = new int[setNonZeros.size()];
    valuesI = new double[indexedValues.length];
    
    int i = 0;
    for (Map.Entry<Integer, Double> entry: setNonZeros.entrySet()) {
      indexedValues[i] = entry.getKey();
      valuesI[i] = entry.getValue();
      i++;
    }
    domain = e;
  }


  int indexOf(int x, int y) {

View on GitHub (pinned to 1b7edd19c4)