Skip to content

fix: convert by_alias=None to bool for pydantic v2#2965

Open
Jah-yee wants to merge 2 commits intoopenai:mainfrom
Jah-yee:fix/by_alias_none_type_error
Open

fix: convert by_alias=None to bool for pydantic v2#2965
Jah-yee wants to merge 2 commits intoopenai:mainfrom
Jah-yee:fix/by_alias_none_type_error

Conversation

@Jah-yee
Copy link

@Jah-yee Jah-yee commented Mar 13, 2026

When DEBUG logging is enabled, model_dump() is called with by_alias=None (the default), which causes pydantic-core's Rust serializer to raise:

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

This fix converts None to True (pydantic's default behavior) before passing to model_dump() for pydantic v2.

Root cause: The _compat.py function signature declares by_alias: bool | None = None, but passes it directly to pydantic v2's model_dump() which expects a bool, not None.

Fix: Convert by_alias to bool when passing to pydantic v2, defaulting to True (pydantic's default) when None is passed.

Fixes: #2921

SparkLabScout and others added 2 commits March 11, 2026 16:38
When OPENAI_BASE_URL is set to an empty string, the SDK should
fall back to the default API endpoint instead of using the empty
string as the base URL.

Fixes issue openai#2927
When DEBUG logging is enabled, model_dump() is called with by_alias=None
(the default), which causes pydantic-core's Rust serializer to raise:
  TypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool'

This fix converts None to True (pydantic's default behavior) before
passing to model_dump() for pydantic v2.

Fixes: openai#2921
@Jah-yee Jah-yee requested a review from a team as a code owner March 13, 2026 02:37
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f8384f3e38

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

# warnings are not supported in Pydantic v1
warnings=True if PYDANTIC_V1 else warnings,
by_alias=by_alias,
by_alias=bool(by_alias) if by_alias is not None else True,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep by_alias default false in compat model_dump

Converting by_alias=None to True here changes the meaning of every model_dump(...) call site that omits by_alias (for example _utils/_transform.py and debug serialization in _base_client.py), because pydantic v2 now always serializes aliases instead of field names; this diverges from the v1 branch in the same function (bool(by_alias) => False for None) and from the SDK’s own BaseModel.model_dump() default behavior. For models with aliased fields, this silently changes emitted keys and can break request/data transformations that expect Python field names.

Useful? React with 👍 / 👎.

Jah-yee pushed a commit to Jah-yee/openai-python that referenced this pull request Mar 13, 2026
When DEBUG logging is enabled, model_dump() is called with by_alias=None
(the default), which causes pydantic-core's Rust serializer to raise:
TypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool'

This fix converts None to True (pydantic's default behavior) when passing
to model_dump() for pydantic v2.

Fixes: openai#2965
Jah-yee pushed a commit to Jah-yee/openai-python that referenced this pull request Mar 13, 2026
When DEBUG logging is enabled, model_dump() is called with by_alias=None,
which causes pydantic-core's Rust serializer to raise:
  TypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool'

This fix converts None to True (pydantic's default behavior) before passing
to model_dump() for pydantic v2.

Fixes: openai#2965
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