openai/openai-python · error · ValueError

exclude_computed_fields is only supported in Pydantic v2

Error message

exclude_computed_fields is only supported in Pydantic v2

What it means

exclude_computed_fields (a Pydantic v2 model_dump option to omit computed fields) is not available in the Pydantic v1 compat shim; passing anything other than False raises ValueError.

Source

Thrown at src/openai/_models.py:361

                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,
            *,
            indent: int | None = None,
            ensure_ascii: bool = False,
            include: IncEx | None = None,

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Upgrade to pydantic>=2
  2. Under v1, exclude the fields manually: {k: v for k, v in obj.model_dump().items() if k not in computed_names}

Example fix

# before
d = obj.model_dump(exclude_computed_fields=True)
# after
d = {k: v for k, v in obj.model_dump().items() if k not in {'computed_field'}}
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Calling model.model_dump(exclude_computed_fields=True) on SDK models when the Pydantic v1 implementation is active.

Common situations: Trying to strip computed/derived fields before logging or storing payloads, using Pydantic v2-style calls in a v1 environment.

Related errors


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