{"record":{"id":"508eb2750ccd34bc","repo":"microsoft/semantic-kernel","slug":"cannot-create-thread-because-it-has-already-been-d","errorCode":null,"errorMessage":"Cannot create thread because it has already been deleted.","messagePattern":"Cannot create thread because it has already been deleted\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/agent.py","lineNumber":130,"sourceCode":"    \"\"\"Base class for agent threads.\"\"\"\n\n    def __init__(self):\n        \"\"\"Initialize the agent thread.\"\"\"\n        self._is_deleted: bool = False  # type: ignore\n        self._id: str | None = None  # type: ignore\n\n    @property\n    def id(self) -> str | None:\n        \"\"\"Returns the ID of the current thread (if any).\"\"\"\n        if self._is_deleted:\n            raise RuntimeError(\"Thread has been deleted; call `create()` to recreate it.\")\n        return self._id\n\n    async def create(self) -> str | None:\n        \"\"\"Starts the thread and returns the thread ID.\"\"\"\n        # A thread should not be recreated after it has been deleted.\n        if self._is_deleted:\n            raise RuntimeError(\"Cannot create thread because it has already been deleted.\")\n\n        # If the thread ID is already set, we're done, just return the Id.\n        if self.id is not None:\n            return self.id\n\n        # Otherwise, create the thread.\n        self._id = await self._create()\n        return self.id\n\n    async def delete(self) -> None:\n        \"\"\"Ends the current thread.\"\"\"\n        # A thread should not be deleted if it has already been deleted.\n        if self._is_deleted:\n            return\n\n        # If the thread ID is not set, we're done, just return.\n        if self.id is None:\n            self._is_deleted = True","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/agent.py#L112-L148","documentation":"AgentThread.create() refuses to run on an instance whose _is_deleted flag is True — deletion is irreversible for that instance. This pairs with error 693: once a thread is deleted you cannot revive it; you must build a brand-new thread object. The guard prevents ambiguous half-deleted state.","triggerScenarios":"Calling await thread.create() on the same instance after await thread.delete(); reusing a thread variable across a delete/create cycle.","commonSituations":"Trying to 'reset' a conversation by recreating the same thread; generic retry logic that calls create() again after a failure that triggered deletion.","solutions":["After delete(), construct a new AgentThread subclass instance and call create() on it.","Do not attempt to recreate a deleted thread instance; treat deletion as permanent.","Set the old reference to None and allocate a fresh thread for the next conversation."],"exampleFix":"# before\nawait thread.delete()\nawait thread.create()  # RuntimeError\n# after\nawait thread.delete()\nthread = MyAgentThread()  # new instance\nawait thread.create()","handlingStrategy":"validation","validationCode":"if getattr(thread, '_is_deleted', False):\n    # build a brand-new thread instance; do not call create() on this one\n    thread = MyAgentThread()\nawait thread.create()","typeGuard":"def thread_is_deleted(thread) -> bool:\n    return getattr(thread, '_is_deleted', False)","tryCatchPattern":"try:\n    await thread.create()\nexcept RuntimeError as e:\n    if 'already been deleted' in str(e):\n        thread = MyAgentThread()  # new instance\n        await thread.create()\n    else:\n        raise","preventionTips":["Treat deletion as permanent for that instance.","Construct a new AgentThread subclass after delete().","Null stale references.","Don't put create() inside generic retry-on-failure loops that may follow a delete."],"tags":["agents","thread","lifecycle","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}