microsoft/autogen · error · InvalidOperationException

Only one choice is supported in streaming response

Error message

Only one choice is supported in streaming response

What it means

OpenAIChatAgent.GenerateStreamingReplyAsync throws InvalidOperationException when a streaming update arrives with ChoiceIndex > 0. The agent yields raw StreamingChatCompletionsUpdate envelopes and its downstream connector can only reconstruct a single assistant message, so n > 1 completions are unsupported.

Source

Thrown at dotnet/src/AutoGen.OpenAI.V1/Agent/OpenAIChatAgent.cs:117

    {
        var settings = this.CreateChatCompletionsOptions(options, messages);
        var reply = await this.openAIClient.GetChatCompletionsAsync(settings, cancellationToken);

        return new MessageEnvelope<ChatCompletions>(reply, from: this.Name);
    }

    public async IAsyncEnumerable<IMessage> GenerateStreamingReplyAsync(
        IEnumerable<IMessage> messages,
        GenerateReplyOptions? options = null,
        [EnumeratorCancellation] CancellationToken cancellationToken = default)
    {
        var settings = this.CreateChatCompletionsOptions(options, messages);
        var response = await this.openAIClient.GetChatCompletionsStreamingAsync(settings, cancellationToken);
        await foreach (var update in response.WithCancellation(cancellationToken))
        {
            if (update.ChoiceIndex > 0)
            {
                throw new InvalidOperationException("Only one choice is supported in streaming response");
            }

            yield return new MessageEnvelope<StreamingChatCompletionsUpdate>(update, from: this.Name);
        }
    }

    private ChatCompletionsOptions CreateChatCompletionsOptions(GenerateReplyOptions? options, IEnumerable<IMessage> messages)
    {
        var oaiMessages = messages.Select(m => m switch
        {
            IMessage<ChatRequestMessage> chatRequestMessage => chatRequestMessage.Content,
            _ => throw new ArgumentException("Invalid message type")
        });

        // add system message if there's no system message in messages
        if (!oaiMessages.Any(m => m is ChatRequestSystemMessage))
        {
            oaiMessages = new[] { new ChatRequestSystemMessage(systemMessage) }.Concat(oaiMessages);

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Set options.ChoiceCount = 1 (or leave it unset) on the ChatCompletionsOptions passed to the agent
  2. If you need multiple candidates, issue separate non-streaming calls instead of one n>1 streaming call
  3. Check for stray ChoiceCount assignments when reusing/cloned options objects

Example fix

// before
var options = new ChatCompletionsOptions { ChoiceCount = 3 };
var agent = new OpenAIChatAgent(client, "gpt", options);

// after
var options = new ChatCompletionsOptions { ChoiceCount = 1 };
var agent = new OpenAIChatAgent(client, "gpt", options);
Defensive patterns

Strategy: validation

Validate before calling

if (options.ChoiceCount is > 1)
    throw new InvalidOperationException("OpenAIChatAgent streaming requires ChoiceCount == 1");

Try / catch

catch (InvalidOperationException ex) when (ex.Message.Contains("Only one choice"))
{
    logger.LogError("Streaming aborted: multiple choices configured");
}

Prevention

When it happens

Trigger: The ChatCompletionsOptions used to build the agent has ChoiceCount (n) greater than 1, or the deployment/server returns multiple choices per update; then any chunk for the second choice triggers the throw mid-stream.

Common situations: Porting non-streaming sample code that sets n for best-of sampling; Azure deployments configured for multiple candidates; hand-built options copied from OpenAI cookbook examples.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/2846558b6856b70a. Report an issue: GitHub.