microsoft/semantic-kernel · error · AgentThreadOperationException

The thread could not be created due to an error response fro

Error message

The thread could not be created due to an error response from the service.

What it means

Raised as AgentThreadOperationException by AssistantAgentThread._create when the underlying client.beta.threads.create() call throws. The original exception is chained (from ex) so the real service error (rate limit, auth, bad tool_resources) is preserved in __cause__.

Source

Thrown at python/semantic_kernel/agents/open_ai/openai_assistant_agent.py:177

            raise ValueError("Client cannot be None")

        self._client = client
        self._id = thread_id
        self._messages = messages
        self._metadata = metadata
        self._tool_resources = tool_resources

    @override
    async def _create(self) -> str:
        """Starts the thread and returns its ID."""
        try:
            response = await self._client.beta.threads.create(
                messages=self._messages,
                metadata=self._metadata,
                tool_resources=self._tool_resources,
            )
        except Exception as ex:
            raise AgentThreadOperationException(
                "The thread could not be created due to an error response from the service."
            ) from ex
        return response.id

    @override
    async def _delete(self) -> None:
        """Ends the current thread."""
        if self._id is None:
            raise AgentThreadOperationException("The thread cannot be deleted because it has not been created yet.")
        try:
            await self._client.beta.threads.delete(self._id)
        except Exception as ex:
            raise AgentThreadOperationException(
                "The thread could not be deleted due to an error response from the service."
            ) from ex

    @override
    async def _on_new_message(self, new_message: str | ChatMessageContent) -> None:

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect the chained __cause__ for the true status code and message.
  2. Retry with backoff for transient (429/5xx) errors.
  3. Validate metadata (<=16 key-value pairs, keys<=64 chars) and tool_resources shape before creating.
  4. Refresh credentials if the cause is a 401/403.

Example fix

# before
try:
    await thread.create()
except AgentThreadOperationException:
    pass  # original cause lost
# after
try:
    await thread.create()
except AgentThreadOperationException as e:
    logging.error("thread create failed: %s", e.__cause__)
    raise
Defensive patterns

Strategy: try-catch

Validate before calling

from semantic_kernel.exceptions.agent_exceptions import AgentThreadOperationException

# validate payload shape before creating
assert isinstance(metadata, dict) and len(metadata) <= 16
try:
    await thread.create()
except AgentThreadOperationException as e:
    logging.error("thread create failed: %s", e.__cause__)
    raise

Try / catch

import asyncio
from semantic_kernel.exceptions.agent_exceptions import AgentThreadOperationException

async def create_thread(thread, attempts=3):
    for i in range(attempts):
        try:
            return await thread.create()
        except AgentThreadOperationException as e:
            cause = e.__cause__
            status = getattr(cause, "status_code", None)
            if status in (429, 500, 502, 503) and i < attempts - 1:
                await asyncio.sleep(2 ** i)
                continue
            raise

Prevention

When it happens

Trigger: Calling thread.create() (which invokes _create) when the OpenAI service rejects thread creation: invalid metadata/tool_resources payload, authentication failure, rate limiting, or a network error.

Common situations: Expired API key or token; malformed tool_resources/messages; hitting rate limits; transient network issues; passing messages with an unsupported role.

Related errors


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