anthropic-sdk-python icon indicating copy to clipboard operation
anthropic-sdk-python copied to clipboard

httpx.PoolTimeout, fixed by disabling HTTP keepalive

Open charles-dyfis-net opened this issue 1 year ago • 4 comments

This is a deeply odd error, insofar as it's something that happens 100% of the time only when my code is invoking Anthropic (via client libraries 0.28.0) from our CI environment (hosted in AWS us-east-2).

We get a anthropic.APITimeoutError: Request timed out, thrown from a httpx.PoolTimeout, thrown from a httpcore.PoolTimeout during a generally unremarkable test case. The following change in usage/invocation prevents it entirely:

- async_client = AsyncAnthropic()
+ http_client = httpx.AsyncClient(
+     limits=httpx.Limits(max_connections=None, max_keepalive_connections=0)
+ )
+ async_client = AsyncAnthropic(http_client=http_client)

charles-dyfis-net avatar Jun 25 '24 16:06 charles-dyfis-net

What version of httpx are you using?

If you're not on the latest I would strongly recommend upgrading, there were some issues with older versions of httpx not closing connections properly.

RobertCraigie avatar Jun 25 '24 16:06 RobertCraigie

Indeed, this was observed when behind a few releases (httpx 0.25.2) -- I'll retest on 0.27.0 and see if that resolves the issue.

charles-dyfis-net avatar Jun 25 '24 17:06 charles-dyfis-net

Unfortunately, the issue still reproduces with httpx 0.27.0.

charles-dyfis-net avatar Jun 25 '24 17:06 charles-dyfis-net

Ah! Updating httpx alone didn't solve the problem.

I had a dependency pinning anyio, which prevented httpcore from being updated. Updating that pin to permit a modern anyio permits newer httpcore to be pulled in, and that resolves the problem.

Perhaps anthropic-sdk-python should explicitly specify appropriate minimum versions of both?

charles-dyfis-net avatar Jun 25 '24 18:06 charles-dyfis-net

Glad this problem was fixed for you! We don't want to pin when we can avoid it, as it can cause worse problems for people in some situations.

rattrayalex avatar Jul 07 '24 22:07 rattrayalex