Skip to content

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

Open
@se-ok

Description

@se-ok

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions