{"record":{"id":"2846558b6856b70a","repo":"microsoft/autogen","slug":"only-one-choice-is-supported-in-streaming-response","errorCode":null,"errorMessage":"Only one choice is supported in streaming response","messagePattern":"Only one choice is supported in streaming response","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.OpenAI.V1/Agent/OpenAIChatAgent.cs","lineNumber":117,"sourceCode":"    {\n        var settings = this.CreateChatCompletionsOptions(options, messages);\n        var reply = await this.openAIClient.GetChatCompletionsAsync(settings, cancellationToken);\n\n        return new MessageEnvelope<ChatCompletions>(reply, from: this.Name);\n    }\n\n    public async IAsyncEnumerable<IMessage> GenerateStreamingReplyAsync(\n        IEnumerable<IMessage> messages,\n        GenerateReplyOptions? options = null,\n        [EnumeratorCancellation] CancellationToken cancellationToken = default)\n    {\n        var settings = this.CreateChatCompletionsOptions(options, messages);\n        var response = await this.openAIClient.GetChatCompletionsStreamingAsync(settings, cancellationToken);\n        await foreach (var update in response.WithCancellation(cancellationToken))\n        {\n            if (update.ChoiceIndex > 0)\n            {\n                throw new InvalidOperationException(\"Only one choice is supported in streaming response\");\n            }\n\n            yield return new MessageEnvelope<StreamingChatCompletionsUpdate>(update, from: this.Name);\n        }\n    }\n\n    private ChatCompletionsOptions CreateChatCompletionsOptions(GenerateReplyOptions? options, IEnumerable<IMessage> messages)\n    {\n        var oaiMessages = messages.Select(m => m switch\n        {\n            IMessage<ChatRequestMessage> chatRequestMessage => chatRequestMessage.Content,\n            _ => throw new ArgumentException(\"Invalid message type\")\n        });\n\n        // add system message if there's no system message in messages\n        if (!oaiMessages.Any(m => m is ChatRequestSystemMessage))\n        {\n            oaiMessages = new[] { new ChatRequestSystemMessage(systemMessage) }.Concat(oaiMessages);","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.OpenAI.V1/Agent/OpenAIChatAgent.cs#L99-L135","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set options.ChoiceCount = 1 (or leave it unset) on the ChatCompletionsOptions passed to the agent","If you need multiple candidates, issue separate non-streaming calls instead of one n>1 streaming call","Check for stray ChoiceCount assignments when reusing/cloned options objects"],"exampleFix":"// before\nvar options = new ChatCompletionsOptions { ChoiceCount = 3 };\nvar agent = new OpenAIChatAgent(client, \"gpt\", options);\n\n// after\nvar options = new ChatCompletionsOptions { ChoiceCount = 1 };\nvar agent = new OpenAIChatAgent(client, \"gpt\", options);","handlingStrategy":"validation","validationCode":"if (options.ChoiceCount is > 1)\n    throw new InvalidOperationException(\"OpenAIChatAgent streaming requires ChoiceCount == 1\");","typeGuard":null,"tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message.Contains(\"Only one choice\"))\n{\n    logger.LogError(\"Streaming aborted: multiple choices configured\");\n}","preventionTips":["Keep ChoiceCount unset (defaults to 1)","Avoid porting n>1 sampling code to streaming agents","Fingerprint options before constructing agents"],"tags":["openai","streaming","choice-count","invalid-operation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}