Skip to content

Bug: by_alias: NoneType in openai-python v2.24.0 when logging.DEBUG is enabled #2921

@GuillermoGaete

Description

@GuillermoGaete

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

  1. Enable DEBUG logging: logging.basicConfig(level=logging.DEBUG)
  2. Make any API call via the openai client (sync or async)
  3. 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
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions