From 0998d8ff90053c5e9529a9bc0a6824e571f626d3 Mon Sep 17 00:00:00 2001 From: atian8179 Date: Sat, 7 Mar 2026 22:58:23 +0800 Subject: [PATCH 1/2] fix: treat empty OPENAI_BASE_URL env var as unset 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 #2927 --- src/openai/_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openai/_client.py b/src/openai/_client.py index aadf3601f2..04261feace 100644 --- a/src/openai/_client.py +++ b/src/openai/_client.py @@ -161,7 +161,7 @@ def __init__( self.websocket_base_url = websocket_base_url if base_url is None: - base_url = os.environ.get("OPENAI_BASE_URL") + base_url = os.environ.get("OPENAI_BASE_URL") or None if base_url is None: base_url = f"https://api.openai.com/v1" @@ -536,7 +536,7 @@ def __init__( self.websocket_base_url = websocket_base_url if base_url is None: - base_url = os.environ.get("OPENAI_BASE_URL") + base_url = os.environ.get("OPENAI_BASE_URL") or None if base_url is None: base_url = f"https://api.openai.com/v1" From 7c96139517a7117a57cdb6286f5ea8ddabb26042 Mon Sep 17 00:00:00 2001 From: atian8179 Date: Sat, 7 Mar 2026 22:59:45 +0800 Subject: [PATCH 2/2] fix: coerce by_alias to bool in model_dump for pydantic v2 compatibility 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 #2921 --- src/openai/_compat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openai/_compat.py b/src/openai/_compat.py index 020ffeb2ca..b4b05b171b 100644 --- a/src/openai/_compat.py +++ b/src/openai/_compat.py @@ -149,7 +149,7 @@ 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, + by_alias=bool(by_alias) if by_alias is not None else False, ) return cast( "dict[str, Any]",