Skip to content

base_url of OpenAI Client Instance Cannot Be Modified Due to Missing Update in Setter Method #913

Closed
@xuing

Description

@xuing

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 encountered an issue where the base_url of an OpenAI client instance couldn't be modified. This is due to the setter method of base_url not updating self._base_url.

Possible Solution

I suggest modifying the setter method for base_url to ensure self._base_url is also updated. Additionally, incorporate handling for the trailing slash of the URL, as done in the init method:

@base_url.setter
def base_url(self, url: URL | str) -> None:
    url = url if isinstance(url, URL) else URL(url)
    url = self._enforce_trailing_slash(url)
    self._base_url = url
    self._client.base_url = url

To Reproduce

  1. Set up an OpenAI client instance with initial parameters including base_url.
  2. Attempt to modifybase_url.
  3. Observe that self._base_url within the client instance remains unchanged, indicating that the modification has not been applied.

image

Code snippets

import os

from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),
    base_url="https://api.openai.com/v1",
)
print(client.base_url)
client.base_url = "https://newurl.example.com/v1"
print(client.base_url)

OS

Windows11

Python version

Python 3.12.0

Library version

1.3.6

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