Skip to content

fix: treat empty OPENAI_BASE_URL env var as unset#2936

Open
atian8179 wants to merge 1 commit intoopenai:mainfrom
atian8179:fix/empty-base-url-env-fallback
Open

fix: treat empty OPENAI_BASE_URL env var as unset#2936
atian8179 wants to merge 1 commit intoopenai:mainfrom
atian8179:fix/empty-base-url-env-fallback

Conversation

@atian8179
Copy link

Problem

When OPENAI_BASE_URL is set to an empty string (export OPENAI_BASE_URL=""), the client fails with APIConnectionError instead of falling back to the default endpoint.

Root Cause

os.environ.get("OPENAI_BASE_URL") returns "" (not None) for empty env vars. The subsequent if base_url is None check doesn't trigger, so the default https://api.openai.com/v1 is never assigned.

Fix

Add or None to coerce empty strings to None:

base_url = os.environ.get("OPENAI_BASE_URL") or None

Applied to both OpenAI and AsyncOpenAI client constructors.

Fixes #2927

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
@atian8179 atian8179 requested a review from a team as a code owner March 7, 2026 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty OPENAI_BASE_URL prevents fallback to default API endpoint

1 participant