fix: coerce by_alias to bool in model_dump for pydantic v2 compatibility#2937
Open
atian8179 wants to merge 2 commits intoopenai:mainfrom
Open
fix: coerce by_alias to bool in model_dump for pydantic v2 compatibility#2937atian8179 wants to merge 2 commits intoopenai:mainfrom
atian8179 wants to merge 2 commits intoopenai:mainfrom
Conversation
When OPENAI_BASE_URL is set to an empty string, os.environ.get() returns '' instead of None, bypassing the fallback to the default API endpoint. This causes APIConnectionError. Add 'or None' to coerce empty strings to None so the default https://api.openai.com/v1 fallback is reached. Applied to both sync (OpenAI) and async (AsyncOpenAI) clients. Fixes openai#2927
Pydantic v2's Rust-based serializer rejects None for by_alias, raising TypeError. The model_dump() wrapper passes by_alias=None directly to pydantic v2, which crashes when DEBUG logging is enabled (triggering the model_dump call in _build_request). Coerce by_alias to bool before passing to pydantic v2, matching the existing behavior in the pydantic v1 fallback path. Fixes openai#2921
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Enabling DEBUG logging causes
TypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool'on any API call.Root Cause
model_dump()in_compat.pydeclaresby_alias: bool | None = Noneand passes it directly to pydantic v2'smodel_dump(). Pydantic v2's Rust serializer doesn't acceptNoneforby_alias.The pydantic v1 fallback path already coerces with
bool(by_alias)— the v2 path was missing this.Fix
Fixes #2921