Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions fix_base_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
"""Fix script for issue #2927: empty OPENAI_BASE_URL should fallback to default"""

file_path = "src/openai/_client.py"

with open(file_path, 'r') as f:
lines = f.readlines()

modified_lines = []
i = 0
while i < len(lines):
line = lines[i]

# Look for the pattern:
# if base_url is None:
# base_url = os.environ.get("OPENAI_BASE_URL")
# if base_url is None:
# base_url = f"https://api.openai.com/v1"

if 'if base_url is None:' in line and i+1 < len(lines):
next_line = lines[i+1]
if 'base_url = os.environ.get("OPENAI_BASE_URL")' in next_line:
# This is the first "if base_url is None"
# Keep it as is
modified_lines.append(line)
modified_lines.append(next_line)
i += 2

# Now check if the next line is the second "if base_url is None"
if i < len(lines) and 'if base_url is None:' in lines[i]:
# Replace this second check with "if not base_url:"
indent = lines[i][:lines[i].index('if')]
modified_lines.append(f"{indent}# Treat empty string same as None to allow fallback to default\n")
modified_lines.append(f"{indent}# Fixes #2927: export OPENAI_BASE_URL=\"\" should not prevent default URL\n")
modified_lines.append(f"{indent}if not base_url:\n")
i += 1
continue

modified_lines.append(line)
i += 1

with open(file_path, 'w') as f:
f.writelines(modified_lines)

print("✅ Fixed both occurrences (sync and async clients)")
8 changes: 6 additions & 2 deletions src/openai/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ def __init__(

if base_url is None:
base_url = os.environ.get("OPENAI_BASE_URL")
if base_url is None:
# Treat empty string same as None to allow fallback to default
# Fixes #2927: export OPENAI_BASE_URL="" should not prevent default URL
if not base_url:
base_url = f"https://api.openai.com/v1"
Comment on lines +167 to 168

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.


super().__init__(
Expand Down Expand Up @@ -537,7 +539,9 @@ def __init__(

if base_url is None:
base_url = os.environ.get("OPENAI_BASE_URL")
if base_url is None:
# Treat empty string same as None to allow fallback to default
# Fixes #2927: export OPENAI_BASE_URL="" should not prevent default URL
if not base_url:
base_url = f"https://api.openai.com/v1"

super().__init__(
Expand Down