{"record":{"id":"dbc44463989a95ee","repo":"microsoft/semantic-kernel","slug":"client-cannot-be-none-dbc444","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/open_ai/openai_assistant_agent.py","lineNumber":159,"sourceCode":"        client: AsyncOpenAI,\n        thread_id: str | None = None,\n        messages: Iterable[\"ThreadCreateMessage\"] | Omit = omit,\n        metadata: dict[str, Any] | Omit = omit,\n        tool_resources: ToolResources | Omit = omit,\n    ) -> None:\n        \"\"\"Initialize the OpenAI Assistant Thread.\n\n        Args:\n            client: The AsyncOpenAI client.\n            thread_id: The ID of the thread\n            messages: The messages in the thread.\n            metadata: The metadata.\n            tool_resources: The tool resources.\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\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(","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/open_ai/openai_assistant_agent.py#L141-L177","documentation":"Raised as a plain ValueError (not an AgentException) by AssistantAgentThread.__init__ when the client argument is None. The thread delegates all service calls to the AsyncOpenAI/AsyncAzureOpenAI client, so a missing client would AttributeError on the first call; the guard fails fast at construction.","triggerScenarios":"Instantiating AssistantAgentThread(client=None, ...) because the caller passed a client variable that was never assigned, or because client construction failed upstream and returned None.","commonSituations":"Building the client conditionally and forgetting the else branch; an exception during client creation swallowed, leaving client=None; refactoring that drops the client argument.","solutions":["Pass a valid AsyncOpenAI or AsyncAzureOpenAI client instance to AssistantAgentThread.","Construct the client before the thread and assert it is not None.","Ensure upstream client-creation code does not return None or swallow errors."],"exampleFix":"# before\nthread = AssistantAgentThread(client=client)  # client is None\n# after\nfrom openai import AsyncOpenAI\nclient = AsyncOpenAI(api_key=...)\nthread = AssistantAgentThread(client=client)","handlingStrategy":"validation","validationCode":"if client is None:\n    raise ValueError(\"Cannot build AssistantAgentThread: client is None (construct the OpenAI client first).\")\nthread = AssistantAgentThread(client=client)","typeGuard":"from openai import AsyncOpenAI, AsyncAzureOpenAI\n\ndef is_valid_client(c) -> bool:\n    return isinstance(c, (AsyncOpenAI, AsyncAzureOpenAI))","tryCatchPattern":null,"preventionTips":["Construct the OpenAI client in a factory that never returns None.","Do not swallow exceptions during client creation; let them propagate.","Assert the client is not None before building the thread."],"tags":["openai","semantic-kernel","agents","threads","initialization","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}