openai/openai-python · error · ValueError

ensure_ascii is only supported in Pydantic v2

Error message

ensure_ascii is only supported in Pydantic v2

What it means

ensure_ascii (a Pydantic v2 model_dump_json option controlling ASCII escaping) is not implemented in the Pydantic v1 compat shim; only the default False is accepted, so passing True raises ValueError.

Source

Thrown at src/openai/_models.py:421

                exclude_none: Whether to exclude fields that have a value of `None`.
                round_trip: Whether to use serialization/deserialization between JSON and class instance.
                warnings: Whether to show any warnings that occurred during serialization.

            Returns:
                A JSON string representation of the model.
            """
            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 ensure_ascii != False:
                raise ValueError("ensure_ascii is only supported in Pydantic v2")
            if exclude_computed_fields != False:
                raise ValueError("exclude_computed_fields is only supported in Pydantic v2")
            return super().json(  # type: ignore[reportDeprecated]
                indent=indent,
                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,
            )


class _EagerIterable(list[_T], Generic[_T]):
    """
    Accepts any Iterable[T] input (including generators), consumes it
    eagerly, and validates all items upfront.

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Upgrade to pydantic>=2 to use ensure_ascii=True
  2. Under v1, re-encode afterwards: json.dumps(json.loads(obj.model_dump_json()), ensure_ascii=True)

Example fix

# before
j = obj.model_dump_json(ensure_ascii=True)
# after
import json
j = json.dumps(json.loads(obj.model_dump_json()), ensure_ascii=True)
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Calling model.model_dump_json(ensure_ascii=True) on SDK models while the Pydantic v1 implementation is active, typically to force \uXXXX escaping of non-ASCII characters.

Common situations: Producing ASCII-safe JSON for legacy consumers or logs with Pydantic v2-style options in a v1 environment.

Related errors


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