openai/openai-python · error · ValueError

context is only supported in Pydantic v2

Error message

context is only supported in Pydantic v2

What it means

The context serialization keyword (a Pydantic v2 feature for passing contextual data to custom serializers) has no equivalent in the Pydantic v1 compat shim; passing context=<anything but None> to model_dump raises ValueError.

Source

Thrown at src/openai/_models.py:355

                    `round_trip` parameter instead.
                round_trip: If True, dumped values should be valid as input for non-idempotent types such as Json[T].
                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

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Remove the context argument and pass the data explicitly to your serialization helpers
  2. Upgrade to pydantic>=2 to use context-aware serializers

Example fix

# before
d = obj.model_dump(context=ctx)
# after
d = obj.model_dump()
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Calling model.model_dump(context={'tenant': 'x'}) on SDK models when the Pydantic v1 implementation is active.

Common situations: Porting Pydantic v2 code that threads request context into serializers into an environment pinned to pydantic v1.

Related errors


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