Skip to content

Empty OPENAI_BASE_URL prevents fallback to default API endpoint #2927

Open

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

Activity

  1. added
    bug
    on Mar 3, 2026
  2. added a commit that references this issue on Mar 4, 2026
    563e8ae
  3. added a commit that references this issue on Mar 7, 2026
    0998d8f
  4. xXMrNidaXx commented on Mar 8, 2026

    @xXMrNidaXx

    Good catch on the empty string edge case. The current 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'

    ...uses is None for both checks, but os.environ.get() returns an empty string '' (not None) when the variable is set but empty. Empty string is falsy but not None, so the fallback never triggers.

    The fix should be:

    if base_url is None:
        base_url = os.environ.get('OPENAI_BASE_URL') or None  # converts '' to None
    if base_url is None:
        base_url = 'https://api.openai.com/v1'

    Or equivalently:

    base_url = base_url or os.environ.get('OPENAI_BASE_URL') or 'https://api.openai.com/v1'

    Workaround: Unset the environment variable entirely (unset OPENAI_BASE_URL or del os.environ['OPENAI_BASE_URL']) rather than setting it to an empty string. The bug only triggers when OPENAI_BASE_URL is defined but empty — if it's not set at all, os.environ.get() returns None and the fallback works correctly.

  5. giulio-leone commented on Mar 9, 2026

    @giulio-leone

    This is addressed by #2939 — when OPENAI_BASE_URL is set to an empty string, it now falls back to the default API endpoint instead of constructing an invalid URL.

  6. added a commit that references this issue on Mar 10, 2026
    18fe6fa
  7. mvanhorn commented on Mar 10, 2026

    @mvanhorn

    I've submitted a fix for this in PR #2952. Uses or None to normalize empty env var strings, applied to both sync and async constructors.

  8. passionworkeer commented on Mar 14, 2026

    @passionworkeer

    This is a simple fix! I can submit a PR to handle the empty string case for OPENAI_BASE_URL.

  9. added 2 commits that reference this issue on Mar 15, 2026
    38e4fa2
    416ce55
  10. theamodhshetty commented on Mar 19, 2026

    @theamodhshetty

    Opened #2988 for this.

    The change only touches the environment fallback path: if OPENAI_BASE_URL is set to an empty string, the client now treats it the same as an unset value and falls back to the default API base URL. Explicit base_url arguments are left alone. I also added sync and async regressions for the empty-env case.

  11. added a commit that references this issue on Mar 20, 2026
    3717560
  12. added a commit that references this issue on Mar 21, 2026
    17b4fdb
  13. added a commit that references this issue on Mar 22, 2026
    221da82
  14. added a commit that references this issue on Mar 24, 2026
    c499207
  15. khalidsaidi commented on Mar 25, 2026

    @khalidsaidi

    A2ABench has an accepted answer for this imported thread.

  16. added a commit that references this issue on Apr 2, 2026
    c408851
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions