{"record":{"id":"b4af9a689467dde6","repo":"microsoft/autogen","slug":"only-one-choice-is-supported-in-streaming-response-b4af9a","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.SemanticKernel/SemanticKernelAgent.cs","lineNumber":92,"sourceCode":"\n        return new MessageEnvelope<ChatMessageContent>(reply[0], 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 chatHistory = BuildChatHistory(messages);\n        var option = BuildOption(options);\n        var chatService = GetChatCompletionService();\n        var response = chatService.GetStreamingChatMessageContentsAsync(chatHistory, option, _kernel, cancellationToken);\n\n        await foreach (var content in response)\n        {\n            if (content.ChoiceIndex > 0)\n            {\n                throw new InvalidOperationException(\"Only one choice is supported in streaming response\");\n            }\n\n            yield return new MessageEnvelope<StreamingChatMessageContent>(content, from: this.Name);\n        }\n    }\n\n    private ChatHistory BuildChatHistory(IEnumerable<IMessage> messages)\n    {\n        var chatMessageContents = ProcessMessage(messages);\n        // if there's no system message in chatMessageContents, add one to the beginning\n        if (!chatMessageContents.Any(c => c.Role == AuthorRole.System))\n        {\n            chatMessageContents = new[] { new ChatMessageContent(AuthorRole.System, _systemMessage) }.Concat(chatMessageContents);\n        }\n\n        return new ChatHistory(chatMessageContents);\n    }\n","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.SemanticKernel/SemanticKernelAgent.cs#L74-L110","documentation":"Thrown inside SemanticKernelAgent.GenerateStreamingReplyAsync when a streamed StreamingChatMessageContent arrives with ChoiceIndex > 0. The streaming path yields every chunk as a single IMessage stream and has no channel for parallel choices, so any second-choice chunk aborts the enumeration.","triggerScenarios":"Calling GenerateStreamingReplyAsync with ResultsPerPrompt/ChoiceCount greater than 1 so the connector emits interleaved chunks for choices 0 and 1.","commonSituations":"Streaming with n>1 sampling options copied from another agent, or a connector/model upgrade that began honoring the choice count in streaming responses.","solutions":["Ensure ResultsPerPrompt is 1 (or unset) in the options used for GenerateStreamingReplyAsync","Use the non-streaming GenerateReplyAsync only after verifying single-choice settings; better, keep streaming but with one choice","Test the connector configuration directly with GetStreamingChatMessageContentsAsync to confirm a single choice is returned"],"exampleFix":"// before\nvar options = new GenerateReplyOptions { ResultsPerPrompt = 2 };\nawait foreach (var chunk in skAgent.GenerateStreamingReplyAsync(msgs, options)) { ... }\n\n// after\nvar options = new GenerateReplyOptions { ResultsPerPrompt = 1 };\nawait foreach (var chunk in skAgent.GenerateStreamingReplyAsync(msgs, options)) { ... }","handlingStrategy":"validation","validationCode":"if (options?.ResultsPerPrompt is > 1)\n    throw new ArgumentOutOfRangeException(nameof(options), \"Streaming SK agent supports exactly one choice.\");\n\nawait foreach (var chunk in skAgent.GenerateStreamingReplyAsync(messages, options)) { /* ... */ }","typeGuard":null,"tryCatchPattern":"try { await foreach (var c in skAgent.GenerateStreamingReplyAsync(msgs, opt)) Yield(c); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Only one choice\"))\n{\n    opt = opt is null ? null : new GenerateReplyOptions { Temperature = opt.Temperature, ResultsPerPrompt = 1 };\n    // restart stream with corrected options\n}","preventionTips":["Pin ResultsPerPrompt to 1 for all streaming code paths","Smoke-test streaming after upgrading SK connectors since choice handling can change","Surround streaming enumerations with exception handling so one bad chunk doesn't kill the SSE response"],"tags":["csharp","semantic-kernel","streaming","options"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}