openai/openai-python · error · ValueError

serialize_as_any is only supported in Pydantic v2

Error message

serialize_as_any is only supported in Pydantic v2

What it means

serialize_as_any (Pydantic v2 duck-typing serialization, useful for serialized subclass fields declared with a base type) is not implemented in the Pydantic v1 compat shim; only the default False is accepted in model_dump.

Source

Thrown at src/openai/_models.py:357

                warnings: How to handle serialization errors. False/"none" ignores them, True/"warn" logs errors,
                    "error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
                fallback: A function to call when an unknown value is encountered. If not provided,
                    a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError] error is raised.
                serialize_as_any: Whether to serialize fields with duck-typing serialization behavior.

            Returns:
                A dictionary representation of the model.
            """
            if mode not in {"json", "python"}:
                raise ValueError("mode must be either 'json' or 'python'")
            if round_trip != False:
                raise ValueError("round_trip is only supported in Pydantic v2")
            if warnings != True:
                raise ValueError("warnings is only supported in Pydantic v2")
            if context is not None:
                raise ValueError("context is only supported in Pydantic v2")
            if serialize_as_any != False:
                raise ValueError("serialize_as_any is only supported in Pydantic v2")
            if fallback is not None:
                raise ValueError("fallback is only supported in Pydantic v2")
            if exclude_computed_fields != False:
                raise ValueError("exclude_computed_fields is only supported in Pydantic v2")
            dumped = super().dict(  # pyright: ignore[reportDeprecated]
                include=include,
                exclude=exclude,
                by_alias=by_alias if by_alias is not None else False,
                exclude_unset=exclude_unset,
                exclude_defaults=exclude_defaults,
                exclude_none=exclude_none,
            )

            return cast("dict[str, Any]", json_safe(dumped)) if mode == "json" else dumped

        @override
        def model_dump_json(
            self,

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Upgrade to pydantic>=2, where serialize_as_any=True works
  2. Under v1, serialize the object manually (e.g. via .dict() plus custom logic) or restructure the model to include the subclass fields

Example fix

# before
d = obj.model_dump(serialize_as_any=True)
# after (requires pydantic>=2)
# ensure openai uses pydantic v2, then:
d = obj.model_dump(serialize_as_any=True)
Defensive patterns

Strategy: validation

Validate before calling

import pydantic
if pydantic.VERSION.startswith('1.'):
    kwargs.pop('serialize_as_any', None)
obj.model_dump(**kwargs)

Prevention

When it happens

Trigger: Calling model.model_dump(serialize_as_any=True) on SDK models while running the Pydantic v1 implementation — typically to get subclass fields emitted for a field typed as the base model.

Common situations: Polymorphic response models where a field is typed as BaseModel but holds a subclass; code copied from Pydantic v2 docs into a pydantic v1 environment.

Related errors


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