Skip to content

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

Open
rmotgi1227 wants to merge 1 commit into
openai:mainfrom
rmotgi1227:fix/empty-base-url-env-fallback
Open

fix(client): treat empty OPENAI_BASE_URL env var as unset#3275
rmotgi1227 wants to merge 1 commit into
openai:mainfrom
rmotgi1227:fix/empty-base-url-env-fallback

Conversation

@rmotgi1227
Copy link
Copy Markdown

Fixes #2927.

Summary

When OPENAI_BASE_URL is set to an empty string in the environment, os.environ.get("OPENAI_BASE_URL") returns "". The subsequent if base_url is None check is skipped, so base_url = "" is passed to httpx — which raises APIConnectionError: Connection error before any network request is made.

Root cause

if base_url is None:
    base_url = os.environ.get("OPENAI_BASE_URL")
if base_url is None:          # ← empty string is not None, falls through
    base_url = "https://api.openai.com/v1"

Fix

Change is Nonenot base_url so both None and "" fall back to the default:

if not base_url:
    base_url = "https://api.openai.com/v1"

Applied to both OpenAI.__init__ (line 214) and AsyncOpenAI.__init__ (line 720).

os.environ.get("OPENAI_BASE_URL") returns "" when the variable is set
but blank. The subsequent 'if base_url is None' check then skips the
default fallback, passing an empty string to httpx which raises
APIConnectionError. Change to 'if not base_url' to treat both None
and empty string as absent, falling back to the default API endpoint.

Fixes openai#2927
@rmotgi1227 rmotgi1227 requested a review from a team as a code owner May 19, 2026 18:11
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