Empty OPENAI_BASE_URL prevents fallback to default API endpoint
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
Research direction
Start in src/openai/_client.py at the base_url initialization logic linked in the issue, then reproduce the behavior with OPENAI_BASE_URL set to an empty string and client.models.list(). Done means the client uses the documented default endpoint instead of producing a connection error in this environment.
Written by the indexing model from the issue text.
Description
Confirm this is an issue with the Python library and not an underlying OpenAI API
- This is an issue with the Python library
Describe the bug
I noticed a small issue in the base_url initialization logic: (https://github.com/openai/openai-python/blob/656e3cab4a18262a49b961d41293367e45ee71b9/src/openai/_client.py#L163-L166)
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 the environment variable OPENAI_BASE_URL is defined but empty (e.g., OPENAI_BASE_URL=""), then os.environ.get("OPENAI_BASE_URL") returns an empty string. As a result:
- base_url becomes ""
- the second condition (if base_url is None) is never triggered
- the intended fallback "https://api.openai.com/v1" is skipped
An empty string is not a valid base URL, so this can lead to confusing behavior.
To Reproduce
export OPENAI_BASE_URL=""- run the following:
from openai import OpenAI
client = OpenAI()
client.models.list()
- Trigger an "openai.APIConnectionError: Connection error" error
Code snippets
OS
macOS
Python version
Python 3.12.2
Library version
2.15.0
- Dominant language
- Python
- Stars
- 31.7k
- Forks
- 5.8k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 96
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from openai/openai-python
-
v4
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
openai/openai-python#3837 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
openai/openai-python#3755 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
openai/openai-python#3556 · 3 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
openai/openai-python#3402 · 2 comments ·
-
upstream
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
openai/openai-python#3294 · 2 comments ·
All issues in openai/openai-python
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100