microsoft/semantic-kernel · error · AgentChatException

Failed to delete thread: {e}

Error message

Failed to delete thread: {e}

What it means

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.

Source

Thrown at python/semantic_kernel/agents/channels/open_ai_assistant_channel.py:121

            assistant_name = None
            if message.assistant_id and message.assistant_id not in agent_names:
                agent = await self.client.beta.assistants.retrieve(message.assistant_id)
                if agent.name:
                    agent_names[message.assistant_id] = agent.name
            assistant_name = agent_names.get(message.assistant_id) if message.assistant_id else message.assistant_id

            content: ChatMessageContent = generate_message_content(str(assistant_name), message)

            if len(content.items) > 0:
                yield content

    @override
    async def reset(self) -> None:
        """Reset the agent's thread."""
        try:
            await self.client.beta.threads.delete(thread_id=self.thread_id)
        except Exception as e:
            raise AgentChatException(f"Failed to delete thread: {e}")

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Catch AgentChatException around reset/teardown and log it so cleanup failures do not crash the app.
  2. Avoid calling reset twice on the same channel; mark the thread as deleted and skip.
  3. Confirm the thread_id is current and not already deleted before reset.
  4. Verify the OpenAI client/API key is valid and has threads:delete permission; retry transient network errors.

Example fix

// before
await chat.reset()  # may raise AgentChatException
// after
try:
    await chat.reset()
except AgentChatException as e:
    logger.warning(f'Thread cleanup failed: {e}')
Defensive patterns

Strategy: try-catch

Validate before calling

deleted = False
def maybe_reset(chat):
    global deleted
    if deleted: return
    try:
        await chat.reset(); deleted = True
    except Exception: raise

Try / catch

from semantic_kernel.exceptions.agent_exceptions import AgentChatException
try:
    await chat.reset()
except AgentChatException as e:
    logger.warning(f'Thread deletion failed (may already be gone): {e}')

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/9b1a9d43f8744771. Report an issue: GitHub.