{"record":{"id":"e4aef043d5f06c11","repo":"microsoft/semantic-kernel","slug":"the-thread-could-not-be-created-due-to-an-error-re-e4aef0","errorCode":null,"errorMessage":"The thread could not be created due to an error response from the service.","messagePattern":"The thread could not be created due to an error response from the service\\.","errorType":"exception","errorClass":"AgentThreadOperationException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/azure_ai/azure_ai_agent.py","lineNumber":302,"sourceCode":"            raise ValueError(\"Client cannot be None\")\n\n        self._client = client\n        self._id = thread_id\n        self._messages = messages or []\n        self._metadata = metadata\n        self._tool_resources = tool_resources\n\n    @override\n    async def _create(self) -> str:\n        \"\"\"Starts the thread and returns its ID.\"\"\"\n        try:\n            response = await self._client.agents.threads.create(\n                messages=self._messages,\n                metadata=self._metadata,\n                tool_resources=self._tool_resources,\n            )\n        except Exception as ex:\n            raise AgentThreadOperationException(\n                \"The thread could not be created due to an error response from the service.\"\n            ) from ex\n        return response.id\n\n    @override\n    async def _delete(self) -> None:\n        \"\"\"Ends the current thread.\"\"\"\n        if self._id is None:\n            raise AgentThreadOperationException(\"The thread cannot be deleted because it has not been created yet.\")\n        try:\n            await self._client.agents.threads.delete(self._id)\n        except Exception as ex:\n            raise AgentThreadOperationException(\n                \"The thread could not be deleted due to an error response from the service.\"\n            ) from ex\n\n    @override\n    async def _on_new_message(self, new_message: str | ChatMessageContent) -> None:","sourceCodeStart":284,"sourceCodeEnd":320,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/azure_ai/azure_ai_agent.py#L284-L320","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the chained exception ('from ex') to get the real status code and service error message.","Verify the AIProjectClient credential is valid and not expired (re-auth or use DefaultAzureCredential).","Validate tool_resources and messages payloads against the Azure AI Agents API schema before creating.","Retry on transient failures (429/5xx) with backoff; treat 4xx as configuration errors."],"exampleFix":"try:\n    await thread.create()\nexcept AgentThreadOperationException as e:\n    cause = e.__cause__  # inspect real service error\n    if _is_transient(cause):\n        await asyncio.sleep(backoff); await thread.create()\n    else:\n        raise","handlingStrategy":"try-catch","validationCode":"def validate_thread_inputs(messages, metadata, tool_resources):\n    if messages is not None and not isinstance(messages, list):\n        raise TypeError('messages must be a list or None')\n    if tool_resources is not None and not isinstance(tool_resources, dict):\n        raise TypeError('tool_resources must be a dict or None')\n    return True","typeGuard":null,"tryCatchPattern":"from semantic_kernel.exceptions.agent_exceptions import AgentThreadOperationException\ntry:\n    await thread.create()\nexcept AgentThreadOperationException as e:\n    cause = e.__cause__\n    if _is_transient(cause):\n        await asyncio.sleep(backoff); await thread.create()\n    else:\n        log.error('Thread create failed: %r', cause); raise","preventionTips":["Inspect the chained cause for the real HTTP status before deciding to retry.","Validate tool_resources and messages shape before create.","Ensure credentials are fresh for long-running sessions."],"tags":["azure-ai","thread","network","service-error","authentication"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}