Skip to content

anthropic.APIConnectionError: Connection error. Due to proxy env variables not getting applied to. #923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
se-ok opened this issue Apr 3, 2025 · 0 comments

Comments

@se-ok
Copy link

se-ok commented Apr 3, 2025

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

  1. give httpx client to anthropic.Anthropic or AsyncAnthropic
  2. 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.

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

No branches or pull requests

1 participant