microsoft/semantic-kernel · error · KernelException

Expected {data.Count} text embedding(s), but received {embed

Error message

Expected {data.Count} text embedding(s), but received {embeddings.Count}

What it means

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.

Source

Thrown at dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.Embeddings.cs:47

        Kernel? kernel,
        int? dimensions,
        CancellationToken cancellationToken)
    {
        var result = new List<ReadOnlyMemory<float>>(data.Count);

        if (data.Count > 0)
        {
            var embeddingsOptions = new EmbeddingGenerationOptions()
            {
                Dimensions = dimensions
            };

            ClientResult<OpenAIEmbeddingCollection> response = await RunRequestAsync(() => this.Client!.GetEmbeddingClient(targetModel).GenerateEmbeddingsAsync(data, embeddingsOptions, cancellationToken)).ConfigureAwait(false);
            var embeddings = response.Value;

            if (embeddings.Count != data.Count)
            {
                throw new KernelException($"Expected {data.Count} text embedding(s), but received {embeddings.Count}");
            }

            for (var i = 0; i < embeddings.Count; i++)
            {
                result.Add(embeddings[i].ToFloats());
            }
        }

        return result;
    }
}

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. 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.
  2. Verify the endpoint URL and (for Azure) the api-version query parameter match a version that supports batch embedding responses.
  3. If behind a proxy or gateway, confirm it forwards the full input array without modification.
  4. Check the OpenAI/ Azure SDK package version for breaking changes in the embedding response model.
  5. Inspect the raw HTTP response (enable SDK logging) to see whether the server returned fewer items or a different JSON structure.

Example fix

// before — batch of 5 may be truncated by a limited endpoint
var embeddings = await service.GenerateEmbeddingsAsync(new[] { "a", "b", "c", "d", "e" });

// after — chunk to stay within endpoint batch limits
foreach (var chunk in inputs.Chunk(size: 1))
{
    var batch = await service.GenerateEmbeddingsAsync(chunk);
    results.AddRange(batch);
}
Defensive patterns

Strategy: retry

Validate before calling

// Validate batch size before calling to stay within endpoint limits
int MaxBatchSize = 16; // tune for your endpoint
if (data.Count > MaxBatchSize)
{
    throw new ArgumentException($"Batch size {data.Count} exceeds endpoint limit {MaxBatchSize}. Split the input.");
}

Try / catch

try
{
    var embeddings = await embeddingService.GenerateEmbeddingsAsync(batch);
}
catch (KernelException ex) when (ex.Message.Contains("text embedding(s), but received"))
{
    // server returned a mismatched count — retry with a single-item batch
    var single = await embeddingService.GenerateEmbeddingsAsync(new[] { batch[0] });
    logger.LogWarning("Embedding batch mismatch; fell back to single-item request.");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/b03fd2b72605fe29. Report an issue: GitHub.