openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

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

Open xuing opened this issue 3 years ago • 3 comments

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • [X] 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

xuing avatar Dec 01 '23 06:12 xuing

I may be able to fix it. I have confirmed that it works correctly using your Possible Solution. After writing a test, I will submit a pull request.

tomoish avatar Dec 01 '23 10:12 tomoish

Thanks for the report, while we fix this you should be able to use client.with_options() to set the base URL instead as a workaround.

RobertCraigie avatar Dec 01 '23 13:12 RobertCraigie

This will be fixed in the next release! https://github.com/openai/openai-python/pull/919

RobertCraigie avatar Dec 01 '23 18:12 RobertCraigie