Skip to content

Commit 69cdfc3

Browse files
feat(client): add DefaultHttpxClient and DefaultAsyncHttpxClient (#1302)
1 parent 70eb081 commit 69cdfc3

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,12 @@ You can directly override the [httpx client](https://www.python-httpx.org/api/#c
549549
- Additional [advanced](https://www.python-httpx.org/advanced/#client-instances) functionality
550550

551551
```python
552-
import httpx
553-
from openai import OpenAI
552+
from openai import OpenAI, DefaultHttpxClient
554553

555554
client = OpenAI(
556555
# Or use the `OPENAI_BASE_URL` env var
557556
base_url="http://my.test.server.example.com:8083",
558-
http_client=httpx.Client(
557+
http_client=DefaultHttpxClient(
559558
proxies="http://my.test.proxy.example.com",
560559
transport=httpx.HTTPTransport(local_address="0.0.0.0"),
561560
),

src/openai/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
UnprocessableEntityError,
3030
APIResponseValidationError,
3131
)
32+
from ._base_client import DefaultHttpxClient, DefaultAsyncHttpxClient
3233
from ._utils._logs import setup_logging as _setup_logging
3334

3435
__all__ = [
@@ -67,6 +68,8 @@
6768
"DEFAULT_TIMEOUT",
6869
"DEFAULT_MAX_RETRIES",
6970
"DEFAULT_CONNECTION_LIMITS",
71+
"DefaultHttpxClient",
72+
"DefaultAsyncHttpxClient",
7073
]
7174

7275
from .lib import azure as _azure

src/openai/_base_client.py

+42-2
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,27 @@ def _idempotency_key(self) -> str:
716716
return f"stainless-python-retry-{uuid.uuid4()}"
717717

718718

719-
class SyncHttpxClientWrapper(httpx.Client):
719+
class _DefaultHttpxClient(httpx.Client):
720+
def __init__(self, **kwargs: Any) -> None:
721+
kwargs.setdefault("timeout", DEFAULT_TIMEOUT)
722+
kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS)
723+
kwargs.setdefault("follow_redirects", True)
724+
super().__init__(**kwargs)
725+
726+
727+
if TYPE_CHECKING:
728+
DefaultHttpxClient = httpx.Client
729+
"""An alias to `httpx.Client` that provides the same defaults that this SDK
730+
uses internally.
731+
732+
This is useful because overriding the `http_client` with your own instance of
733+
`httpx.Client` will result in httpx's defaults being used, not ours.
734+
"""
735+
else:
736+
DefaultHttpxClient = _DefaultHttpxClient
737+
738+
739+
class SyncHttpxClientWrapper(DefaultHttpxClient):
720740
def __del__(self) -> None:
721741
try:
722742
self.close()
@@ -1262,7 +1282,27 @@ def get_api_list(
12621282
return self._request_api_list(model, page, opts)
12631283

12641284

1265-
class AsyncHttpxClientWrapper(httpx.AsyncClient):
1285+
class _DefaultAsyncHttpxClient(httpx.AsyncClient):
1286+
def __init__(self, **kwargs: Any) -> None:
1287+
kwargs.setdefault("timeout", DEFAULT_TIMEOUT)
1288+
kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS)
1289+
kwargs.setdefault("follow_redirects", True)
1290+
super().__init__(**kwargs)
1291+
1292+
1293+
if TYPE_CHECKING:
1294+
DefaultAsyncHttpxClient = httpx.AsyncClient
1295+
"""An alias to `httpx.AsyncClient` that provides the same defaults that this SDK
1296+
uses internally.
1297+
1298+
This is useful because overriding the `http_client` with your own instance of
1299+
`httpx.AsyncClient` will result in httpx's defaults being used, not ours.
1300+
"""
1301+
else:
1302+
DefaultAsyncHttpxClient = _DefaultAsyncHttpxClient
1303+
1304+
1305+
class AsyncHttpxClientWrapper(DefaultAsyncHttpxClient):
12661306
def __del__(self) -> None:
12671307
try:
12681308
# TODO(someday): support non asyncio runtimes here

src/openai/_client.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ def __init__(
7474
max_retries: int = DEFAULT_MAX_RETRIES,
7575
default_headers: Mapping[str, str] | None = None,
7676
default_query: Mapping[str, object] | None = None,
77-
# Configure a custom httpx client. See the [httpx documentation](https://www.python-httpx.org/api/#client) for more details.
77+
# Configure a custom httpx client.
78+
# We provide a `DefaultHttpxClient` class that you can pass to retain the default values we use for `limits`, `timeout` & `follow_redirects`.
79+
# See the [httpx documentation](https://www.python-httpx.org/api/#client) for more details.
7880
http_client: httpx.Client | None = None,
7981
# Enable or disable schema validation for data returned by the API.
8082
# When enabled an error APIResponseValidationError is raised
@@ -272,7 +274,9 @@ def __init__(
272274
max_retries: int = DEFAULT_MAX_RETRIES,
273275
default_headers: Mapping[str, str] | None = None,
274276
default_query: Mapping[str, object] | None = None,
275-
# Configure a custom httpx client. See the [httpx documentation](https://www.python-httpx.org/api/#asyncclient) for more details.
277+
# Configure a custom httpx client.
278+
# We provide a `DefaultAsyncHttpxClient` class that you can pass to retain the default values we use for `limits`, `timeout` & `follow_redirects`.
279+
# See the [httpx documentation](https://www.python-httpx.org/api/#asyncclient) for more details.
276280
http_client: httpx.AsyncClient | None = None,
277281
# Enable or disable schema validation for data returned by the API.
278282
# When enabled an error APIResponseValidationError is raised

0 commit comments

Comments
 (0)