microsoft/semantic-kernel · error · AgentInitializationException

response_format must be a dictionary, a subclass of BaseMode

Error message

response_format must be a dictionary, a subclass of BaseModel, a Python class/type, or None

What it means

Raised by configure_response_format() when response_format is not None/'auto', not a dict, and not a Python type/class. The method supports dict, BaseModel subclass, a plain class (schema built via KernelJsonSchemaBuilder), or None; anything else (a string, an int, a list, an instance object) hits the final else branch.

Source

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

                configured_response_format = response_format  # type: ignore
            else:
                raise AgentInitializationException(
                    f"Encountered unexpected response_format type: {resp_type}. Allowed types are `json_object` "
                    " and `json_schema`."
                )
        elif isinstance(response_format, type):
            # If it's a type, differentiate based on whether it's a BaseModel subclass
            if issubclass(response_format, BaseModel):
                configured_response_format = type_to_response_format_param(response_format)  # type: ignore
            else:
                generated_schema = KernelJsonSchemaBuilder.build(parameter_type=response_format, structured_output=True)
                assert generated_schema is not None  # nosec
                configured_response_format = generate_structured_output_response_format_schema(
                    name=response_format.__name__, schema=generated_schema
                )
        else:
            # If it's not a dict or a type, throw an exception
            raise AgentInitializationException(
                "response_format must be a dictionary, a subclass of BaseModel, a Python class/type, or None"
            )

        return configured_response_format  # type: ignore

    # endregion

    # region Agent Channel Methods

    def get_channel_keys(self) -> Iterable[str]:
        """Get the channel keys.

        Returns:
            Iterable[str]: The channel keys.
        """
        # Distinguish from other channel types.
        yield f"{OpenAIAssistantAgent.__name__}"

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Pass None or 'auto' for default text, a dict for json_object/json_schema, or a BaseModel subclass / plain class for schema generation.
  2. If you meant JSON mode, pass {'type':'json_object'} not the bare string.
  3. Type-check response_format before the call: must be None, dict, or type.

Example fix

# before
fmt = OpenAIAssistantAgent.configure_response_format('json_object')

# after
fmt = OpenAIAssistantAgent.configure_response_format({'type':'json_object'})
Defensive patterns

Strategy: type-guard

Validate before calling

from typing import is_type
ok = response_format is None or isinstance(response_format, dict) or is_type(response_format)
assert ok, 'response_format must be None, dict, or a class'

Type guard

import inspect
def is_supported_response_format(rf) -> bool:
    if rf is None or isinstance(rf, dict):
        return True
    return inspect.isclass(rf)

Try / catch

from semantic_kernel.exceptions.agent_exceptions import AgentInitializationException
try:
    fmt = OpenAIAssistantAgent.configure_response_format(response_format)
except AgentInitializationException as e:
    if 'must be a dictionary' in str(e):
        fmt = None  # default to text

Prevention

When it happens

Trigger: Passing response_format='json_object' (string instead of dict), an instance of a model rather than the class, a list, or any other non-supported value.

Common situations: Passing a string shorthand ('auto' is special-cased but other strings are not); passing a model instance instead of the model class; integrating with code that sends arbitrary JSON values.

Related errors


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