Skip to content

Commit 2cb3324

Browse files
committed
chore(client): improve error message for invalid http_client argument (anthropics#367)
1 parent 1f110a2 commit 2cb3324

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/anthropic/_base_client.py

+10
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,11 @@ def __init__(
780780
else:
781781
timeout = DEFAULT_TIMEOUT
782782

783+
if http_client is not None and not isinstance(http_client, httpx.Client): # pyright: ignore[reportUnnecessaryIsInstance]
784+
raise TypeError(
785+
f"Invalid `http_client` argument; Expected an instance of `httpx.Client` but got {type(http_client)}"
786+
)
787+
783788
super().__init__(
784789
version=version,
785790
limits=limits,
@@ -1322,6 +1327,11 @@ def __init__(
13221327
else:
13231328
timeout = DEFAULT_TIMEOUT
13241329

1330+
if http_client is not None and not isinstance(http_client, httpx.AsyncClient): # pyright: ignore[reportUnnecessaryIsInstance]
1331+
raise TypeError(
1332+
f"Invalid `http_client` argument; Expected an instance of `httpx.AsyncClient` but got {type(http_client)}"
1333+
)
1334+
13251335
super().__init__(
13261336
version=version,
13271337
base_url=base_url,

tests/test_client.py

+20
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,16 @@ def test_http_client_timeout_option(self) -> None:
300300
timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore
301301
assert timeout == DEFAULT_TIMEOUT # our default
302302

303+
async def test_invalid_http_client(self) -> None:
304+
with pytest.raises(TypeError, match="Invalid `http_client` arg"):
305+
async with httpx.AsyncClient() as http_client:
306+
Anthropic(
307+
base_url=base_url,
308+
api_key=api_key,
309+
_strict_response_validation=True,
310+
http_client=cast(Any, http_client),
311+
)
312+
303313
def test_default_headers_option(self) -> None:
304314
client = Anthropic(
305315
base_url=base_url, api_key=api_key, _strict_response_validation=True, default_headers={"X-Foo": "bar"}
@@ -1103,6 +1113,16 @@ async def test_http_client_timeout_option(self) -> None:
11031113
timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore
11041114
assert timeout == DEFAULT_TIMEOUT # our default
11051115

1116+
def test_invalid_http_client(self) -> None:
1117+
with pytest.raises(TypeError, match="Invalid `http_client` arg"):
1118+
with httpx.Client() as http_client:
1119+
AsyncAnthropic(
1120+
base_url=base_url,
1121+
api_key=api_key,
1122+
_strict_response_validation=True,
1123+
http_client=cast(Any, http_client),
1124+
)
1125+
11061126
def test_default_headers_option(self) -> None:
11071127
client = AsyncAnthropic(
11081128
base_url=base_url, api_key=api_key, _strict_response_validation=True, default_headers={"X-Foo": "bar"}

0 commit comments

Comments
 (0)