stanfordnlp/CoreNLP · error · RuntimeException

Two models must have the same number of classes

Error message

Two models must have the same number of classes

What it means

The two-model FactoredSequenceModel constructor combines two SequenceModels that score the same label space, so it verifies both models report the same number of possible values at position 0. If the class counts differ it throws RuntimeException('Two models must have the same number of classes').

Solutions

  1. Combine only models trained/defined over the same label space; retrain one model with matching class counts.
  2. Map or pad the smaller model's possible values so counts align before construction.
  3. Use the SequenceModel[] + weights constructor if the intended semantics allow different label spaces (verify it fits your use case).

Example fix

// before
SequenceModel m1 = taggerModel;   // 45 tags
SequenceModel m2 = nerModel;      // 5 classes
FactoredSequenceModel f = new FactoredSequenceModel(m1, m2);
// after
SequenceModel m2 = retrained5classCompatModel; // same class count as m1
FactoredSequenceModel f = new FactoredSequenceModel(m1, m2);
Defensive patterns

Strategy: validation

Validate before calling

// verify equal class counts before composing
if (m1.getPossibleValues(0).length != m2.getPossibleValues(0).length)
  throw new IllegalArgumentException("class-count mismatch: " + m1.getPossibleValues(0).length + " vs " + m2.getPossibleValues(0).length);

Prevention

When it happens

Trigger: new FactoredSequenceModel(model1, model2) where model1.getPossibleValues(0).length != model2.getPossibleValues(0).length — e.g. combining a tagger model with a different tag set against an NER model, at FactoredSequenceModel.java:111.

Common situations: Mixing models trained on different tag inventories or different feature pipelines (e.g. a 3-class entity model with a 5-class one); combining a coarse tagger with a fine NER model in factored decoding.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/sequences/FactoredSequenceModel.java:111

    return model1.getPossibleValues(position);
  }

  /**
   * using this constructor results in a weighted addition of the two models' scores.
   * @param model1
   * @param model2
   * @param wt1 weight of model1
   * @param wt2 weight of model2
   */
  public FactoredSequenceModel(SequenceModel model1, SequenceModel model2, double wt1, double wt2){
    this(model1,model2);
    this.model1Wt = wt1;
    this.model2Wt = wt2;
  }

  public FactoredSequenceModel(SequenceModel model1, SequenceModel model2) {
    //if (model1.leftWindow() != model2.leftWindow()) throw new RuntimeException("Two models must have same window size");
    if (model1.getPossibleValues(0).length != model2.getPossibleValues(0).length) throw new RuntimeException("Two models must have the same number of classes");
    if (model1.length() != model2.length()) throw new RuntimeException("Two models must have the same sequence length");
    this.model1 = model1;
    this.model2 = model2;
  }

  public FactoredSequenceModel(SequenceModel[] models, double[] weights){
    this.models = models;
    this.wts = weights;
    /*
  for(int i = 1; i < models.length; i++){
    if (models[0].getPossibleValues(0).length != models[i].getPossibleValues(0).length) throw new RuntimeException("All models must have the same number of classes");
    if(models[0].length() != models[i].length())
      throw new RuntimeException("All models must have the same sequence length");

    }
    */
  }

View on GitHub (pinned to 1b7edd19c4)