fix: treat empty OPENAI_BASE_URL same as None for default fallback#2998
fix: treat empty OPENAI_BASE_URL same as None for default fallback#2998vivekvar-dl wants to merge 1 commit intoopenai:mainfrom
Conversation
Fixes openai#2927 When OPENAI_BASE_URL environment variable is set but empty (e.g., export OPENAI_BASE_URL=""), the client initialization fails with APIConnectionError because os.environ.get() returns an empty string, which is not None, preventing the fallback to the default API endpoint. This commit changes the second condition from 'if base_url is None' to 'if not base_url', which treats both None and empty string as falsy, allowing the fallback to https://api.openai.com/v1. Changes: - OpenAI (sync client): Line 163-167 - AsyncOpenAI (async client): Line 538-545 The fix is safe and pythonic: - None still falls through to fallback (unchanged behavior) - Empty string now also falls through (fixes the bug) - Non-empty strings are used as-is (unchanged behavior) - Explicit base_url parameter bypasses env var (unchanged behavior) Root cause: Empty env var creates empty string, not None, blocking the intended default URL fallback logic.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 17b4fdb3b7
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if not base_url: | ||
| base_url = f"https://api.openai.com/v1" |
There was a problem hiding this comment.
Don't override an explicitly empty
base_url
if not base_url broadens this fix from the empty OPENAI_BASE_URL env var to the explicit base_url argument as well. That changes constructor behavior for callers that intentionally pass base_url="" with a custom http_client/transport to keep requests relative: _prepare_url() in _base_client.py concatenates relative request paths with self.base_url, so this used to stay on the caller's transport, but after this change it silently falls back to https://api.openai.com/v1. Limiting the fallback to env-derived values would preserve that existing override semantics.
Useful? React with 👍 / 👎.
Fix: Empty OPENAI_BASE_URL prevents fallback to default endpoint
Fixes #2927
Problem
When
OPENAI_BASE_URLis set but empty, client initialization fails withAPIConnectionError:Root cause:
os.environ.get("OPENAI_BASE_URL")returns""(empty string), notNone. The current logic only checksif base_url is None, so the empty string bypasses the fallback:Solution
Change second condition from
if base_url is Nonetoif not base_url:Changes
File:
src/openai/_client.pyBoth instances updated identically.
Safety
✅ Pythonic:
not base_urlis idiomatic for "None or empty"✅ Backward compatible:
base_url=None→ still falls through ✅OPENAI_BASE_URLnot set → still falls through ✅OPENAI_BASE_URL="https://custom"→ still used ✅base_url="https://custom"(explicit) → still used ✅✅ Better UX: Empty string would cause connection error anyway; falling back to default is more helpful
Testing
Before:
After:
Edge Cases Handled
OPENAI_BASE_URLunsetOPENAI_BASE_URL=""OPENAI_BASE_URL="https://custom"base_url="https://custom"(param)Why This Matters
export VAR=""instead ofunset VARRelated
Checklist: