Skip to content

fix: treat empty OPENAI_BASE_URL as unset#2928

Open
LuciferDono wants to merge 1 commit intoopenai:mainfrom
LuciferDono:LuciferDono/fix-empty-base-url-2927
Open

fix: treat empty OPENAI_BASE_URL as unset#2928
LuciferDono wants to merge 1 commit intoopenai:mainfrom
LuciferDono:LuciferDono/fix-empty-base-url-2927

Conversation

@LuciferDono
Copy link

Summary

When OPENAI_BASE_URL is set to an empty string (e.g. export OPENAI_BASE_URL=""), os.environ.get("OPENAI_BASE_URL") returns "" instead of None. The subsequent if base_url is None check passes, so the default https://api.openai.com/v1 fallback is never applied. This causes an APIConnectionError on the next API call.

Fixes #2927.

Change

Added or None to coerce empty strings to None in both the OpenAI and AsyncOpenAI constructors:

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

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

This is consistent with how an unset variable should behave — an empty string is not a valid base URL.

When OPENAI_BASE_URL is defined but empty, os.environ.get() returns ""
instead of None, so the fallback to https://api.openai.com/v1 is
skipped. Add `or None` to coerce empty strings to None in both the
sync and async client constructors.

Fixes openai#2927
@LuciferDono LuciferDono requested a review from a team as a code owner March 4, 2026 03:20
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