stanfordnlp/CoreNLP · error · RuntimeException

Two models must have the same sequence length

Error message

Two models must have the same sequence length

What it means

The two-model FactoredSequenceModel constructor also requires both models to describe the same sequence length; if model1.length() != model2.length() it throws RuntimeException('Two models must have the same sequence length').

Solutions

  1. Ensure both models are constructed over the same underlying sequence (same document, same padding/windowing) before combining.
  2. Align lengths by rebuilding one model with the other's length/window conventions.
  3. Check that both models consume identical input documents (same tokenization and sentence boundaries).

Example fix

// before
FactoredSequenceModel f = new FactoredSequenceModel(modelOverPaddedSeq, modelOverRawSeq);
// after
SequenceModel m2 = new MyModel(paddedDocument); // same length as model1
FactoredSequenceModel f = new FactoredSequenceModel(modelOverPaddedSeq, m2);
Defensive patterns

Strategy: validation

Validate before calling

// verify equal sequence lengths before composing
if (m1.length() != m2.length())
  throw new IllegalArgumentException("length mismatch: " + m1.length() + " vs " + m2.length());

Prevention

When it happens

Trigger: new FactoredSequenceModel(model1, model2) where the two SequenceModels report different length() values — e.g. one model built over a longer windowed/padded sequence than the other, at FactoredSequenceModel.java:112.

Common situations: Combining a model constructed on the full padded sequence with one built on a raw-length sequence; models whose length() derives from different underlying documents or sentence splits.

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/a7c6e99c227e11d0. Report an issue: GitHub.

Appendix: source

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

  }

  /**
   * 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)