fix(provider): adapt anthropic source to httpx2 rename in anthropic 1.0.0 (#9791)

anthropic 1.0.0 renamed the bundled ``_base_client.httpx`` module to
``_base_client.httpx2``. The previous ``getattr(_base_client, "httpx",
httpx)`` fallback silently reverted to the global httpx import, which can
break the SDK's isinstance validation when a proxy client is used. Resolve
the SDK's own module in version-agnostic order (httpx → httpx2 → global)
and update the corresponding unit test to assert against the same
resolution.
This commit is contained in:
Ruochen Pan
2026-08-24 10:33:29 +08:00
committed by GitHub
parent 19d00fb1f0
commit c87b7733a5
2 changed files with 18 additions and 2 deletions
@@ -131,7 +131,14 @@ class ProviderAnthropic(Provider):
try:
from anthropic import _base_client as anthropic_base_client
httpx_module = getattr(anthropic_base_client, "httpx", httpx)
# anthropic <1.0.0 exposes the bundled httpx as ``_base_client.httpx``;
# 1.0.0+ renamed it to ``_base_client.httpx2``. Prefer the SDK's own
# module in either case and fall back to the global httpx import.
httpx_module = getattr(
anthropic_base_client,
"httpx",
getattr(anthropic_base_client, "httpx2", httpx),
)
except ImportError:
pass
return create_proxy_client(
+10 -1
View File
@@ -134,10 +134,19 @@ def test_create_http_client_uses_anthropic_httpx_module(monkeypatch):
from anthropic import _base_client as anthropic_base_client
# anthropic <1.0.0 exposes the bundled httpx as ``_base_client.httpx``;
# 1.0.0+ renamed it to ``_base_client.httpx2``. Resolve the SDK's own module
# the same way the production code does so the assertion stays version-agnostic.
expected_httpx_module = getattr(
anthropic_base_client,
"httpx",
getattr(anthropic_base_client, "httpx2", None),
)
assert captured["provider_label"] == "Anthropic"
assert captured["proxy"] == "http://127.0.0.1:7890"
assert captured["headers"] == {"X-Trace-Id": "trace-1"}
assert captured["httpx_module"] is anthropic_base_client.httpx
assert captured["httpx_module"] is expected_httpx_module
def test_create_http_client_falls_back_to_global_httpx_module(monkeypatch):