{"record":{"id":"9b1a9d43f8744771","repo":"microsoft/semantic-kernel","slug":"failed-to-delete-thread-e-9b1a9d","errorCode":null,"errorMessage":"Failed to delete thread: {e}","messagePattern":"Failed to delete thread: (.+?)","errorType":"exception","errorClass":"AgentChatException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/channels/open_ai_assistant_channel.py","lineNumber":121,"sourceCode":"            assistant_name = None\n            if message.assistant_id and message.assistant_id not in agent_names:\n                agent = await self.client.beta.assistants.retrieve(message.assistant_id)\n                if agent.name:\n                    agent_names[message.assistant_id] = agent.name\n            assistant_name = agent_names.get(message.assistant_id) if message.assistant_id else message.assistant_id\n\n            content: ChatMessageContent = generate_message_content(str(assistant_name), message)\n\n            if len(content.items) > 0:\n                yield content\n\n    @override\n    async def reset(self) -> None:\n        \"\"\"Reset the agent's thread.\"\"\"\n        try:\n            await self.client.beta.threads.delete(thread_id=self.thread_id)\n        except Exception as e:\n            raise AgentChatException(f\"Failed to delete thread: {e}\")\n","sourceCodeStart":103,"sourceCodeEnd":122,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/channels/open_ai_assistant_channel.py#L103-L122","documentation":"Raised by OpenAIAssistantChannel.reset as an AgentChatException wrapping any exception thrown while deleting the OpenAI Assistants thread via self.client.beta.threads.delete(thread_id=...). It surfaces a thread-deletion failure (network, auth, 404 already-deleted, rate limit) as a chat-level exception.","triggerScenarios":"Calling channel.reset()/chat teardown that deletes the assistant thread when the thread id is invalid/already deleted, the OpenAI client lacks permissions, the request times out, or the API rate-limits the delete.","commonSituations":"Teardown running twice (double delete of the same thread); expired/invalid API key; network blip during cleanup; thread already removed out-of-band; stale thread_id held after deletion.","solutions":["Catch AgentChatException around reset/teardown and log it so cleanup failures do not crash the app.","Avoid calling reset twice on the same channel; mark the thread as deleted and skip.","Confirm the thread_id is current and not already deleted before reset.","Verify the OpenAI client/API key is valid and has threads:delete permission; retry transient network errors."],"exampleFix":"// before\nawait chat.reset()  # may raise AgentChatException\n// after\ntry:\n    await chat.reset()\nexcept AgentChatException as e:\n    logger.warning(f'Thread cleanup failed: {e}')","handlingStrategy":"try-catch","validationCode":"deleted = False\ndef maybe_reset(chat):\n    global deleted\n    if deleted: return\n    try:\n        await chat.reset(); deleted = True\n    except Exception: raise","typeGuard":null,"tryCatchPattern":"from semantic_kernel.exceptions.agent_exceptions import AgentChatException\ntry:\n    await chat.reset()\nexcept AgentChatException as e:\n    logger.warning(f'Thread deletion failed (may already be gone): {e}')","preventionTips":["Idempotent teardown: mark thread deleted and skip double reset","Validate thread_id currency and API key before reset","Retry transient network errors once"],"tags":["openai","assistant","channel","thread-lifecycle","network","try-catch"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}