asyncio.run() cannot be called from a running event loop #10

Closed
opened 2026-02-15 16:27:26 -05:00 by yindo · 1 comment
Owner

Originally created by @chenxizhang on GitHub (Jan 27, 2024).

I am using python 3.11.7 and the latest version of the Ollama pythons SDKs to run the sample code about AsyncClient

from ollama import AsyncClient
import asyncio

async def chat():
    message ={ "role":"user","content":"why people smile?"}
    response = await AsyncClient().chat(model="llama2",message=[message])

asyncio.run(chat())

I got an error as below.

RuntimeError Traceback (most recent call last)
Cell In[7], line 8
5 message ={ "role":"user","content":"why people smile?"}
6 response = await AsyncClient().chat(model="llama2",message=[message])
----> 8 asyncio.run(chat())

File ~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:186, in run(main, debug)
161 """Execute the coroutine and return the result.
162
163 This function runs the passed coroutine, taking care of
(...)
182 asyncio.run(main())
183 """
184 if events._get_running_loop() is not None:
185 # fail fast with short traceback
--> 186 raise RuntimeError(
187 "asyncio.run() cannot be called from a running event loop")
189 with Runner(debug=debug) as runner:
190 return runner.run(main)

RuntimeError: asyncio.run() cannot be called from a running event loop

Originally created by @chenxizhang on GitHub (Jan 27, 2024). I am using python 3.11.7 and the latest version of the Ollama pythons SDKs to run the sample code about AsyncClient ``` from ollama import AsyncClient import asyncio async def chat(): message ={ "role":"user","content":"why people smile?"} response = await AsyncClient().chat(model="llama2",message=[message]) asyncio.run(chat()) ``` I got an error as below. RuntimeError Traceback (most recent call last) Cell In[7], [line 8](vscode-notebook-cell:?execution_count=7&line=8) [5](vscode-notebook-cell:?execution_count=7&line=5) message ={ "role":"user","content":"why people smile?"} [6](vscode-notebook-cell:?execution_count=7&line=6) response = await AsyncClient().chat(model="llama2",message=[message]) ----> [8](vscode-notebook-cell:?execution_count=7&line=8) asyncio.run(chat()) File [~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:186](https://file+.vscode-resource.vscode-cdn.net/Users/areschen/tedia/ollama/~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:186), in run(main, debug) [161](https://file+.vscode-resource.vscode-cdn.net/Users/areschen/tedia/ollama/~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:161) """Execute the coroutine and return the result. [162](https://file+.vscode-resource.vscode-cdn.net/Users/areschen/tedia/ollama/~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:162) [163](https://file+.vscode-resource.vscode-cdn.net/Users/areschen/tedia/ollama/~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:163) This function runs the passed coroutine, taking care of (...) [182](https://file+.vscode-resource.vscode-cdn.net/Users/areschen/tedia/ollama/~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:182) asyncio.run(main()) [183](https://file+.vscode-resource.vscode-cdn.net/Users/areschen/tedia/ollama/~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:183) """ [184](https://file+.vscode-resource.vscode-cdn.net/Users/areschen/tedia/ollama/~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:184) if events._get_running_loop() is not None: [185](https://file+.vscode-resource.vscode-cdn.net/Users/areschen/tedia/ollama/~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:185) # fail fast with short traceback --> [186](https://file+.vscode-resource.vscode-cdn.net/Users/areschen/tedia/ollama/~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:186) raise RuntimeError( [187](https://file+.vscode-resource.vscode-cdn.net/Users/areschen/tedia/ollama/~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:187) "asyncio.run() cannot be called from a running event loop") [189](https://file+.vscode-resource.vscode-cdn.net/Users/areschen/tedia/ollama/~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:189) with Runner(debug=debug) as runner: [190](https://file+.vscode-resource.vscode-cdn.net/Users/areschen/tedia/ollama/~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:190) return runner.run(main) RuntimeError: asyncio.run() cannot be called from a running event loop
yindo closed this issue 2026-02-15 16:27:26 -05:00
Author
Owner

@chenxizhang commented on GitHub (Jan 27, 2024):

fixed it, and added some sample code in this pr #36

@chenxizhang commented on GitHub (Jan 27, 2024): fixed it, and added some sample code in this pr #36
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#10