{"record":{"id":"f9988e72fbea59e3","repo":"microsoft/semantic-kernel","slug":"the-thread-could-not-be-created-due-to-an-error-re-f9988e","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/open_ai/openai_assistant_agent.py","lineNumber":177,"sourceCode":"            raise ValueError(\"Client cannot be None\")\n\n        self._client = client\n        self._id = thread_id\n        self._messages = messages\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.beta.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.beta.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":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/open_ai/openai_assistant_agent.py#L159-L195","documentation":"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__.","triggerScenarios":"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.","commonSituations":"Expired API key or token; malformed tool_resources/messages; hitting rate limits; transient network issues; passing messages with an unsupported role.","solutions":["Inspect the chained __cause__ for the true status code and message.","Retry with backoff for transient (429/5xx) errors.","Validate metadata (<=16 key-value pairs, keys<=64 chars) and tool_resources shape before creating.","Refresh credentials if the cause is a 401/403."],"exampleFix":"# before\ntry:\n    await thread.create()\nexcept AgentThreadOperationException:\n    pass  # original cause lost\n# after\ntry:\n    await thread.create()\nexcept AgentThreadOperationException as e:\n    logging.error(\"thread create failed: %s\", e.__cause__)\n    raise","handlingStrategy":"try-catch","validationCode":"from semantic_kernel.exceptions.agent_exceptions import AgentThreadOperationException\n\n# validate payload shape before creating\nassert isinstance(metadata, dict) and len(metadata) <= 16\ntry:\n    await thread.create()\nexcept AgentThreadOperationException as e:\n    logging.error(\"thread create failed: %s\", e.__cause__)\n    raise","typeGuard":null,"tryCatchPattern":"import asyncio\nfrom semantic_kernel.exceptions.agent_exceptions import AgentThreadOperationException\n\nasync def create_thread(thread, attempts=3):\n    for i in range(attempts):\n        try:\n            return await thread.create()\n        except AgentThreadOperationException as e:\n            cause = e.__cause__\n            status = getattr(cause, \"status_code\", None)\n            if status in (429, 500, 502, 503) and i < attempts - 1:\n                await asyncio.sleep(2 ** i)\n                continue\n            raise","preventionTips":["Always log e.__cause__ to see the real service status code.","Validate metadata (<=16 pairs) and tool_resources before create().","Retry transient 429/5xx with exponential backoff."],"tags":["openai","semantic-kernel","agents","threads","network","service-error","retry"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}