openai/openai-python · error · TypeError
Non BaseModel types are only supported with Pydantic v2 - {m
Error message
Non BaseModel types are only supported with Pydantic v2 - {model} What it means
to_strict_json_schema converts a type into the strict JSON schema the API needs for structured outputs. It accepts only pydantic BaseModel classes or pydantic v2 TypeAdapter instances; anything else (plain classes, pydantic v1 types, instances) raises this TypeError. Note dataclass-like types must already be wrapped in a TypeAdapter by callers such as type_to_response_format_param.
Source
Thrown at src/openai/lib/_pydantic.py:22
from typing import Any, TypeVar
from typing_extensions import TypeGuard
import pydantic
from .._types import NOT_GIVEN
from .._utils import is_dict as _is_dict, is_list
from .._compat import PYDANTIC_V1, model_json_schema
_T = TypeVar("_T")
def to_strict_json_schema(model: type[pydantic.BaseModel] | pydantic.TypeAdapter[Any]) -> dict[str, Any]:
if inspect.isclass(model) and is_basemodel_type(model):
schema = model_json_schema(model)
elif (not PYDANTIC_V1) and isinstance(model, pydantic.TypeAdapter):
schema = model.json_schema()
else:
raise TypeError(f"Non BaseModel types are only supported with Pydantic v2 - {model}")
return _ensure_strict_json_schema(schema, path=(), root=schema)
def _ensure_strict_json_schema(
json_schema: object,
*,
path: tuple[str, ...],
root: dict[str, object],
) -> dict[str, Any]:
"""Mutates the given JSON schema to ensure it conforms to the `strict` standard
that the API expects.
"""
if not is_dict(json_schema):
raise TypeError(f"Expected {json_schema} to be a dictionary; path={path}")
defs = json_schema.get("$defs")
if is_dict(defs):View on GitHub (pinned to 9917c6e28e)
Solutions
- Pass the pydantic BaseModel class itself (not an instance)
- For dataclass/TypedDict types, wrap with pydantic.TypeAdapter (requires pydantic v2)
- Use the SDK helpers pydantic_function_tool / type_to_response_format_param which do the wrapping for you
Example fix
# before schema = to_strict_json_schema(MyDataclass) # after schema = to_strict_json_schema(pydantic.TypeAdapter(MyDataclass))
Defensive patterns
Strategy: type-guard
Type guard
def strict_schema_input_ok(obj: object) -> bool:
if inspect.isclass(obj):
return is_basemodel_type(obj)
return (not PYDANTIC_V1) and isinstance(obj, pydantic.TypeAdapter) Prevention
- Prefer SDK helpers (pydantic_function_tool) over calling to_strict_json_schema directly
- Pass classes, not instances
When it happens
Trigger: Calling to_strict_json_schema(SomePlainClass), passing a BaseModel instance instead of the class, or passing a TypeAdapter under pydantic v1 (where the isinstance check is short-circuited by 'not PYDANTIC_V1').
Common situations: Direct use of the helper with unsupported types; pydantic v1 environments; passing a class that inherits from something BaseModel-like but is not a real BaseModel.
Related errors
- Expected {json_schema} to be a dictionary; path={path}
- Expected `$ref: {ref}` to resolved to a dictionary but got {
- Unexpected $ref format {ref!r}; Does not start with #/
- Pydantic models must subclass our base model type, e.g. `fro
- mode must be either 'json' or 'python'
AI-assisted analysis of openai/openai-python@9917c6e28e (2026-08-28).
Data as JSON: /api/errors/11ec3ef1a26a787b.
Report an issue: GitHub.