microsoft/semantic-kernel · error · ValueError

Client cannot be None

Error message

Client cannot be None

What it means

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.

Source

Thrown at python/semantic_kernel/agents/open_ai/openai_assistant_agent.py:159

        client: AsyncOpenAI,
        thread_id: str | None = None,
        messages: Iterable["ThreadCreateMessage"] | Omit = omit,
        metadata: dict[str, Any] | Omit = omit,
        tool_resources: ToolResources | Omit = omit,
    ) -> None:
        """Initialize the OpenAI Assistant Thread.

        Args:
            client: The AsyncOpenAI client.
            thread_id: The ID of the thread
            messages: The messages in the thread.
            metadata: The metadata.
            tool_resources: The tool resources.
        """
        super().__init__()

        if client is None:
            raise ValueError("Client cannot be None")

        self._client = client
        self._id = thread_id
        self._messages = messages
        self._metadata = metadata
        self._tool_resources = tool_resources

    @override
    async def _create(self) -> str:
        """Starts the thread and returns its ID."""
        try:
            response = await self._client.beta.threads.create(
                messages=self._messages,
                metadata=self._metadata,
                tool_resources=self._tool_resources,
            )
        except Exception as ex:
            raise AgentThreadOperationException(

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Pass a valid AsyncOpenAI or AsyncAzureOpenAI client instance to AssistantAgentThread.
  2. Construct the client before the thread and assert it is not None.
  3. Ensure upstream client-creation code does not return None or swallow errors.

Example fix

# before
thread = AssistantAgentThread(client=client)  # client is None
# after
from openai import AsyncOpenAI
client = AsyncOpenAI(api_key=...)
thread = AssistantAgentThread(client=client)
Defensive patterns

Strategy: validation

Validate before calling

if client is None:
    raise ValueError("Cannot build AssistantAgentThread: client is None (construct the OpenAI client first).")
thread = AssistantAgentThread(client=client)

Type guard

from openai import AsyncOpenAI, AsyncAzureOpenAI

def is_valid_client(c) -> bool:
    return isinstance(c, (AsyncOpenAI, AsyncAzureOpenAI))

Prevention

When it happens

Trigger: Instantiating AssistantAgentThread(client=None, ...) because the caller passed a client variable that was never assigned, or because client construction failed upstream and returned None.

Common situations: Building the client conditionally and forgetting the else branch; an exception during client creation swallowed, leaving client=None; refactoring that drops the client argument.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/dbc44463989a95ee. Report an issue: GitHub.