{"record":{"id":"b03fd2b72605fe29","repo":"microsoft/semantic-kernel","slug":"expected-data-count-text-embedding-s-but-recei","errorCode":null,"errorMessage":"Expected {data.Count} text embedding(s), but received {embeddings.Count}","messagePattern":"Expected (.+?) text embedding\\(s\\), but received (.+?)","errorType":"exception","errorClass":"KernelException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.Embeddings.cs","lineNumber":47,"sourceCode":"        Kernel? kernel,\n        int? dimensions,\n        CancellationToken cancellationToken)\n    {\n        var result = new List<ReadOnlyMemory<float>>(data.Count);\n\n        if (data.Count > 0)\n        {\n            var embeddingsOptions = new EmbeddingGenerationOptions()\n            {\n                Dimensions = dimensions\n            };\n\n            ClientResult<OpenAIEmbeddingCollection> response = await RunRequestAsync(() => this.Client!.GetEmbeddingClient(targetModel).GenerateEmbeddingsAsync(data, embeddingsOptions, cancellationToken)).ConfigureAwait(false);\n            var embeddings = response.Value;\n\n            if (embeddings.Count != data.Count)\n            {\n                throw new KernelException($\"Expected {data.Count} text embedding(s), but received {embeddings.Count}\");\n            }\n\n            for (var i = 0; i < embeddings.Count; i++)\n            {\n                result.Add(embeddings[i].ToFloats());\n            }\n        }\n\n        return result;\n    }\n}\n","sourceCodeStart":29,"sourceCodeEnd":59,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.Embeddings.cs#L29-L59","documentation":"After calling OpenAI's GenerateEmbeddingsAsync with N input strings, the library verifies the response contains exactly N embedding vectors. If the count differs, it throws immediately rather than returning a misaligned list that would silently corrupt downstream vector operations. This is a hard integrity check on the 1:1 contract between input texts and output embeddings.","triggerScenarios":"Calling GetEmbeddingsAsync with a batch of multiple texts (data.Count > 1) where the embedding endpoint returns a different number of vectors. Happens when a non-OpenAI-compatible endpoint (Azure proxy, local model, OpenAI-compatible server) silently truncates or rejects part of a batch request, or when an API version mismatch changes the response envelope shape.","commonSituations":"Using an OpenAI-compatible local server (e.g. llama.cpp, vLLM, Ollama) that has different batch-size limits or ignores extra inputs. Pointing the connector at an Azure OpenAI deployment whose API-version query param is stale and returns a different response schema. A gateway/load-balancer splitting a batch and only forwarding part of it.","solutions":["Reduce the input batch to a single string and retry; if it succeeds, the endpoint has a batch-size limitation — send smaller batches or one-at-a-time.","Verify the endpoint URL and (for Azure) the api-version query parameter match a version that supports batch embedding responses.","If behind a proxy or gateway, confirm it forwards the full input array without modification.","Check the OpenAI/ Azure SDK package version for breaking changes in the embedding response model.","Inspect the raw HTTP response (enable SDK logging) to see whether the server returned fewer items or a different JSON structure."],"exampleFix":"// before — batch of 5 may be truncated by a limited endpoint\nvar embeddings = await service.GenerateEmbeddingsAsync(new[] { \"a\", \"b\", \"c\", \"d\", \"e\" });\n\n// after — chunk to stay within endpoint batch limits\nforeach (var chunk in inputs.Chunk(size: 1))\n{\n    var batch = await service.GenerateEmbeddingsAsync(chunk);\n    results.AddRange(batch);\n}","handlingStrategy":"retry","validationCode":"// Validate batch size before calling to stay within endpoint limits\nint MaxBatchSize = 16; // tune for your endpoint\nif (data.Count > MaxBatchSize)\n{\n    throw new ArgumentException($\"Batch size {data.Count} exceeds endpoint limit {MaxBatchSize}. Split the input.\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var embeddings = await embeddingService.GenerateEmbeddingsAsync(batch);\n}\ncatch (KernelException ex) when (ex.Message.Contains(\"text embedding(s), but received\"))\n{\n    // server returned a mismatched count — retry with a single-item batch\n    var single = await embeddingService.GenerateEmbeddingsAsync(new[] { batch[0] });\n    logger.LogWarning(\"Embedding batch mismatch; fell back to single-item request.\");\n}","preventionTips":["Keep batch sizes small (1–16) when using non-OpenAI or Azure endpoints to avoid silent truncation.","Pin the Azure OpenAI api-version to one tested with your SDK version.","Log the input count alongside the response count when debugging embedding pipelines."],"tags":["embeddings","openai","response-validation","batch","azure"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}