Skip to content

Accept injected httpx2 clients - #1768

Draft
dsfaccini wants to merge 2 commits into
anthropics:mainfrom
dsfaccini:httpx2-support
Draft

Accept injected httpx2 clients#1768
dsfaccini wants to merge 2 commits into
anthropics:mainfrom
dsfaccini:httpx2-support

Conversation

@dsfaccini

Copy link
Copy Markdown

httpx2 is Pydantic's actively-maintained, API-compatible fork of httpx. Because it lives in a separate import namespace, httpx2.Client, httpx2.AsyncClient, httpx2.Response, etc. are distinct classes that fail isinstance(x, httpx.*) — so today Anthropic(http_client=httpx2.AsyncClient()) raises TypeError: Invalid http_client argument.

This is a minimal, opt-in demonstration that the SDK can accept an injected httpx2 client end-to-end — intended as a conversation starter for #1755 rather than a finished feature. src/anthropic/_base_client.py is Stainless-managed, so the durable fix belongs at the generator/template level; this PR just shows the shape of the change and that it works.

Behind a guarded import httpx2 (a complete no-op when httpx2 isn't installed):

  • _base_client.py — widen the two http_client isinstance gates (sync + async) to accept httpx2 clients (the TypeError message text is unchanged); coerce the request URL to str for httpx2 clients in _build_request (httpx2's build_request rejects a classic httpx.URL; classic httpx clients are untouched); widen the two except httpx.TimeoutException sites so httpx2 timeouts still map to APITimeoutError.
  • _response.py — widen the two except httpx.StreamConsumed sites so a double-read over an httpx2 client still raises the friendly StreamAlreadyConsumed.

Adds tests/test_httpx2_client.py (guarded by pytest.importorskip("httpx2")) covering client acceptance and an httpx2.MockTransport round-trip, plus httpx2 as a dev-only dependency (Python 3.10+). No user-facing extra, no wrapper classes, no docs/examples.

httpx2 (https://github.com/pydantic/httpx2) is an API-compatible fork of
httpx that lives in a separate import namespace, so httpx2.Client & co.
fail isinstance() against httpx.*, making Anthropic(http_client=...) reject
an injected httpx2 client with "Invalid `http_client` argument".

Accept httpx2 clients with a minimal, opt-in change in _base_client.py:

- guarded `import httpx2` plus module-level type tuples for the sync/async
  client classes and the timeout exceptions
- widen the two `http_client` isinstance gates to the client tuples
- coerce the request URL to `str` for httpx2 clients in `_build_request`,
  since httpx2's build_request rejects a classic httpx.URL (classic httpx
  clients are left untouched)
- widen the two `except httpx.TimeoutException` sites so httpx2 timeouts
  still map to APITimeoutError

Adds tests/test_httpx2_client.py (guarded by pytest.importorskip) covering
acceptance and a MockTransport round-trip, and httpx2 as a dev dependency
(Python 3.10+).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant