Skip to content

fix(client): treat empty OPENAI_BASE_URL as unset and fall back to default#2980

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

fix(client): treat empty OPENAI_BASE_URL as unset and fall back to default#2980
NIK-TIGER-BILL wants to merge 1 commit intoopenai:mainfrom
NIK-TIGER-BILL:fix/empty-openai-base-url-env-fallback

Conversation

@NIK-TIGER-BILL
Copy link

Problem

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. As a result, the second if base_url is None check never fires, the default https://api.openai.com/v1 fallback is skipped, and the client tries to connect to an empty URL, producing a confusing APIConnectionError.

# Before (broken with OPENAI_BASE_URL="")
if base_url is None:
    base_url = os.environ.get("OPENAI_BASE_URL")  # returns ""
if base_url is None:          # "" is not None — this branch is skipped!
    base_url = "https://api.openai.com/v1"

Solution

Convert the empty string to None using or None, so the fallback fires correctly:

if base_url is None:
    base_url = os.environ.get("OPENAI_BASE_URL") or None
if base_url is None:
    base_url = "https://api.openai.com/v1"

Applied to both the sync OpenAI client and the async AsyncOpenAI client.

Testing

  • Syntax verified with Python AST parser
  • Reproducer: export OPENAI_BASE_URL="" && python -c "from openai import OpenAI; OpenAI().models.list()" — previously raised APIConnectionError, now falls back to default endpoint

Fixes #2927

…fault

When OPENAI_BASE_URL is set to an empty string, os.environ.get() returns
"" which is falsy but not None, so the second `if base_url is None`
check never fires and the default API URL is never used.

Using `or None` converts empty strings to None, allowing the fallback
to proceed correctly.

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