From bee7537f80a8c894193465e105fb741ce4d02caf Mon Sep 17 00:00:00 2001 From: NIK-TIGER-BILL Date: Sat, 21 Mar 2026 07:12:32 +0000 Subject: [PATCH] fix(compat): coerce by_alias=None to False in model_dump for pydantic v2 Fixes #2921 model_dump() in _compat.py declares by_alias as bool | None = None and was forwarding the value as-is to pydantic's model_dump(). pydantic-core's Rust-based serializer (pydantic v2) does not accept None for by_alias and raises: TypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool' This error only manifests when DEBUG-level logging is enabled on the openai logger, because the call that omits by_alias is inside a 'if log.isEnabledFor(logging.DEBUG)' block in _base_client.py. Fix: replace 'by_alias=by_alias' with an explicit coercion so that None becomes False (the natural default) instead of being forwarded to pydantic: by_alias=bool(by_alias) if by_alias is not None else False The pydantic v1 code path already used bool(by_alias), so this aligns both branches. --- src/openai/_compat.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/openai/_compat.py b/src/openai/_compat.py index 020ffeb2ca..f7d1d6ea9b 100644 --- a/src/openai/_compat.py +++ b/src/openai/_compat.py @@ -149,7 +149,8 @@ def model_dump( exclude_defaults=exclude_defaults, # warnings are not supported in Pydantic v1 warnings=True if PYDANTIC_V1 else warnings, - by_alias=by_alias, + # pydantic-core's Rust serializer rejects None for by_alias; coerce to bool. + by_alias=bool(by_alias) if by_alias is not None else False, ) return cast( "dict[str, Any]",