{"record":{"id":"b28aaf0600690602","repo":"stanfordnlp/CoreNLP","slug":"linearconstraints-length","errorCode":null,"errorMessage":"linearConstraints.length (","messagePattern":"linearConstraints\\.length \\(","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/sequences/ExactBestSequenceFinder.java","lineNumber":48,"sourceCode":"   * Runs the Viterbi algorithm on the sequence model given by the TagScorer\n   * in order to find the best sequence.\n   *\n   * @param ts The SequenceModel to be used for scoring\n   * @return An array containing the int tags of the best sequence\n   */\n  @Override\n  public int[] bestSequence(SequenceModel ts) {\n    return bestSequence(ts, null).first();\n  }\n\n  private static Pair<int[], Double> bestSequence(SequenceModel ts, double[][] linearConstraints) {\n    // Set up tag options\n    final int length = ts.length();\n    final int leftWindow = ts.leftWindow();\n    final int rightWindow = ts.rightWindow();\n    final int padLength = length + leftWindow + rightWindow;\n    if (linearConstraints != null && linearConstraints.length != padLength)\n      throw new RuntimeException(\"linearConstraints.length (\" +  linearConstraints.length + \") does not match padLength (\" + padLength + \") of SequenceModel\" + \", length==\"+length+\", leftW=\"+leftWindow+\", rightW=\"+rightWindow);\n    int[][] tags = new int[padLength][];\n    int[] tagNum = new int[padLength];\n    if (DEBUG) { log.info(\"Doing bestSequence length \" + length + \"; leftWin \" + leftWindow + \"; rightWin \" + rightWindow + \"; padLength \" + padLength); }\n    for (int pos = 0; pos < padLength; pos++) {\n      // potentially constrain values considered in inference (e.g., to only observed tags for a word if word is common)\n      tags[pos] = ts.getPossibleValues(pos);\n      tagNum[pos] = tags[pos].length;\n      if (DEBUG) { log.info(\"There are \" + tagNum[pos] + \" values at position \" + pos + \": \" + Arrays.toString(tags[pos])); }\n    }\n\n    // Set up product space sizes\n    int[] productSizes = initProductSizes(ts, tagNum, new int[padLength]);\n\n    // Score all of each window's options\n    int[] tempTags = new int[padLength];\n    double[][] windowScore = computeWindowScore(ts, tags, tagNum, tempTags, productSizes);\n\n    // Set up score and backtrace arrays","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/sequences/ExactBestSequenceFinder.java#L30-L66","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nint[] constraints = new int[ts.length()];\nnew ExactBestSequenceFinder().bestSequence(ts, constraints);\n// after\nint padLength = ts.length() + ts.leftWindow() + ts.rightWindow();\nint[] constraints = new int[padLength];\nnew ExactBestSequenceFinder().bestSequence(ts, constraints);","handlingStrategy":"validation","validationCode":"// size constraints to the padded length before decoding\nint padLength = ts.length() + ts.leftWindow() + ts.rightWindow();\nif (linearConstraints != null && linearConstraints.length != padLength)\n  linearConstraints = java.util.Arrays.copyOf(linearConstraints, padLength);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["java","stanford-corenlp","crf","sequence-model","viterbi"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}