Skip to content

Commit

Permalink
fix(client): correct base_url setter implementation (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Dec 1, 2023
1 parent 1bfc69b commit 29d0c8b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/anthropic/_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 @@ -454,6 +454,14 @@ class Model(BaseModel):
assert isinstance(response, Model)
assert response.foo == 2

def test_base_url_setter(self) -> None:
client = Anthropic(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(ANTHROPIC_BASE_URL="http://localhost:5000/from/env"):
client = Anthropic(api_key=api_key, _strict_response_validation=True)
Expand Down Expand Up @@ -1194,6 +1202,16 @@ class Model(BaseModel):
assert isinstance(response, Model)
assert response.foo == 2

def test_base_url_setter(self) -> None:
client = AsyncAnthropic(
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(ANTHROPIC_BASE_URL="http://localhost:5000/from/env"):
client = AsyncAnthropic(api_key=api_key, _strict_response_validation=True)
Expand Down

0 comments on commit 29d0c8b

Please sign in to comment.