Skip to content

fix: handle empty OPENAI_BASE_URL env var by falling back to default#2939

Open
giulio-leone wants to merge 1 commit intoopenai:mainfrom
giulio-leone:fix/empty-base-url-fallback
Open

fix: handle empty OPENAI_BASE_URL env var by falling back to default#2939
giulio-leone wants to merge 1 commit intoopenai:mainfrom
giulio-leone:fix/empty-base-url-fallback

Conversation

@giulio-leone
Copy link

Summary

Fixes #2927 — When OPENAI_BASE_URL is set to an empty string (e.g., export OPENAI_BASE_URL=""), the client fails with APIConnectionError instead of falling back to https://api.openai.com/v1.

Root Cause

if base_url is None:
    base_url = os.environ.get("OPENAI_BASE_URL")  # returns "" not None
if base_url is None:  # "" is not None → fallback skipped
    base_url = f"https://api.openai.com/v1"

os.environ.get("OPENAI_BASE_URL") returns "" when the variable is set but empty. Since "" is not None, the default URL fallback is skipped.

Fix

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

or None coerces empty strings (and other falsy values) to None, allowing the default fallback to activate. Applied to both OpenAI and AsyncOpenAI constructors.

Tests

Added 2 new test cases (test_base_url_env_empty_string) verifying both sync and async clients fall back to the default URL when OPENAI_BASE_URL="". All existing test_base_url_env tests continue to pass.

When `OPENAI_BASE_URL` is set to an empty string, `os.environ.get()`
returns `""` instead of `None`, bypassing the default URL fallback.
This causes an `APIConnectionError` since an empty string is not a
valid base URL.

Apply `or None` to coerce empty strings to `None`, allowing the
fallback to `https://api.openai.com/v1` to activate correctly.

Fixes openai#2927

Signed-off-by: Giulio Leone <6887247+giulio-leone@users.noreply.github.com>
@giulio-leone
Copy link
Author

Friendly ping — CI is green, tests pass, rebased on latest. Ready for review whenever convenient. Happy to address any feedback. 🙏

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