microsoft/semantic-kernel · error · AgentInitializationException

Missing required 'client' in OpenAIAssistantAgent._from_dict

Error message

Missing required 'client' in OpenAIAssistantAgent._from_dict()

What it means

Raised by OpenAIAssistantAgent._from_dict() when the kwargs passed to the declarative spec builder do not contain a 'client' key (or it is None). The declarative/YAML deserialization path needs an already-constructed AsyncOpenAI client because it cannot infer credentials from the spec dict alone; without it, no assistant can be retrieved or created.

Source

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

        *,
        kernel: Kernel,
        prompt_template_config: "PromptTemplateConfig | None" = None,
        **kwargs,
    ) -> _T:
        """Create an Assistant Agent from the provided dictionary.

        Args:
            data: The dictionary containing the agent data.
            kernel: The kernel to use for the agent.
            prompt_template_config: The prompt template configuration.
            kwargs: Additional keyword arguments. Note: unsupported keys may raise validation errors.

        Returns:
            AzureAIAgent: The OpenAI Assistant Agent instance.
        """
        client: AsyncOpenAI = kwargs.pop("client", None)
        if client is None:
            raise AgentInitializationException("Missing required 'client' in OpenAIAssistantAgent._from_dict()")

        spec = AgentSpec.model_validate(data)

        if "settings" in kwargs:
            kwargs.pop("settings")

        args = data.pop("arguments", None)
        arguments = None
        if args:
            arguments = KernelArguments(**args)

        # Handle arguments from kwargs, merging with any arguments from data
        if "arguments" in kwargs and kwargs["arguments"] is not None:
            incoming_args = kwargs["arguments"]
            arguments = arguments | incoming_args if arguments is not None else incoming_args

        if spec.id:
            existing_definition = await client.beta.assistants.retrieve(spec.id)

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Pass an AsyncOpenAI instance: OpenAIAssistantAgent.from_dict(data, kernel=kernel, client=client).
  2. Build the client first via create_client()/create_client_and_model() and forward it.
  3. If using a registry, ensure the client is registered/injected before _from_dict is dispatched.

Example fix

# before
agent = await OpenAIAssistantAgent.from_dict(spec_dict, kernel=kernel)

# after
client = await OpenAIAssistantAgent.create_client(api_key=os.environ['OPENAI_API_KEY'], ai_model_id='gpt-4o')
agent = await OpenAIAssistantAgent.from_dict(spec_dict, kernel=kernel, client=client)
Defensive patterns

Strategy: type-guard

Validate before calling

from openai import AsyncOpenAI
client = kwargs.get('client')
assert isinstance(client, AsyncOpenAI), 'client kwarg must be an AsyncOpenAI instance'

Type guard

from openai import AsyncOpenAI
def has_valid_client(kwargs: dict) -> bool:
    return isinstance(kwargs.get('client'), AsyncOpenAI)

Try / catch

from semantic_kernel.exceptions.agent_exceptions import AgentInitializationException
try:
    agent = await OpenAIAssistantAgent.from_dict(spec, kernel=kernel, client=client)
except AgentInitializationException as e:
    if 'client' in str(e):
        client = await OpenAIAssistantAgent.create_client(api_key=key, ai_model_id=mid)
        agent = await OpenAIAssistantAgent.from_dict(spec, kernel=kernel, client=client)

Prevention

When it happens

Trigger: Calling OpenAIAssistantAgent.from_dict(data, kernel=kernel) without passing client=... in kwargs; the framework dispatching _from_dict from a registry restore that omitted the client; passing client=None explicitly.

Common situations: Loading agents from YAML/JSON without supplying the client; upgrading code that previously inferred the client automatically; misconfigured agent registry/factory that forgets to thread the client through.

Related errors


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