RuntimeError: no running event loop
when using cl.make_async
alongside cl.instrument_openai()
#1868
Labels
RuntimeError: no running event loop
when using cl.make_async
alongside cl.instrument_openai()
#1868
Description
When
cl.instrument_openai()
is enabled, any OpenAI request goes through Chainlit’s instrumentation. However, if that request is made within a function wrapped bycl.make_async(...)
, the instrumentation eventually callsasyncio.create_task()
from a background thread (which lacks a running event loop). As a result, the following error occurs:Error:
RuntimeError: no running event loop
Steps to Reproduce
cl.instrument_openai()
.Chain
).cl.make_async(...)
, causing it to run in a worker thread.RuntimeError: no running event loop
.Expected Behavior
Calls to an OpenAI endpoint (instrumented by Chainlit) within a background thread should either succeed or bypass streaming instrumentation gracefully without causing a runtime error.
Actual Behavior
Chainlit’s instrumentation tries to schedule an async task (
asyncio.create_task(...)
) in a worker thread, where no event loop is running. This raisesRuntimeError: no running event loop
.Environment
I believe the root cause is that
anyio.to_thread.run_sync
does not share the main event loop with the worker thread. Meanwhile, Chainlit’s instrumentation tries to stream or log messages by callingasyncio.create_task
within that thread. This triggers theno running event loop
error.The text was updated successfully, but these errors were encountered: