{"record":{"id":"56f41176a66b05d6","repo":"microsoft/autogen","slug":"resultsperprompt-greater-than-1-is-not-supported-i-56f411","errorCode":null,"errorMessage":"ResultsPerPrompt greater than 1 is not supported in this semantic kernel agent","messagePattern":"ResultsPerPrompt greater than 1 is not supported in this semantic kernel agent","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.SemanticKernel/SemanticKernelChatCompletionAgent.cs","lineNumber":35,"sourceCode":"    public string Name { get; }\n    private readonly ChatCompletionAgent _chatCompletionAgent;\n\n    public SemanticKernelChatCompletionAgent(ChatCompletionAgent chatCompletionAgent)\n    {\n        this.Name = chatCompletionAgent.Name ?? throw new ArgumentNullException(nameof(chatCompletionAgent.Name));\n        this._chatCompletionAgent = chatCompletionAgent;\n    }\n\n    public async Task<IMessage> GenerateReplyAsync(IEnumerable<IMessage> messages, GenerateReplyOptions? options = null,\n        CancellationToken cancellationToken = default)\n    {\n        var agentThread = new ChatHistoryAgentThread(BuildChatHistory(messages));\n        var reply = await _chatCompletionAgent\n            .InvokeAsync(agentThread, cancellationToken: cancellationToken)\n            .ToArrayAsync(cancellationToken: cancellationToken);\n\n        return reply.Length > 1\n            ? throw new InvalidOperationException(\"ResultsPerPrompt greater than 1 is not supported in this semantic kernel agent\")\n            : new MessageEnvelope<ChatMessageContent>(reply[0], from: this.Name);\n    }\n\n    private ChatHistory BuildChatHistory(IEnumerable<IMessage> messages)\n    {\n        return new ChatHistory(ProcessMessage(messages));\n    }\n\n    private IEnumerable<ChatMessageContent> ProcessMessage(IEnumerable<IMessage> messages)\n    {\n        return messages.Select(m => m switch\n        {\n            IMessage<ChatMessageContent> cmc => cmc.Content,\n            _ => throw new ArgumentException(\"Invalid message type\")\n        });\n    }\n}\n","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.SemanticKernel/SemanticKernelChatCompletionAgent.cs#L17-L53","documentation":"SemanticKernelChatCompletionAgent.GenerateReplyAsync invokes the SK ChatCompletionAgent and materializes all replies with ToArrayAsync. If more than one reply comes back, it throws InvalidOperationException because the method must return exactly one IMessage envelope.","triggerScenarios":"The underlying ChatCompletionAgent (or its execution settings / duplicated responses) returns multiple ChatMessageContent items in a single InvokeAsync enumeration — e.g. ChoiceCount/ResultsPerPrompt > 1, or an SK agent framework version that yields multiple items per invoke.","commonSituations":"Setting PromptExecutionSettings with ChoiceCount > 1 on the ChatCompletionAgent's Kernel, or upgrading Microsoft.SemanticKernel to a version where InvokeAsync emits additional system/tool items that count toward reply.Length.","solutions":["Ensure the ChatCompletionAgent's execution settings request exactly one choice (ChoiceCount/ResultsPerPrompt = 1 or unset)","Upgrade AutoGen.SemanticKernel to a version matching your Microsoft.SemanticKernel package so InvokeAsync semantics line up","If multiple replies are expected, call _chatCompletionAgent.InvokeAsync yourself instead of through this adapter"],"exampleFix":"// before\nvar skAgent = new ChatCompletionAgent\n{\n    Name = \"assistant\",\n    Kernel = kernel,\n    ExecutionSettings = new PromptExecutionSettings { ChoiceCount = 3 },\n};\n\n// after\nvar skAgent = new ChatCompletionAgent\n{\n    Name = \"assistant\",\n    Kernel = kernel,\n    ExecutionSettings = new PromptExecutionSettings(), // single choice\n};","handlingStrategy":"validation","validationCode":"// ensure execution settings request a single choice before invoking the adapter\nvar settings = kernel.GetPromptExecutionSettings<OpenAIPromptExecutionSettings>();\nif (settings?.ChoiceCount is > 1)\n    throw new ConfigurationException(\"SemanticKernelChatCompletionAgent requires ChoiceCount=1\");","typeGuard":null,"tryCatchPattern":"try { return await adapter.GenerateReplyAsync(messages, options); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ResultsPerPrompt\"))\n{\n    throw new ConfigurationException(\"Backing ChatCompletionAgent returned multiple replies; set ChoiceCount=1\", ex);\n}","preventionTips":["Keep ChoiceCount/ResultsPerPrompt at 1 in the SK agent's execution settings","Pin AutoGen.SemanticKernel and Microsoft.SemanticKernel to tested, matching versions","Add an integration test asserting single-reply behavior after package updates"],"tags":["csharp","semantic-kernel","options","invalid-operation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}