Skip to content

Commit d0c928a

Browse files
chore(client): improve error message for invalid http_client argument (#1216)
1 parent 71236e0 commit d0c928a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Diff for: src/openai/_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,

Diff for: tests/test_client.py

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

295+
async def test_invalid_http_client(self) -> None:
296+
with pytest.raises(TypeError, match="Invalid `http_client` arg"):
297+
async with httpx.AsyncClient() as http_client:
298+
OpenAI(
299+
base_url=base_url,
300+
api_key=api_key,
301+
_strict_response_validation=True,
302+
http_client=cast(Any, http_client),
303+
)
304+
295305
def test_default_headers_option(self) -> None:
296306
client = OpenAI(
297307
base_url=base_url, api_key=api_key, _strict_response_validation=True, default_headers={"X-Foo": "bar"}
@@ -994,6 +1004,16 @@ async def test_http_client_timeout_option(self) -> None:
9941004
timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore
9951005
assert timeout == DEFAULT_TIMEOUT # our default
9961006

1007+
def test_invalid_http_client(self) -> None:
1008+
with pytest.raises(TypeError, match="Invalid `http_client` arg"):
1009+
with httpx.Client() as http_client:
1010+
AsyncOpenAI(
1011+
base_url=base_url,
1012+
api_key=api_key,
1013+
_strict_response_validation=True,
1014+
http_client=cast(Any, http_client),
1015+
)
1016+
9971017
def test_default_headers_option(self) -> None:
9981018
client = AsyncOpenAI(
9991019
base_url=base_url, api_key=api_key, _strict_response_validation=True, default_headers={"X-Foo": "bar"}

0 commit comments

Comments
 (0)