{"record":{"id":"612673482545a329","repo":"microsoft/semantic-kernel","slug":"copilotclient-cannot-be-none","errorCode":null,"errorMessage":"CopilotClient cannot be None","messagePattern":"CopilotClient cannot be None","errorType":"exception","errorClass":"AgentThreadInitializationException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/copilot_studio/copilot_studio_agent.py","lineNumber":213,"sourceCode":"\n@experimental\nclass CopilotStudioAgentThread(AgentThread):\n    \"\"\"The Copilot Studio Agent Thread.\"\"\"\n\n    def __init__(\n        self,\n        client: CopilotClient,\n        conversation_id: str | None = None,\n    ) -> None:\n        \"\"\"Initializes a new instance of the CopilotStudioAgentThread class.\n\n        Args:\n            client: The Copilot Client.\n            conversation_id: The conversation ID. This is the Copilot Studio conversation ID.\n        \"\"\"\n        super().__init__()\n        if client is None:\n            raise AgentThreadInitializationException(\"CopilotClient cannot be None\")\n\n        self._client = client\n        self._conversation_id = conversation_id  # Copilot Studio conversation ID\n\n    @property\n    def conversation_id(self) -> str | None:\n        \"\"\"Get the conversation ID.\"\"\"\n        return self._conversation_id\n\n    @conversation_id.setter\n    def conversation_id(self, value: str | None) -> None:\n        \"\"\"Set the conversation ID.\"\"\"\n        self._conversation_id = value\n\n    @override\n    @property\n    def id(self) -> str | None:\n        \"\"\"Get the thread ID.\"\"\"","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/copilot_studio/copilot_studio_agent.py#L195-L231","documentation":"Raised by CopilotStudioAgentThread.__init__ when the client argument is None. Although the type hint declares client as CopilotClient (non-optional), Python does not enforce this at runtime, so the constructor explicitly guards against None with an AgentThreadInitializationException. The thread needs a live CopilotClient to start conversations and ask questions.","triggerScenarios":"Manually constructing CopilotStudioAgentThread(client=None) or passing a variable that evaluated to None. Also possible if create_client() returned None unexpectedly (it never does in normal flow, but a monkeypatched or mocked path could).","commonSituations":"Calling code builds the thread separately from the agent and forgets to pass the agent's client; using a factory or lambda that yields None under an edge condition; test mocks that return None for the client.","solutions":["Pass the agent's client instance: CopilotStudioAgentThread(agent.client).","If constructing the thread before the agent, create the client first via CopilotStudioAgent.create_client(...) and pass it to both.","Avoid manual thread construction — let CopilotStudioAgent.invoke/get_response create the thread internally by omitting the thread argument."],"exampleFix":"# before\nthread = CopilotStudioAgentThread(client=None)\n\n# after\nthread = CopilotStudioAgentThread(client=agent.client)","handlingStrategy":"validation","validationCode":"if client is None:\n    raise ValueError(\"client must not be None; create one via CopilotStudioAgent.create_client()\")\nthread = CopilotStudioAgentThread(client=client)","typeGuard":"from microsoft_agents.copilotstudio.client import CopilotClient\n\ndef is_valid_client(c) -> bool:\n    return isinstance(c, CopilotClient)","tryCatchPattern":null,"preventionTips":["Always pass agent.client when constructing a thread manually.","Prefer omitting the thread argument so the agent creates it internally.","Never construct CopilotStudioAgentThread before the agent/client is initialized."],"tags":["validation","thread","copilot-studio","initialization"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}