microsoft/semantic-kernel · error · InvalidOperationException

The Bedrock agent requires a message to be invoked.

Error message

The Bedrock agent requires a message to be invoked.

What it means

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.

Source

Thrown at dotnet/src/Agents/Bedrock/BedrockAgent.cs:112

    public IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsync(
        ICollection<ChatMessageContent> messages,
        AgentThread? thread = null,
        BedrockAgentInvokeOptions? options = null,
        CancellationToken cancellationToken = default)
    {
        return this.InvokeAsync(messages, thread, (AgentInvokeOptions?)options, cancellationToken);
    }

    /// <inheritdoc/>
    public override async IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsync(
        ICollection<ChatMessageContent> messages,
        AgentThread? thread = null,
        AgentInvokeOptions? options = null,
        [EnumeratorCancellation] CancellationToken cancellationToken = default)
    {
        if (messages.Count == 0)
        {
            throw new InvalidOperationException("The Bedrock agent requires a message to be invoked.");
        }

        // Create a thread if needed
        BedrockAgentThread bedrockThread = await this.EnsureThreadExistsWithMessagesAsync(
            messages,
            thread,
            () => new BedrockAgentThread(this.RuntimeClient),
            cancellationToken).ConfigureAwait(false);

        // Get the context contributions from the AIContextProviders.
#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.
        AIContext providersContext = await bedrockThread.AIContextProviders.ModelInvokingAsync(messages, cancellationToken).ConfigureAwait(false);
#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.

        // Ensure that the last message provided is a user message
        string message = this.ExtractUserMessage(messages.Last());

        // Build session state with conversation history and override instructions if needed

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Ensure at least one ChatMessageContent is passed to InvokeAsync.
  2. Guard the call site: skip invocation when there is nothing to send, or supply a default user message.

Example fix

// before
await agent.InvokeAsync(new List<ChatMessageContent>()); // throws 173

// after
if (messages.Count > 0)
{
    await agent.InvokeAsync(messages);
}
Defensive patterns

Strategy: validation

Validate before calling

if (messages is null || messages.Count == 0)
    throw new ArgumentException("Bedrock agent requires at least one message.");
await agent.InvokeAsync(messages, thread, options, ct);

Type guard

static bool HasMessages(ICollection<ChatMessageContent> m) => m is not null && m.Count > 0;

Try / catch

try { await agent.InvokeAsync(messages, thread); }
catch (InvalidOperationException ex) when (ex.Message.Contains("requires a message"))
{ /* ensure the caller supplies at least one message */ }

Prevention

When it happens

Trigger: Calling agent.InvokeAsync([]) or InvokeAsync with an empty list. Also if a caller passes a collection that was populated then cleared before the call.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/04598a82601514ec. Report an issue: GitHub.