{"record":{"id":"f73c36b35c0c2798","repo":"microsoft/semantic-kernel","slug":"the-agent-thread-is-not-a-chathistoryagentthread","errorCode":null,"errorMessage":"The agent thread is not a ChatHistoryAgentThread.","messagePattern":"The agent thread is not a ChatHistoryAgentThread\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/OpenAI/Internal/ResponseThreadActions.cs","lineNumber":309,"sourceCode":"                    content: null)\n                {\n                    ModelId = modelId,\n                    InnerContent = functionCallUpdateContent,\n                    Items = [functionCallUpdateContent],\n                };\n            overrideHistory.Add(functionResultMessage);\n            yield return streamingFunctionResultMessage;\n        }\n    }\n\n    private static ChatHistory GetChatHistory(AgentThread agentThread)\n    {\n        if (agentThread is ChatHistoryAgentThread chatHistoryAgentThread)\n        {\n            return chatHistoryAgentThread.ChatHistory;\n        }\n\n        throw new InvalidOperationException(\"The agent thread is not a ChatHistoryAgentThread.\");\n    }\n\n    private static void ThrowIfIncompleteOrFailed(OpenAIResponseAgent agent, ResponseResult response)\n    {\n        if (response.Status is ResponseStatus.Incomplete or ResponseStatus.Failed)\n        {\n            throw new KernelException(\n                $\"Run failed with status: `{response.Status}` for agent `{agent.Name}` with error: {response.Error.Message} or incomplete details: {response.IncompleteStatusDetails.Reason}\");\n        }\n    }\n\n    /// <summary>\n    /// Processes a function result and returns a string representation.\n    /// The OpenAI Responses API does not support multimodal tool results, so ImageContent returns an error message.\n    /// </summary>\n    internal static string GetFunctionResultAsString(object? result)\n    {\n        var processed = FunctionCallsProcessor.ProcessFunctionResult(result ?? string.Empty);","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/OpenAI/Internal/ResponseThreadActions.cs#L291-L327","documentation":"ResponseThreadActions.GetChatHistory expects an AgentThread of the concrete type ChatHistoryAgentThread so it can read its .ChatHistory. Any other AgentThread subclass (e.g., OpenAIAssistantAgentThread) lacks that property and throws InvalidOperationException.","triggerScenarios":"Passing a non-ChatHistoryAgentThread (a server-backed OpenAIAssistantAgentThread or a custom AgentThread) into a code path that needs local chat history.","commonSituations":"Mixing the Assistant-API server thread model with the Responses-API code path; reusing a thread object across incompatible agent types.","solutions":["Use a ChatHistoryAgentThread with Responses-API agents (OpenAIResponseAgent).","Construct the thread via new ChatHistoryAgentThread() rather than the server-thread factory.","Confirm the agent type and thread type are compatible before invoking."],"exampleFix":"// before\nAgentThread thread = new OpenAIAssistantAgentThread(client);\nawait foreach (var r in responseAgent.InvokeAsync(thread)) { }\n// after\nAgentThread thread = new ChatHistoryAgentThread();\nawait foreach (var r in responseAgent.InvokeAsync(thread)) { }","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static bool IsChatHistoryThread(AgentThread t) => t is ChatHistoryAgentThread;","tryCatchPattern":"try { var history = GetChatHistory(thread); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not a ChatHistoryAgentThread\")) {\n    thread = new ChatHistoryAgentThread(); // create a compatible thread\n}","preventionTips":["Use ChatHistoryAgentThread with Responses-API agents.","Do not reuse Assistant-API server threads with Responses paths.","Document which thread type each agent expects."],"tags":["openai","agent-thread","responses-api","invalid-argument"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}