microsoft/semantic-kernel · error · AgentInitializationException
Could not generate schema for the type {response_format}.
Error message
Could not generate schema for the type {response_format}. What it means
Raised by configure_response_format() on the plain-Python-class branch when KernelJsonSchemaBuilder.build(parameter_type=response_format, structured_output=True) returns None. The builder returns None when it cannot derive a JSON schema for the given type, so the method cannot construct a structured output envelope.
Source
Thrown at python/semantic_kernel/agents/open_ai/openai_responses_agent.py:801
f"Encountered unexpected response_format type: {resp_type}. Allowed types are `json_object` "
" and `json_schema`."
)
if isinstance(response_format, type):
if issubclass(response_format, BaseModel):
interim_format = type_to_text_format_param(response_format)
if interim_format["type"] != "json_schema":
raise AgentInitializationException("Only 'json_schema' is allowed from that helper.")
configured_format = {
"type": "json_schema",
"name": interim_format.get("name", response_format.__name__),
"schema": interim_format.get("schema"),
"strict": interim_format.get("strict", True),
}
else:
# Build a schema from a plain Python class
generated_schema = KernelJsonSchemaBuilder.build(parameter_type=response_format, structured_output=True)
if generated_schema is None:
raise AgentInitializationException(f"Could not generate schema for the type {response_format}.")
configured_format = {
"type": "json_schema",
"name": response_format.__name__,
"schema": generated_schema,
"strict": True,
}
else:
raise AgentInitializationException(
"response_format must be a dictionary, a subclass of BaseModel, a Python class/type, or None"
)
return {"format": configured_format}
# endregion
# region Invocation Methods
@trace_agent_get_responseView on GitHub (pinned to c028a0c7dc)
Solutions
- Convert the class to a pydantic BaseModel (preferred — uses the BaseModel branch and the openai helper).
- Ensure all fields have type annotations the builder understands.
- Build the JSON schema yourself and pass it via the dict path with type 'json_schema'.
- Log KernelJsonSchemaBuilder.build(...) in isolation to see why it returns None.
Example fix
// before
class Out:
value = None # no annotation
cfg = OpenAIResponsesAgent.configure_response_format(Out)
// after
from pydantic import BaseModel
class Out(BaseModel):
value: str
cfg = OpenAIResponsesAgent.configure_response_format(Out) Defensive patterns
Strategy: validation
Validate before calling
from semantic_kernel.schema.kernel_json_schema_builder import KernelJsonSchemaBuilder
schema = KernelJsonSchemaBuilder.build(parameter_type=response_format, structured_output=True)
if schema is None:
raise ValueError('Cannot derive schema; convert to BaseModel or supply dict') Type guard
def type_has_schema(t: type) -> bool:
from semantic_kernel.schema.kernel_json_schema_builder import KernelJsonSchemaBuilder
return KernelJsonSchemaBuilder.build(parameter_type=t, structured_output=True) is not None Try / catch
from semantic_kernel.exceptions.agent_exceptions import AgentInitializationException
try:
cfg = OpenAIResponsesAgent.configure_response_format(MyClass)
except AgentInitializationException as e:
if 'generate schema' in str(e):
from pydantic import BaseModel
class MyModel(BaseModel):
... # mirror MyClass
cfg = OpenAIResponsesAgent.configure_response_format(MyModel)
raise Prevention
- Prefer pydantic BaseModel over plain classes for structured output.
- Ensure all fields are type-annotated with schema-compatible types.
- Smoke-test KernelJsonSchemaBuilder.build() on your type before wiring it in.
When it happens
Trigger: Passing a plain Python class/type (not a pydantic BaseModel) whose annotations or structure the KernelJsonSchemaBuilder cannot introspect — e.g. a class with no type-annotated fields, dynamic/Generated types, or types the builder explicitly skips.
Common situations: Using a dataclass or TypedDict the schema builder doesn't support, a class with untyped attributes, or passing a builtin/abstract type. The builder yields None rather than a partial/invalid schema.
Related errors
- If response_format has type 'json_schema', 'json_schema' mus
- Encountered unexpected response_format type: {resp_type}. Al
- Only 'json_schema' is allowed from that helper.
- The option keys 'name' and 'type' are required for a paramet
- The provided response format '{formatString}' is not support
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/e4d2ecef52033eb8.
Report an issue: GitHub.