-
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi @PeaceES So for Azure AI Agents with Streamlit’s synchronous runtime. The Here's a distilled pattern that’s been shown to work more reliably: Recommended Pattern: Isolated Event Loop Execution Use a dedicated event loop per async call, and catch loop closure errors gracefully. import asyncio
def run_async_task(coro, *args):
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(coro(*args))
except RuntimeError as e:
# Retry with fresh loop if closed
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(coro(*args))
finally:
loop.close()This avoids reusing a stale loop and sidesteps cleanup timing issues from Additional Tips
Here is example code for Agent Calling in Streamlit Please try the following def query_agent(agent_client, thread_id, message):
async def run():
await agent_client.create_message(thread_id, role="user", content=message)
return await agent_client.create_run(thread_id, agent_id)
return run_async_task(run)Some additional reading |
Beta Was this translation helpful? Give feedback.


Hi @PeaceES
So for Azure AI Agents with Streamlit’s synchronous runtime. The
event loop is closederror typically stems from Streamlit reusing or prematurely closing the loop across reruns or threaded callbacks.Here's a distilled pattern that’s been shown to work more reliably:
Recommended Pattern: Isolated Event Loop Execution
Use a dedicated event loop per async call, and catch loop closure errors gracefully.