fix: convert by_alias=None to bool for pydantic v2#2965
fix: convert by_alias=None to bool for pydantic v2#2965Jah-yee wants to merge 2 commits intoopenai:mainfrom
Conversation
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
There was a problem hiding this comment.
💡 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, |
There was a problem hiding this comment.
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 👍 / 👎.
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
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
When DEBUG logging is enabled,
model_dump()is called withby_alias=None(the default), which causes pydantic-core's Rust serializer to raise:This fix converts
NonetoTrue(pydantic's default behavior) before passing tomodel_dump()for pydantic v2.Root cause: The
_compat.pyfunction signature declaresby_alias: bool | None = None, but passes it directly to pydantic v2'smodel_dump()which expects a bool, not None.Fix: Convert
by_aliastoboolwhen passing to pydantic v2, defaulting toTrue(pydantic's default) whenNoneis passed.Fixes: #2921