{"record":{"id":"2502226b0b7ae1b2","repo":"microsoft/semantic-kernel","slug":"cannot-retrieve-chat-history-since-the-thread-has","errorCode":null,"errorMessage":"Cannot retrieve chat history, since the thread has been deleted.","messagePattern":"Cannot retrieve chat history, since the thread has been deleted\\.","errorType":"exception","errorClass":"AgentThreadOperationException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/autogen/autogen_conversable_agent.py","lineNumber":89,"sourceCode":"        \"\"\"Called when a new message has been contributed to the chat.\"\"\"\n        if isinstance(new_message, str):\n            new_message = ChatMessageContent(role=AuthorRole.USER, content=new_message)\n\n        if (\n            not new_message.metadata\n            or \"thread_id\" not in new_message.metadata\n            or new_message.metadata[\"thread_id\"] != self._id\n        ):\n            self._chat_history.add_message(new_message)\n\n    async def get_messages(self) -> AsyncIterable[ChatMessageContent]:\n        \"\"\"Retrieve the current chat history.\n\n        Returns:\n            An async iterable of ChatMessageContent.\n        \"\"\"\n        if self._is_deleted:\n            raise AgentThreadOperationException(\"Cannot retrieve chat history, since the thread has been deleted.\")\n        if self._id is None:\n            await self.create()\n        for message in self._chat_history.messages:\n            yield message\n\n    async def reduce(self) -> ChatHistory | None:\n        \"\"\"Reduce the chat history to a smaller size.\"\"\"\n        if self._id is None:\n            raise AgentThreadOperationException(\"Cannot reduce chat history, since the thread is not currently active.\")\n        if not isinstance(self._chat_history, ChatHistoryReducer):\n            return None\n        return await self._chat_history.reduce()\n\n\n@experimental\nclass AutoGenConversableAgent(Agent):\n    \"\"\"A Semantic Kernel wrapper around an AutoGen 0.2 `ConversableAgent`.\n","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/autogen/autogen_conversable_agent.py#L71-L107","documentation":"Thrown by AutoGenConversableAgentThread.get_messages when the thread's _is_deleted flag is True. Once delete() has run, the in-memory chat history is cleared and the thread is marked deleted, so any subsequent attempt to read messages is rejected as an invalid operation on a dead thread.","triggerScenarios":"Calling `async for m in thread.get_messages()` after `await thread.delete()`; reusing a thread reference that another code path already deleted.","commonSituations":"Cleanup-then-inspect flow in tests or shutdown hooks; shared thread object deleted by a finally block before a later read; chaining agents where one deletes the thread.","solutions":["Do not call get_messages after delete(); reorder logic so reads happen before cleanup.","Create a fresh thread (new AutoGenConversableAgentThread) if you need history after deletion.","Track deletion state in your own code and short-circuit reads when deleted."],"exampleFix":"# before\nawait thread.delete()\nmsgs = [m async for m in thread.get_messages()]\n\n# after\nmsgs = [m async for m in thread.get_messages()]\nawait thread.delete()","handlingStrategy":"validation","validationCode":"if thread._is_deleted:\n    raise RuntimeError('thread already deleted; cannot read messages')","typeGuard":"def thread_is_readable(thread) -> bool:\n    return not getattr(thread, '_is_deleted', False)","tryCatchPattern":"from semantic_kernel.exceptions.agent_exceptions import AgentThreadOperationException\ntry:\n    messages = [m async for m in thread.get_messages()]\nexcept AgentThreadOperationException as e:\n    if 'has been deleted' in str(e):\n        messages = []\n    else:\n        raise","preventionTips":["Read thread state before deleting.","Do not share a thread reference across code that may delete it."],"tags":["thread","lifecycle","autogen","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}