{"record":{"id":"01730d30a4aedb72","repo":"microsoft/semantic-kernel","slug":"cannot-delete-the-thread-since-it-has-not-been-cr-01730d","errorCode":null,"errorMessage":"Cannot delete the thread, since it has not been created.","messagePattern":"Cannot delete the thread, since it has not been created\\.","errorType":"exception","errorClass":"AgentThreadOperationException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/open_ai/openai_responses_agent.py","lineNumber":212,"sourceCode":"    @override\n    async def _create(self) -> str:\n        \"\"\"Starts the thread and returns its ID.\"\"\"\n        if self._is_deleted:\n            raise AgentThreadOperationException(\n                \"Cannot create a new thread, since the current thread has been deleted.\"\n            )\n        self._enable_store = True\n\n        # The ID isn't available until after a message is sent\n        return \"\"\n\n    @override\n    async def _delete(self) -> None:\n        \"\"\"Ends the current thread.\"\"\"\n        if self._is_deleted:\n            return\n        if self.response_id is None:\n            raise AgentThreadOperationException(\"Cannot delete the thread, since it has not been created.\")\n        self._chat_history.clear()\n        self._is_deleted = True\n\n    @override\n    async def _on_new_message(self, new_message: str | ChatMessageContent) -> None:\n        \"\"\"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 not self.response_id:\n            self._chat_history.add_message(new_message)\n\n    async def get_messages(\n        self, limit: int | None = None, sort_order: Literal[\"asc\", \"desc\"] | None = \"desc\"\n    ) -> AsyncIterable[ChatMessageContent]:\n        \"\"\"Retrieve the current chat history.\"\"\"\n        if self._is_deleted:\n            raise AgentThreadOperationException(\"Cannot retrieve chat history, since the thread has been deleted.\")","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/open_ai/openai_responses_agent.py#L194-L230","documentation":"Raised as AgentThreadOperationException by ResponseAgentThread._delete() when delete is called but response_id is None (the thread was never actually created — no message was ever sent, so OpenAI never returned an id). Because there is nothing server-side to delete and the local chat history is the only state, the library treats deletion of a never-started thread as an error to surface misuse.","triggerScenarios":"Instantiating a ResponseAgentThread and calling delete() before sending any message; a teardown hook running on a thread that never received input; calling delete twice where the first call cleared state incorrectly.","commonSituations":"Early-exit/exception paths that clean up a thread before first use; session teardown firing on an unused thread; conditional flows where the user message was never sent.","solutions":["Only call delete() after the thread has an id (i.e., after at least one message was sent and response_id is set).","Guard deletion: if thread.response_id is not None before deleting.","Reorder logic so the user message/invoke precedes teardown."],"exampleFix":"# before\nthread = ResponseAgentThread(client=client)\nawait thread.delete()\n\n# after\nthread = ResponseAgentThread(client=client)\nawait agent.invoke('hello', thread=thread)\nawait thread.delete()","handlingStrategy":"validation","validationCode":"if thread.response_id is None:\n    # nothing to delete; skip or send a message first\n    pass\nelse:\n    await thread.delete()","typeGuard":"def response_thread_has_id(thread) -> bool:\n    return getattr(thread, 'response_id', None) is not None","tryCatchPattern":"from semantic_kernel.exceptions.agent_exceptions import AgentThreadOperationException\ntry:\n    await thread.delete()\nexcept AgentThreadOperationException as e:\n    if 'not been created' in str(e):\n        pass  # nothing to delete","preventionTips":["Only delete after a message has been sent (response_id set).","Guard teardown with response_id is not None.","Send the user message before any cleanup path."],"tags":["thread-lifecycle","agents","openai-responses","state"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}