stanfordnlp/CoreNLP · error · RuntimeException
linearConstraints.length (
Error message
linearConstraints.length (
What it means
ExactBestSequenceFinder.bestSequence performs Viterbi decoding over a SequenceModel. If linearConstraints is supplied, it must have one entry per padded position (length + leftWindow + rightWindow). A mismatch throws RuntimeException describing the lengths.
Solutions
- Size the constraints array as ts.length() + ts.leftWindow() + ts.rightWindow() before calling bestSequence.
- Pad an existing constraints array with default (unconstrained, e.g. null/empty) entries for the window positions.
- Pass null if no constraints are needed instead of a wrongly sized array.
Example fix
// before int[] constraints = new int[ts.length()]; new ExactBestSequenceFinder().bestSequence(ts, constraints); // after int padLength = ts.length() + ts.leftWindow() + ts.rightWindow(); int[] constraints = new int[padLength]; new ExactBestSequenceFinder().bestSequence(ts, constraints);
Defensive patterns
Strategy: validation
Validate before calling
// size constraints to the padded length before decoding int padLength = ts.length() + ts.leftWindow() + ts.rightWindow(); if (linearConstraints != null && linearConstraints.length != padLength) linearConstraints = java.util.Arrays.copyOf(linearConstraints, padLength);
Prevention
- Always derive constraint arrays from ts.length()+leftWindow()+rightWindow(), never from raw doc length.
- Centralize constraint construction in one helper.
- Write a unit test covering models with non-zero windows.
When it happens
Trigger: Calling bestSequence(ts, linearConstraints) where linearConstraints.length != ts.length() + ts.leftWindow() + ts.rightWindow() — e.g. constraints sized only by the raw sequence length while the model has non-zero windows, at ExactBestSequenceFinder.java:48.
Common situations: Building constraint arrays for the un-padded document length in NER/tagger pipelines while the test sequence includes left/right context windows; reusing constraints across models with different window sizes.
Understand the failure class
Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.
Related errors
- input must be sorted!
- Two models must have the same number of classes
- Two models must have the same sequence length
- KBestSequenceFinder only works with rightWindow == 0 not
- Unknown feature type " + feature
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/b28aaf0600690602.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/sequences/ExactBestSequenceFinder.java:48
* Runs the Viterbi algorithm on the sequence model given by the TagScorer
* in order to find the best sequence.
*
* @param ts The SequenceModel to be used for scoring
* @return An array containing the int tags of the best sequence
*/
@Override
public int[] bestSequence(SequenceModel ts) {
return bestSequence(ts, null).first();
}
private static Pair<int[], Double> bestSequence(SequenceModel ts, double[][] linearConstraints) {
// Set up tag options
final int length = ts.length();
final int leftWindow = ts.leftWindow();
final int rightWindow = ts.rightWindow();
final int padLength = length + leftWindow + rightWindow;
if (linearConstraints != null && linearConstraints.length != padLength)
throw new RuntimeException("linearConstraints.length (" + linearConstraints.length + ") does not match padLength (" + padLength + ") of SequenceModel" + ", length=="+length+", leftW="+leftWindow+", rightW="+rightWindow);
int[][] tags = new int[padLength][];
int[] tagNum = new int[padLength];
if (DEBUG) { log.info("Doing bestSequence length " + length + "; leftWin " + leftWindow + "; rightWin " + rightWindow + "; padLength " + padLength); }
for (int pos = 0; pos < padLength; pos++) {
// potentially constrain values considered in inference (e.g., to only observed tags for a word if word is common)
tags[pos] = ts.getPossibleValues(pos);
tagNum[pos] = tags[pos].length;
if (DEBUG) { log.info("There are " + tagNum[pos] + " values at position " + pos + ": " + Arrays.toString(tags[pos])); }
}
// Set up product space sizes
int[] productSizes = initProductSizes(ts, tagNum, new int[padLength]);
// Score all of each window's options
int[] tempTags = new int[padLength];
double[][] windowScore = computeWindowScore(ts, tags, tagNum, tempTags, productSizes);
// Set up score and backtrace arraysView on GitHub (pinned to 1b7edd19c4)