{"record":{"id":"04598a82601514ec","repo":"microsoft/semantic-kernel","slug":"the-bedrock-agent-requires-a-message-to-be-invoked","errorCode":null,"errorMessage":"The Bedrock agent requires a message to be invoked.","messagePattern":"The Bedrock agent requires a message to be invoked\\.","errorType":"validation","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/Bedrock/BedrockAgent.cs","lineNumber":112,"sourceCode":"    public IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsync(\n        ICollection<ChatMessageContent> messages,\n        AgentThread? thread = null,\n        BedrockAgentInvokeOptions? options = null,\n        CancellationToken cancellationToken = default)\n    {\n        return this.InvokeAsync(messages, thread, (AgentInvokeOptions?)options, cancellationToken);\n    }\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        if (messages.Count == 0)\n        {\n            throw new InvalidOperationException(\"The Bedrock agent requires a message to be invoked.\");\n        }\n\n        // Create a thread if needed\n        BedrockAgentThread bedrockThread = await this.EnsureThreadExistsWithMessagesAsync(\n            messages,\n            thread,\n            () => new BedrockAgentThread(this.RuntimeClient),\n            cancellationToken).ConfigureAwait(false);\n\n        // Get the context contributions from the AIContextProviders.\n#pragma warning disable SKEXP0110, SKEXP0130  // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.\n        AIContext providersContext = await bedrockThread.AIContextProviders.ModelInvokingAsync(messages, cancellationToken).ConfigureAwait(false);\n#pragma warning restore SKEXP0110, SKEXP0130 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.\n\n        // Ensure that the last message provided is a user message\n        string message = this.ExtractUserMessage(messages.Last());\n\n        // Build session state with conversation history and override instructions if needed","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Bedrock/BedrockAgent.cs#L94-L130","documentation":"Thrown at the top of the non-streaming InvokeAsync on BedrockAgent when the messages collection is empty (Count == 0). Bedrock agents require at least one user message to begin a session. This is a caller-contract violation, not a service error.","triggerScenarios":"Calling agent.InvokeAsync([]) or InvokeAsync with an empty list. Also if a caller passes a collection that was populated then cleared before the call.","commonSituations":"Caller built a message list conditionally and it ended up empty; test invoked the agent with no input; upstream message-routing logic dropped all messages.","solutions":["Ensure at least one ChatMessageContent is passed to InvokeAsync.","Guard the call site: skip invocation when there is nothing to send, or supply a default user message."],"exampleFix":"// before\nawait agent.InvokeAsync(new List<ChatMessageContent>()); // throws 173\n\n// after\nif (messages.Count > 0)\n{\n    await agent.InvokeAsync(messages);\n}","handlingStrategy":"validation","validationCode":"if (messages is null || messages.Count == 0)\n    throw new ArgumentException(\"Bedrock agent requires at least one message.\");\nawait agent.InvokeAsync(messages, thread, options, ct);","typeGuard":"static bool HasMessages(ICollection<ChatMessageContent> m) => m is not null && m.Count > 0;","tryCatchPattern":"try { await agent.InvokeAsync(messages, thread); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"requires a message\"))\n{ /* ensure the caller supplies at least one message */ }","preventionTips":["Always pass at least one message when invoking a Bedrock agent.","Guard call sites that build message lists conditionally."],"tags":["validation","bedrock","agent","precondition"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}