Skip to content

fix(compat): handle None by_alias in model_dump for Pydantic v2#2953

Open
mvanhorn wants to merge 1 commit intoopenai:mainfrom
mvanhorn:osc/2921-by-alias-nonetype-fix
Open

fix(compat): handle None by_alias in model_dump for Pydantic v2#2953
mvanhorn wants to merge 1 commit intoopenai:mainfrom
mvanhorn:osc/2921-by-alias-nonetype-fix

Conversation

@mvanhorn
Copy link

Fixes #2921

Problem

model_dump() in _compat.py passes by_alias=None to Pydantic v2's model_dump(), which requires a bool. The Rust serializer raises:

TypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool'

This triggers when the openai logger has DEBUG level enabled, because the affected model_dump() call is inside a log.isEnabledFor(logging.DEBUG) block.

Fix

Convert by_alias from None to False before passing to Pydantic v2's model_dump(), matching the pattern already used in the Pydantic v1 fallback path (line 157: by_alias=bool(by_alias)).

Tests

Added tests/compat/test_model_dump.py with tests for by_alias=None, True, and False.

This contribution was developed with AI assistance (Claude Code).

Pydantic v2's Rust serializer requires by_alias to be a bool, not
None. The v1 fallback path already did bool(by_alias), but the v2
path passed None directly, causing TypeError when DEBUG logging was
enabled.

Fixes openai#2921

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant