From 0998d8ff90053c5e9529a9bc0a6824e571f626d3 Mon Sep 17 00:00:00 2001 From: atian8179 Date: Sat, 7 Mar 2026 22:58:23 +0800 Subject: [PATCH] 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"