-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Description
Confirm this is an issue with the Python library and not an underlying OpenAI API
- This is an issue with the Python library
Describe the bug
model_dump() in openai/_compat.py passes by_alias=None to pydantic's model_dump(), which causes pydantic-core's Rust serializer to raise TypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool'.
This only triggers when the openai logger has level DEBUG enabled, because the affected model_dump() call is inside a if log.isEnabledFor(logging.DEBUG) block in _base_client.py:_build_request() (line 488).
The function signature in _compat.py:134 declares by_alias: bool | None = None, and passes it directly to pydantic without coercing to bool. Pydantic v2's Rust-based serializer does not accept None for by_alias.
To Reproduce
- Enable DEBUG logging:
logging.basicConfig(level=logging.DEBUG) - Make any API call via the openai client (sync or async)
- The
_build_request()method enters the debug logging block and callsmodel_dump(options, exclude_unset=True)without passingby_alias, so it defaults toNone - pydantic-core raises:
TypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool'
Code snippets
import logging
# Enable DEBUG on the openai logger — this is the trigger
logging.basicConfig(level=logging.DEBUG)
from openai import AzureOpenAI # also reproduces with OpenAI()
client = AzureOpenAI(
azure_endpoint="https://example.openai.azure.com",
api_key="fake-key",
api_version="2025-01-01-preview",
)
# Any API call will crash before even reaching the network
client.chat.completions.create(
model="gpt-4.1",
messages=[{"role": "user", "content": "hello"}],
)
# TypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool'OS
Linux (Debian-based Docker container, also reproducible on macOS)
Python version
Python v3.12.5
Library version
openai v2.24.0, pydantic v2.10.2, pydantic-core v2.27.1