Skip to content

fix(client): treat empty OPENAI_BASE_URL env var as unset#2976

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

fix(client): treat empty OPENAI_BASE_URL env var as unset#2976
NIK-TIGER-BILL wants to merge 1 commit intoopenai:mainfrom
NIK-TIGER-BILL:fix/empty-base-url-env-fallback

Conversation

@NIK-TIGER-BILL
Copy link

Problem

When OPENAI_BASE_URL is defined but empty (OPENAI_BASE_URL=""), os.environ.get("OPENAI_BASE_URL") returns an empty string. An empty string is falsy for == None but not for is None, so the second if base_url is None guard is never triggered and the intended fallback to https://api.openai.com/v1 is skipped.

The client then passes an empty string as base_url, which results in a confusing APIConnectionError: Connection error.

Fix

# Before
base_url = os.environ.get("OPENAI_BASE_URL")

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

Normalise an empty env var to None so the default URL is applied.

Same fix applied to both OpenAI and AsyncOpenAI client __init__ methods.

Fixes #2927

When OPENAI_BASE_URL is defined but empty (e.g. OPENAI_BASE_URL=''),
os.environ.get('OPENAI_BASE_URL') returns an empty string, which is
truthy enough to skip the None check. The intended fallback to
'https://api.openai.com/v1' is never reached, causing an
APIConnectionError with a confusing empty base URL.

Fix: use 'os.environ.get("OPENAI_BASE_URL") or None' so that an
empty string is normalised to None and the default URL is applied.

Fixes openai#2927
@NIK-TIGER-BILL NIK-TIGER-BILL requested a review from a team as a code owner March 15, 2026 07:16
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