{"record":{"id":"9b045041fc73a9cc","repo":"microsoft/semantic-kernel","slug":"the-message-could-not-be-added-to-the-thread-due-t-9b0450","errorCode":null,"errorMessage":"The message could not be added to the thread due to an error response from the service.","messagePattern":"The message could not be added to the thread due to an error response from the service\\.","errorType":"exception","errorClass":"AgentThreadOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/OpenAI/OpenAIAssistantChannel.cs","lineNumber":38,"sourceCode":"    : AgentChannel<OpenAIAssistantAgent>\n{\n    private readonly AssistantClient _client = client;\n    private readonly string _threadId = threadId;\n\n    /// <inheritdoc/>\n    protected override async Task ReceiveAsync(IEnumerable<ChatMessageContent> history, CancellationToken cancellationToken)\n    {\n        const string ErrorMessage = \"The message could not be added to the thread due to an error response from the service.\";\n\n        foreach (ChatMessageContent message in history)\n        {\n            try\n            {\n                await AssistantThreadActions.CreateMessageAsync(this._client, this._threadId, message, cancellationToken).ConfigureAwait(false);\n            }\n            catch (ClientResultException ex)\n            {\n                throw new AgentThreadOperationException(ErrorMessage, ex);\n            }\n            catch (AggregateException ex)\n            {\n                throw new AgentThreadOperationException(ErrorMessage, ex);\n            }\n        }\n    }\n\n    /// <inheritdoc/>\n    protected override IAsyncEnumerable<(bool IsVisible, ChatMessageContent Message)> InvokeAsync(\n        OpenAIAssistantAgent agent,\n        CancellationToken cancellationToken)\n    {\n        return ActivityExtensions.RunWithActivityAsync(\n            () => ModelDiagnostics.StartAgentInvocationActivity(agent.Id, agent.GetDisplayName(), agent.Description, agent.Kernel, []),\n            () => AssistantThreadActions.InvokeAsync(agent, this._client, this._threadId, invocationOptions: null, providersAdditionalInstructions: null, this.Logger, agent.Kernel, agent.Arguments, cancellationToken),\n            cancellationToken);\n    }","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/OpenAI/OpenAIAssistantChannel.cs#L20-L56","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect ex.InnerException (ClientResultException) for the HTTP status: 404 → fix thread id, 401/403 → fix key, 429 → throttle.","Verify the thread id stored on the channel corresponds to an existing Assistants v2 thread.","Throttle group-chat turns or add retry-with-backoff for 429s.","Ensure every ChatMessageContent in history has non-empty, service-valid content."],"exampleFix":"// before\nvar chat = new AgentGroupChat(agent) { /* threadId = stale id */ };\nawait chat.InvokeAsync(); // ReceiveAsync throws AgentThreadOperationException\n\n// after — validate the thread exists before the chat runs\nawait thread.CreateAsync();\nvar chat = new AgentGroupChat(agent);\ntry { await chat.InvokeAsync(); }\ncatch (AgentThreadOperationException ex) when (ex.InnerException is ClientResultException c)\n    { logger.LogError(\"history-sync failed: {Status}\", c.Status); throw; }","handlingStrategy":"try-catch","validationCode":"// Ensure the channel's thread exists before the group chat runs\nawait thread.CreateAsync();\nvar chat = new AgentGroupChat(agent);\nawait chat.InvokeAsync();","typeGuard":null,"tryCatchPattern":"try\n{\n    await chat.InvokeAsync();\n}\ncatch (AgentThreadOperationException ex) when (ex.InnerException is ClientResultException cre)\n{\n    logger.LogError(cre, \"Channel history-sync failed: HTTP {Status}\", cre.Status);\n    throw;\n}","preventionTips":["Confirm the thread id on the channel maps to a live Assistants v2 thread.","Keep history messages service-valid and non-empty.","Throttle group-chat turns to avoid rate limits."],"tags":["network","openai","agents","agentchat","service-error","channel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}