Skip to content

Commit

Permalink
fix(client): correct base_url setter implementation (#919)
Browse files Browse the repository at this point in the history
Co-Authored-By: tomoish <tomo.i.120608@gmail.com>
  • Loading branch information
stainless-bot and tomoish committed Dec 1, 2023
1 parent 4233bcd commit 135d9cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/openai/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ 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)
self._base_url = self._enforce_trailing_slash(url if isinstance(url, URL) else URL(url))

@lru_cache(maxsize=None)
def platform_headers(self) -> Dict[str, str]:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,14 @@ class Model(BaseModel):
assert isinstance(response, Model)
assert response.foo == 2

def test_base_url_setter(self) -> None:
client = OpenAI(base_url="https://example.com/from_init", api_key=api_key, _strict_response_validation=True)
assert client.base_url == "https://example.com/from_init/"

client.base_url = "https://example.com/from_setter" # type: ignore[assignment]

assert client.base_url == "https://example.com/from_setter/"

def test_base_url_env(self) -> None:
with update_env(OPENAI_BASE_URL="http://localhost:5000/from/env"):
client = OpenAI(api_key=api_key, _strict_response_validation=True)
Expand Down Expand Up @@ -1102,6 +1110,16 @@ class Model(BaseModel):
assert isinstance(response, Model)
assert response.foo == 2

def test_base_url_setter(self) -> None:
client = AsyncOpenAI(
base_url="https://example.com/from_init", api_key=api_key, _strict_response_validation=True
)
assert client.base_url == "https://example.com/from_init/"

client.base_url = "https://example.com/from_setter" # type: ignore[assignment]

assert client.base_url == "https://example.com/from_setter/"

def test_base_url_env(self) -> None:
with update_env(OPENAI_BASE_URL="http://localhost:5000/from/env"):
client = AsyncOpenAI(api_key=api_key, _strict_response_validation=True)
Expand Down

0 comments on commit 135d9cf

Please sign in to comment.