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

nest_asyncio hangs AsyncAnthropic

Open sang-d opened this issue 1 year ago • 4 comments

When running with nest_asyncio.apply() the AsyncAnthropic execution keeps staying in the thread

import anthropic
import asyncio
import nest_asyncio

nest_asyncio.apply()

client = anthropic.AsyncAnthropic()

message = asyncio.run(
   client.messages.create(
       model="claude-3-5-sonnet-20240620",
       max_tokens=1000,
       temperature=0,
       system="You are a world-class poet. Respond only with short poems.",
       messages=[
           {
               "role": "user",
               "content": "Why is the ocean salty and the sun is so bright?",
           }
       ],
   )
)
print(message.content[0].text)

sang-d avatar Jul 17 '24 11:07 sang-d

Could you share any more details on what your use case is with nest_asyncio? It appears to be archived.

For what it's worth, we're not doing anything fancy with async on top of httpx / anyio so I suspect this issue would need to be resolved there first.

RobertCraigie avatar Sep 09 '24 09:09 RobertCraigie

Also running nest_asyncio.apply(), just updated from 0.28 to 0.34.1 and have hit this problem also.

joelpob avatar Sep 10 '24 20:09 joelpob

@joelpob would you be able to verify if nest_asyncio works with httpx? I suspect this issue lies within the http library we're using / one of it's transitive dependencies

RobertCraigie avatar Sep 11 '24 12:09 RobertCraigie

It appears to be archived.

The repo is archived because its owned passed away.

I am facing the same issue, and I am unable to reproduce it using HTTPX on its own

Edit: The issue is nest_asyncio with anyio, specifically run_sync_in_worker_thread. I think this issue might be related: https://github.com/agronholm/anyio/issues/418

omarhurani avatar Oct 13 '24 11:10 omarhurani

I suspect this will be fixed in the next release, you can try it out now with python -m pip install git+https://github.com/anthropics/anthropic-sdk-python@next. Please reopen if you're still running into this :)

RobertCraigie avatar Nov 19 '24 07:11 RobertCraigie