Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
xuing opened this issue Dec 1, 2023 · 3 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@xuing
Copy link

xuing commented Dec 1, 2023

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

@xuing xuing added the bug Something isn't working label Dec 1, 2023
@tomoish
Copy link
Contributor

tomoish commented Dec 1, 2023

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.

@RobertCraigie
Copy link
Collaborator

RobertCraigie commented Dec 1, 2023

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
Copy link
Collaborator

This will be fixed in the next release! #919

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants