Skip to content

Empty OPENAI_BASE_URL prevents fallback to default API endpoint #2927

@thelyad

Description

@thelyad

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: (

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 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

  1. export OPENAI_BASE_URL=""
  2. run the following:
from openai import OpenAI

client = OpenAI()
client.models.list()
  1. Trigger an "openai.APIConnectionError: Connection error" error

Code snippets

OS

macOS

Python version

Python 3.12.2

Library version

2.15.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions