{"record":{"id":"12a095187af4ee8e","repo":"microsoft/semantic-kernel","slug":"cannot-delete-the-thread-since-it-has-not-been-cr","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/copilot_studio/copilot_studio_agent.py","lineNumber":246,"sourceCode":"    @override\n    @property\n    def id(self) -> str | None:\n        \"\"\"Get the thread ID.\"\"\"\n        return self.conversation_id\n\n    @override\n    async def _create(self) -> str:\n        # Creation is deferred to CopilotStudioAgent._ensure_conversation.\n        if self._is_deleted:\n            raise AgentThreadOperationException(\"Cannot create a thread that has been deleted.\")\n        return \"\"\n\n    @override\n    async def _delete(self) -> None:\n        if self._is_deleted:\n            return\n        if self.conversation_id is None:\n            raise AgentThreadOperationException(\"Cannot delete the thread, since it has not been created.\")\n        self._conversation_id = None\n        self._is_deleted = True\n\n    @override\n    async def _on_new_message(self, new_message: ChatMessageContent) -> None:\n        raise NotImplementedError(\n            \"This method is not implemented for CopilotStudioAgent. \"\n            \"Messages and responses are automatically handled by the Copilot Agent.\"\n        )\n\n\n@experimental\nclass CopilotStudioAgent(Agent):\n    \"\"\"Semantic Kernel abstraction over a Copilot Studio Agent.\"\"\"\n\n    client: CopilotClient\n    channel_type: ClassVar[type[AgentChannel] | None] = None\n","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/copilot_studio/copilot_studio_agent.py#L228-L264","documentation":"Raised by CopilotStudioAgentThread._delete() when conversation_id is None and the thread has not already been deleted. Since _delete() is idempotent for already-deleted threads (early return), this error only fires on the first delete attempt of a thread that never had a conversation started. It is an AgentThreadOperationException.","triggerScenarios":"Constructing a CopilotStudioAgentThread and calling thread.delete() before any agent invocation that would populate conversation_id via _ensure_conversation.","commonSituations":"Cleanup/teardown in a finally block that runs even when the conversation was never started; early-exit error paths that attempt to delete a thread that was created but never used.","solutions":["Guard the delete call: only delete if thread.conversation_id is not None.","Move thread creation closer to first use so it is always populated before cleanup.","Let the agent manage the thread lifecycle by not passing an explicit thread, so there is nothing to manually delete."],"exampleFix":"# before\nthread = CopilotStudioAgentThread(agent.client)\nawait thread.delete()  # raises — never started\n\n# after\nthread = CopilotStudioAgentThread(agent.client)\nif thread.conversation_id is not None:\n    await thread.delete()","handlingStrategy":"validation","validationCode":"if thread.conversation_id is not None:\n    await thread.delete()","typeGuard":null,"tryCatchPattern":"from semantic_kernel.exceptions.agent_exceptions import AgentThreadOperationException\n\ntry:\n    await thread.delete()\nexcept AgentThreadOperationException:\n    pass  # nothing to delete; safe to ignore","preventionTips":["Guard delete calls with a conversation_id check.","Use try/except AgentThreadOperationException in cleanup/finally blocks.","Create threads lazily, only when the conversation is actually about to start."],"tags":["thread","lifecycle","copilot-studio"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}