{"record":{"id":"eedc1ca4355f363e","repo":"microsoft/semantic-kernel","slug":"nameof-copilotstudioagent-requires-a-message-to","errorCode":null,"errorMessage":"{nameof(CopilotStudioAgent)} requires a message to be invoked.","messagePattern":"(.+?) requires a message to be invoked\\.","errorType":"validation","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/Copilot/CopilotStudioAgent.cs","lineNumber":52,"sourceCode":"    }\n\n    /// <summary>\n    /// CopilotStudioAgent does not support instructions like other agents.\n    /// </summary>\n    internal new string? Instructions => null;\n\n    /// <inheritdoc/>\n    public override async IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsync(\n        ICollection<ChatMessageContent> messages,\n        AgentThread? thread = null,\n        AgentInvokeOptions? options = null,\n        [EnumeratorCancellation] CancellationToken cancellationToken = default)\n    {\n        messages ??= [];\n\n        if (messages.Count == 0)\n        {\n            throw new InvalidOperationException($\"{nameof(CopilotStudioAgent)} requires a message to be invoked.\");\n        }\n\n        // Create a thread if needed\n        CopilotStudioAgentThread agentThread = await this.EnsureThreadExistsWithMessagesAsync(\n            messages,\n            thread,\n            () => new CopilotStudioAgentThread(this.Client) { Logger = this.ActiveLoggerFactory.CreateLogger<CopilotStudioAgentThread>() },\n            cancellationToken).ConfigureAwait(false);\n\n        // Invoke the agent\n        IAsyncEnumerable<ChatMessageContent> invokeResults = this.InvokeInternalAsync(messages, agentThread, cancellationToken);\n\n        // Return the results to the caller in AgentResponseItems.\n        await foreach (ChatMessageContent result in invokeResults.ConfigureAwait(false))\n        {\n            await this.NotifyThreadOfNewMessage(agentThread, result, cancellationToken).ConfigureAwait(false);\n\n            if (options?.OnIntermediateMessage is not null)","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Copilot/CopilotStudioAgent.cs#L34-L70","documentation":"Thrown by CopilotStudioAgent.InvokeAsync when the messages collection is empty (after the null-coalescing to an empty list). The Copilot Studio agent requires at least one user message to formulate a question for the underlying AskQuestionAsync call. This is a precondition guard, not a service error.","triggerScenarios":"Calling agent.InvokeAsync(messages: []) or agent.InvokeAsync(messages: null) with no messages in the collection. Also when a caller conditionally builds a message list that ends up empty.","commonSituations":"Passing an empty ChatHistory; a UI flow that allows submission of no input; conditional message construction that produced zero items; refactoring that removed the message-add step.","solutions":["Ensure the messages collection passed to InvokeAsync contains at least one ChatMessageContent before calling.","Guard at the call site: check messages.Count > 0 before invoking.","If user input is optional in your flow, supply a default/prompt message."],"exampleFix":"// before\nvar messages = new List<ChatMessageContent>(); // empty\nawait foreach (var r in agent.InvokeAsync(messages, thread)) { ... }\n\n// after\nif (messages.Count == 0)\n    messages.Add(new ChatMessageContent(AuthorRole.User, userInput));\nawait foreach (var r in agent.InvokeAsync(messages, thread)) { ... }","handlingStrategy":"validation","validationCode":"if (messages is null || messages.Count == 0)\n    throw new ArgumentException(\"At least one message is required to invoke the Copilot agent.\", nameof(messages));","typeGuard":"static bool HasMessages(ICollection<ChatMessageContent>? m) => m is { Count: > 0 };","tryCatchPattern":"try { await foreach (var r in agent.InvokeAsync(messages, thread, ct)) { ... } }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"requires a message\"))\n{\n    logger.LogError(\"InvokeAsync called with no messages. Provide at least one user message.\");\n    throw;\n}","preventionTips":["Guard the call site: only invoke when messages.Count > 0.","In UI flows, disable submit when input is empty.","Provide a default/prompt message when user input is optional."],"tags":["copilot","agents","validation","precondition","csharp"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}