openai/openai-python · error · TypeError

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

Error message

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

What it means

parse_text (used by responses.parse) JSON-parses the model output into text_format. Dataclass-like types are handled with pydantic.TypeAdapter, which requires Pydantic v2; under Pydantic v1 a dataclass/TypedDict text_format raises this TypeError.

Source

Thrown at src/openai/lib/_parsing/_responses.py:153

    return construct_type_unchecked(
        type_=ParsedResponse[TextFormatT],
        value={
            **response.to_dict(),
            "output": output_list,
        },
    )


def parse_text(text: str, text_format: type[TextFormatT] | Omit) -> TextFormatT | None:
    if not is_given(text_format):
        return None

    if is_basemodel_type(text_format):
        return cast(TextFormatT, model_parse_json(text_format, text))

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

        return pydantic.TypeAdapter(text_format).validate_json(text)

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


def get_input_tool_by_name(*, input_tools: Iterable[ToolParam], name: str) -> FunctionToolParam | None:
    for tool in input_tools:
        if tool["type"] == "function" and tool.get("name") == name:
            return tool

    return None


def parse_function_tool_arguments(
    *,
    input_tools: Iterable[ToolParam] | Omit | None,
    function_call: ParsedResponseFunctionToolCall | ResponseFunctionToolCall,

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Upgrade to pydantic>=2
  2. Use a pydantic.BaseModel subclass instead of a dataclass/TypedDict

Example fix

# before
@dataclass
class Output: ...
client.responses.parse(..., text_format=Output)
# after
class Output(pydantic.BaseModel): ...
client.responses.parse(..., text_format=Output)
Defensive patterns

Strategy: validation

Validate before calling

import pydantic; assert int(pydantic.VERSION.split(".")[0]) >= 2 or is_basemodel_type(text_format), "pydantic v2 required for dataclass text_format"

Type guard

def responses_parse_safe(t: type) -> bool:
    return is_basemodel_type(t) or (not PYDANTIC_V1 and is_dataclass_like_type(t))

Try / catch

try:
    parsed = parse_response(response, text_format=Output)
except TypeError as e:
    raise RuntimeError(f"unsupported text_format: {e}") from e

Prevention

When it happens

Trigger: Using client.responses.parse(text_format=MyDataclass) with pydantic v1 installed.

Common situations: Environments pinned to pydantic 1.x by another dependency (e.g. older LangChain, FastAPI 0.x).

Related errors


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