Skip to content

fix(client): treat empty OPENAI_BASE_URL as unset, falling back to default endpoint#2997

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

fix(client): treat empty OPENAI_BASE_URL as unset, falling back to default endpoint#2997
NIK-TIGER-BILL wants to merge 1 commit intoopenai:mainfrom
NIK-TIGER-BILL:fix/empty-base-url-fallback

Conversation

@NIK-TIGER-BILL
Copy link

Description

When OPENAI_BASE_URL is set to an empty string (e.g. export OPENAI_BASE_URL=""), os.environ.get("OPENAI_BASE_URL") returns "" which is not None. This causes the second if base_url is None check to be skipped, so the empty string is passed as base_url to the HTTP client, resulting in confusing connection errors rather than falling back to the default endpoint.

Root Cause

# Before fix
if base_url is None:
    base_url = os.environ.get("OPENAI_BASE_URL")  # returns "" if set to empty
if base_url is None:                               # "" is not None, skipped!
    base_url = f"https://api.openai.com/v1"

Fix

# After fix
if base_url is None:
    base_url = os.environ.get("OPENAI_BASE_URL") or None  # "" becomes None
if base_url is None:
    base_url = f"https://api.openai.com/v1"               # fallback works correctly

This fix is applied to both OpenAI (sync) and AsyncOpenAI classes.

Changes

  • src/openai/_client.py: Use or None to coerce empty string env var to None in both sync and async client constructors

Closes #2927

…fault endpoint

When OPENAI_BASE_URL is set to an empty string, os.environ.get() returns ''
which is falsy but not None, causing the second None-check to be skipped.
This means an empty string is passed as base_url, leading to confusing errors.

Using  ensures that both unset
and empty string values are treated identically, falling back to the default
https://api.openai.com/v1 endpoint.

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