{"record":{"id":"0f964713681b0a60","repo":"microsoft/semantic-kernel","slug":"expected-output-length-modeloutput-length-to-be","errorCode":null,"errorMessage":"Expected output length {modelOutput.Length} to be a multiple of {dimensions} dimensions.","messagePattern":"Expected output length (.+?) to be a multiple of (.+?) dimensions\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.Onnx/BertOnnxTextEmbeddingGenerationService.cs","lineNumber":250,"sourceCode":"                    logger.LogTrace(\"Generated embedding for text: {Text}\", text);\n                }\n            }\n\n            return results;\n        }\n        finally\n        {\n            ArrayPool<long>.Shared.Return(scratch);\n        }\n    }\n\n    private float[] Pool(ReadOnlySpan<float> modelOutput)\n    {\n        int dimensions = this._dimensions;\n        int embeddings = Math.DivRem(modelOutput.Length, dimensions, out int leftover);\n        if (leftover != 0)\n        {\n            throw new InvalidOperationException($\"Expected output length {modelOutput.Length} to be a multiple of {dimensions} dimensions.\");\n        }\n\n        float[] result = new float[dimensions];\n        if (embeddings <= 1)\n        {\n            modelOutput.CopyTo(result);\n        }\n        else\n        {\n            switch (this._options.PoolingMode)\n            {\n                case EmbeddingPoolingMode.Mean or EmbeddingPoolingMode.MeanSquareRootTokensLength:\n                    TensorPrimitives.Add(modelOutput.Slice(0, dimensions), modelOutput.Slice(dimensions, dimensions), result);\n                    for (int pos = dimensions * 2; pos < modelOutput.Length; pos += dimensions)\n                    {\n                        TensorPrimitives.Add(result, modelOutput.Slice(pos, dimensions), result);\n                    }\n","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.Onnx/BertOnnxTextEmbeddingGenerationService.cs#L232-L268","documentation":"Thrown as an InvalidOperationException in BertOnnxTextEmbeddingGenerationService.Pool when the raw model output tensor length is not evenly divisible by the configured dimension count. This indicates a mismatch between the ONNX model's actual output dimensions and the dimensions value the service was initialized with. The pooling math requires exact divisibility.","triggerScenarios":"Loading a BERT ONNX model whose hidden size differs from the configured dimensions value; using the wrong model file with settings calibrated for a different model; the model output tensor was truncated or padded incorrectly during inference.","commonSituations":"Swapped model files (e.g., loading a 384-dim model like all-MiniLM-L6 with dimensions=768); misconfigured dimensions in BertOnnxOptions; ONNX runtime version change altering output tensor layout.","solutions":["Verify the dimensions value in BertOnnxOptions matches the model's hidden size (check the model's config.json).","Use the correct model file matching the configured dimensions.","Inspect modelOutput.Length and dimensions at runtime to diagnose the mismatch."],"exampleFix":"// before: 768-dim model loaded but dimensions set for MiniLM\nvar options = new BertOnnxOptions { /* dimensions defaults to wrong value */ };\n\n// after\nvar options = new BertOnnxOptions\n{\n    MaximumTokens = 512\n    // ensure dimensions match the model's config.json hidden_size\n};\n// e.g., for all-MiniLM-L6-v2, hidden_size = 384","handlingStrategy":"validation","validationCode":"// Read the model's config.json to verify hidden_size matches dimensions\nvar modelConfig = JsonSerializer.Deserialize<JsonElement>(File.ReadAllText(configPath));\nint hiddenSize = modelConfig.GetProperty(\"hidden_size\").GetInt32();\nif (hiddenSize != configuredDimensions)\n    throw new InvalidOperationException($\"Dimension mismatch: model={hiddenSize}, config={configuredDimensions}\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify hidden_size in the model's config.json matches BertOnnxOptions dimensions.","Do not mix model files with settings calibrated for different models.","Log modelOutput.Length and dimensions at startup for diagnostics."],"tags":["onnx","bert","embeddings","model-mismatch","invalid-operation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}