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 by AzureAIAgentThread._create when the underlying client.agents.threads.create call raises any exception. The original service exception is chained (from ex) but the message is generic, so the cause must be inspected. Surfaced as AgentThreadOperationException.

Source

Thrown at python/semantic_kernel/agents/azure_ai/azure_ai_agent.py:302

            raise ValueError("Client cannot be None")

        self._client = client
        self._id = thread_id
        self._messages = messages or []
        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.agents.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.agents.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 exception ('from ex') to get the real status code and service error message.
  2. Verify the AIProjectClient credential is valid and not expired (re-auth or use DefaultAzureCredential).
  3. Validate tool_resources and messages payloads against the Azure AI Agents API schema before creating.
  4. Retry on transient failures (429/5xx) with backoff; treat 4xx as configuration errors.

Example fix

try:
    await thread.create()
except AgentThreadOperationException as e:
    cause = e.__cause__  # inspect real service error
    if _is_transient(cause):
        await asyncio.sleep(backoff); await thread.create()
    else:
        raise
Defensive patterns

Strategy: try-catch

Validate before calling

def validate_thread_inputs(messages, metadata, tool_resources):
    if messages is not None and not isinstance(messages, list):
        raise TypeError('messages must be a list or None')
    if tool_resources is not None and not isinstance(tool_resources, dict):
        raise TypeError('tool_resources must be a dict or None')
    return True

Try / catch

from semantic_kernel.exceptions.agent_exceptions import AgentThreadOperationException
try:
    await thread.create()
except AgentThreadOperationException as e:
    cause = e.__cause__
    if _is_transient(cause):
        await asyncio.sleep(backoff); await thread.create()
    else:
        log.error('Thread create failed: %r', cause); raise

Prevention

When it happens

Trigger: Invalid or malformed tool_resources passed to create; authentication failure (expired token, wrong endpoint); throttling/quota limits; network interruption; invalid messages format; the Azure AI Foundry project not existing at the given endpoint.

Common situations: Token credential expired during a long-running session; wrong connection string/endpoint configured; passing tool_resources referencing a resource (e.g. code interpreter file IDs) that does not exist; transient 429/5xx from the service under load.

Related errors


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