anthropic-sdk-python
anthropic-sdk-python copied to clipboard
nest_asyncio hangs AsyncAnthropic
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)
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.
Also running nest_asyncio.apply(), just updated from 0.28 to 0.34.1 and have hit this problem also.
@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
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
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 :)