Confirm this is an issue with the Python library and not an underlying OpenAI API
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 calls model_dump(options, exclude_unset=True) without passing by_alias, so it defaults to None
- 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
Confirm this is an issue with the Python library and not an underlying OpenAI API
Describe the bug
model_dump()inopenai/_compat.pypassesby_alias=Noneto pydantic'smodel_dump(), which causes pydantic-core's Rust serializer to raiseTypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool'.This only triggers when the
openailogger has levelDEBUGenabled, because the affectedmodel_dump()call is inside aif log.isEnabledFor(logging.DEBUG)block in_base_client.py:_build_request()(line 488).The function signature in
_compat.py:134declaresby_alias: bool | None = None, and passes it directly to pydantic without coercing to bool. Pydantic v2's Rust-based serializer does not acceptNoneforby_alias.To Reproduce
logging.basicConfig(level=logging.DEBUG)_build_request()method enters the debug logging block and callsmodel_dump(options, exclude_unset=True)without passingby_alias, so it defaults toNoneTypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool'Code snippets
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