Accept injected httpx2 clients - #1768
Draft
dsfaccini wants to merge 2 commits into
Draft
Conversation
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+).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
httpx2is Pydantic's actively-maintained, API-compatible fork ofhttpx. Because it lives in a separate import namespace,httpx2.Client,httpx2.AsyncClient,httpx2.Response, etc. are distinct classes that failisinstance(x, httpx.*)— so todayAnthropic(http_client=httpx2.AsyncClient())raisesTypeError: Invalidhttp_clientargument.This is a minimal, opt-in demonstration that the SDK can accept an injected
httpx2client end-to-end — intended as a conversation starter for #1755 rather than a finished feature.src/anthropic/_base_client.pyis 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 twohttp_clientisinstancegates (sync + async) to accept httpx2 clients (theTypeErrormessage text is unchanged); coerce the request URL tostrfor httpx2 clients in_build_request(httpx2'sbuild_requestrejects a classichttpx.URL; classic httpx clients are untouched); widen the twoexcept httpx.TimeoutExceptionsites so httpx2 timeouts still map toAPITimeoutError._response.py— widen the twoexcept httpx.StreamConsumedsites so a double-read over an httpx2 client still raises the friendlyStreamAlreadyConsumed.Adds
tests/test_httpx2_client.py(guarded bypytest.importorskip("httpx2")) covering client acceptance and anhttpx2.MockTransportround-trip, plushttpx2as a dev-only dependency (Python 3.10+). No user-facing extra, no wrapper classes, no docs/examples.