Skip to content

fix(client): ensure setter method updates both _base_url and client.base_url #914

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/openai/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,10 @@ def base_url(self) -> URL:

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

@lru_cache(maxsize=None)
def platform_headers(self) -> Dict[str, str]:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,12 @@ def test_base_url_env(self) -> None:
client = AsyncOpenAI(api_key=api_key, _strict_response_validation=True)
assert client.base_url == "http://localhost:5000/from/env/"

def test_change_base_url(selr) -> None:
with update_env(OPENAI_BASE_URL="http://localhost:5000/from/env"):
client = AsyncOpenAI(api_key=api_key, _strict_response_validation=True)
client.base_url = "http://localhost:5000/custom/path/"
assert client.base_url == "http://localhost:5000/custom/path/"

@pytest.mark.parametrize(
"client",
[
Expand Down