openai/openai-python · error · TypeError

Non BaseModel types are only supported with Pydantic v2 - {r

Error message

Non BaseModel types are only supported with Pydantic v2 - {response_format}

What it means

maybe_parse_content tries to JSON-parse a completion into the given response_format. For dataclass-like (non-Pydantic-BaseModel) types it uses pydantic.TypeAdapter, which only exists in Pydantic v2; under Pydantic v1 it raises this TypeError.

Source

Thrown at src/openai/lib/_parsing/_completions.py:249

def is_parseable_tool(input_tool: ChatCompletionToolUnionParam) -> bool:
    if input_tool["type"] != "function":
        return False

    input_fn = cast(object, input_tool.get("function"))
    if isinstance(input_fn, PydanticFunctionTool):
        return True

    return cast(FunctionDefinition, input_fn).get("strict") or False


def _parse_content(response_format: type[ResponseFormatT], content: str) -> ResponseFormatT:
    if is_basemodel_type(response_format):
        return cast(ResponseFormatT, model_parse_json(response_format, content))

    if is_dataclass_like_type(response_format):
        if PYDANTIC_V1:
            raise TypeError(f"Non BaseModel types are only supported with Pydantic v2 - {response_format}")

        return pydantic.TypeAdapter(response_format).validate_json(content)

    raise TypeError(f"Unable to automatically parse response format type {response_format}")


def type_to_response_format_param(
    response_format: type | completion_create_params.ResponseFormat | Omit,
) -> ResponseFormatParam | Omit:
    if not is_given(response_format):
        return omit

    if is_response_format_param(response_format):
        return response_format

    # type checkers don't narrow the negation of a `TypeGuard` as it isn't
    # a safe default behaviour but we know that at this point the `response_format`
    # can only be a `type`

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Upgrade to pydantic v2 (pip install -U pydantic)
  2. Or use a pydantic.BaseModel subclass as response_format, which works on v1 too

Example fix

# before
@dataclass
class Output: ...
client.chat.completions.parse(..., response_format=Output)
# after
class Output(pydantic.BaseModel): ...
client.chat.completions.parse(..., response_format=Output)
Defensive patterns

Strategy: validation

Validate before calling

import pydantic
from openai.lib._pydantic import PYDANTIC_V1
if PYDANTIC_V1 and not is_basemodel_type(Output):
    raise RuntimeError("upgrade to pydantic v2 or use BaseModel")

Type guard

from pydantic import BaseModel
from openai._compat import is_basemodel_type
def parse_safe(t: type) -> bool:
    return is_basemodel_type(t) or (not PYDANTIC_V1 and is_dataclass_like_type(t))

Try / catch

try:
    parsed = maybe_parse_content(completion, Output)
except TypeError as e:
    raise ConfigurationError(str(e)) from e

Prevention

When it happens

Trigger: Passing a dataclass, TypedDict, or NamedTuple as response_format to chat.completions.parse while pydantic v1 is installed.

Common situations: Legacy projects pinned to pydantic<2; other dependencies forcing pydantic 1.x.

Related errors


AI-assisted analysis of openai/openai-python@9917c6e28e (2026-08-28). Data as JSON: /api/errors/1de479826d145a30. Report an issue: GitHub.