{"record":{"id":"5d11ea7b1bdf7e35","repo":"microsoft/semantic-kernel","slug":"client-cannot-be-none","errorCode":null,"errorMessage":"Client cannot be None","messagePattern":"Client cannot be None","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/azure_ai/azure_ai_agent.py","lineNumber":284,"sourceCode":"        client: AIProjectClient,\n        messages: list[ThreadMessageOptions] | None = None,\n        metadata: dict[str, str] | None = None,\n        thread_id: str | None = None,\n        tool_resources: \"ToolResources | None\" = None,\n    ) -> None:\n        \"\"\"Initialize the Azure AI Agent Thread.\n\n        Args:\n            client: The Azure AI Project client.\n            messages: The messages to initialize the thread with.\n            metadata: The metadata for the thread.\n            thread_id: The ID of the thread\n            tool_resources: The tool resources for the thread.\n        \"\"\"\n        super().__init__()\n\n        if client is None:\n            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(","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/azure_ai/azure_ai_agent.py#L266-L302","documentation":"Raised by AzureAIAgentThread.__init__ when the client argument is None. The thread delegates all operations (create/delete/messages) to an AIProjectClient, so a null client makes it non-functional. Thrown as a plain ValueError, not an agent exception, at construction time.","triggerScenarios":"Instantiating AzureAIAgentThread(client=None) directly; constructing the thread from a factory that failed to capture self.client; calling construct_thread=lambda: AzureAIAgentThread() without the client argument.","commonSituations":"Building a custom thread subclass or factory and forgetting to forward the client; refactoring that moved client creation into a branch that returned None on a config error; tests that construct the thread without mocking a client.","solutions":["Pass a valid AIProjectClient instance to AzureAIAgentThread(client=...).","Create the client via AzureAIAgent.create_client(credential, endpoint) first and reuse it for both the agent and the thread.","Ensure the client is created once and shared rather than constructed conditionally."],"exampleFix":"// before\nthread = AzureAIAgentThread()  // client missing\n// after\nclient = await AzureAIAgent.create_client(credential, endpoint)\nthread = AzureAIAgentThread(client=client)","handlingStrategy":"type-guard","validationCode":"from azure.ai.projects.aio import AIProjectClient\ndef assert_client(client):\n    if not isinstance(client, AIProjectClient):\n        raise TypeError('client must be an AIProjectClient')\n    return client","typeGuard":"def is_valid_client(client) -> bool:\n    return client is not None and isinstance(client, AIProjectClient)","tryCatchPattern":"try:\n    thread = AzureAIAgentThread(client=client)\nexcept ValueError as e:\n    if 'Client cannot be None' in str(e):\n        log.error('Create the AIProjectClient before constructing the thread')\n    raise","preventionTips":["Construct the client once via AzureAIAgent.create_client and pass it everywhere.","In factories, assert the client is non-None before building a thread.","Share a single client across the agent and its threads."],"tags":["azure-ai","thread","client","configuration","agent-initialization"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}