microsoft/semantic-kernel · error · AgentThreadOperationException

The message could not be added to the thread due to an error

Error message

The message could not be added to the thread due to an error response from the service.

What it means

Thrown from the AgentChat sync path: OpenAIAssistantChannel.ReceiveAsync iterates the chat history and calls AssistantThreadActions.CreateMessageAsync for each message. When the underlying AssistantClient.CreateMessageAsync throws a ClientResultException (HTTP/service error), it is wrapped as AgentThreadOperationException at line 38. This is the same service-failure semantics as OnNewMessageInternalAsync but triggered by AgentChat history synchronization rather than a direct thread call.

Source

Thrown at dotnet/src/Agents/OpenAI/OpenAIAssistantChannel.cs:38

    : AgentChannel<OpenAIAssistantAgent>
{
    private readonly AssistantClient _client = client;
    private readonly string _threadId = threadId;

    /// <inheritdoc/>
    protected override async Task ReceiveAsync(IEnumerable<ChatMessageContent> history, CancellationToken cancellationToken)
    {
        const string ErrorMessage = "The message could not be added to the thread due to an error response from the service.";

        foreach (ChatMessageContent message in history)
        {
            try
            {
                await AssistantThreadActions.CreateMessageAsync(this._client, this._threadId, message, cancellationToken).ConfigureAwait(false);
            }
            catch (ClientResultException ex)
            {
                throw new AgentThreadOperationException(ErrorMessage, ex);
            }
            catch (AggregateException ex)
            {
                throw new AgentThreadOperationException(ErrorMessage, ex);
            }
        }
    }

    /// <inheritdoc/>
    protected override IAsyncEnumerable<(bool IsVisible, ChatMessageContent Message)> InvokeAsync(
        OpenAIAssistantAgent agent,
        CancellationToken cancellationToken)
    {
        return ActivityExtensions.RunWithActivityAsync(
            () => ModelDiagnostics.StartAgentInvocationActivity(agent.Id, agent.GetDisplayName(), agent.Description, agent.Kernel, []),
            () => AssistantThreadActions.InvokeAsync(agent, this._client, this._threadId, invocationOptions: null, providersAdditionalInstructions: null, this.Logger, agent.Kernel, agent.Arguments, cancellationToken),
            cancellationToken);
    }

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect ex.InnerException (ClientResultException) for the HTTP status: 404 → fix thread id, 401/403 → fix key, 429 → throttle.
  2. Verify the thread id stored on the channel corresponds to an existing Assistants v2 thread.
  3. Throttle group-chat turns or add retry-with-backoff for 429s.
  4. Ensure every ChatMessageContent in history has non-empty, service-valid content.

Example fix

// before
var chat = new AgentGroupChat(agent) { /* threadId = stale id */ };
await chat.InvokeAsync(); // ReceiveAsync throws AgentThreadOperationException

// after — validate the thread exists before the chat runs
await thread.CreateAsync();
var chat = new AgentGroupChat(agent);
try { await chat.InvokeAsync(); }
catch (AgentThreadOperationException ex) when (ex.InnerException is ClientResultException c)
    { logger.LogError("history-sync failed: {Status}", c.Status); throw; }
Defensive patterns

Strategy: try-catch

Validate before calling

// Ensure the channel's thread exists before the group chat runs
await thread.CreateAsync();
var chat = new AgentGroupChat(agent);
await chat.InvokeAsync();

Try / catch

try
{
    await chat.InvokeAsync();
}
catch (AgentThreadOperationException ex) when (ex.InnerException is ClientResultException cre)
{
    logger.LogError(cre, "Channel history-sync failed: HTTP {Status}", cre.Status);
    throw;
}

Prevention

When it happens

Trigger: Using an OpenAIAssistantAgent inside an AgentChat/AgentGroupChat where the channel tries to mirror history onto the OpenAI thread and the service rejects one message — bad/expired thread id, auth failure, rate limit, malformed content.

Common situations: Multi-agent chat where the thread id points at a deleted/foreign thread; shared key without Assistants access; rate-limited bursts during group-chat turns; a message with content the Assistants API rejects.

Related errors


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