Open
Description
Hi,
With anthropic==0.49.0 and httpx==0.27.0, proxy settings in the env variables in the form of http_proxy
and https_proxy
are not properly applied to establish connections to anthropic, hence resulting in anthropic.APIConnectionError.
Currently anthropic._base_client._DefaultHttpxClient and _DefaultAsyncHttpxClient sets up a customized transport to initialize httpx client,
and given such, httpx does not respect proxy settings in the env.
Temporarily I can make it work with either
- give httpx client to anthropic.Anthropic or AsyncAnthropic
- add the following code to _DefaultHttpxClient or _DefaultAsyncHttpxClient
# https://github.com/anthropics/anthropic-sdk-python/blob/8b244157a7d03766bec645b0e1dc213c6d462165/src/anthropic/_base_client.py#L802
kwargs["transport"] = httpx.HTTPTransport(
# note: limits is always set above
limits=kwargs["limits"],
socket_options=socket_options,
)
proxy = {}
env = {k.lower(): v for k, v in os.environ.items()}
if 'http_proxy' in env:
proxy['http://'] = env['http_proxy']
if 'https_proxy' in env:
proxy['https://'] = env['https_proxy']
if proxy:
kwargs['proxy'] = proxy
But as a layman, I believe there must be more standardized way to handle proxy with httpx.
Although the issue might be related to recent changes to httpx, it would be the best if anthropic can respect the proxy settings in env out of the box by default.
Metadata
Metadata
Assignees
Labels
No labels