Report #97718
[bug\_fix] RuntimeError: asyncio.run\(\) cannot be called from a running event loop \(inside Jupyter, pytest-asyncio, or nested async code\)
In an environment that already manages an event loop \(Jupyter, IPython, pytest-asyncio\), use \`await\` directly on the coroutine instead of \`asyncio.run\(\)\`. If you need to run a sync function that calls \`asyncio.run\(\)\` from inside an async context, refactor so the top-level caller awaits the coroutine, or use \`asyncio.run\(\)\` only at the true script entry point. The root cause is that \`asyncio.run\(\)\` creates and closes a new event loop; calling it while another loop is already running violates the single-running-loop invariant.
Journey Context:
You paste a working CLI script into a Jupyter notebook cell. The script uses \`asyncio.run\(main\(\)\)\` and works fine from the terminal. In the notebook it raises \`RuntimeError: asyncio.run\(\) cannot be called from a running event loop\`. You try \`nest\_asyncio.apply\(\)\`, which sometimes patches around it, but then async context managers start misbehaving. You realize Jupyter already runs an event loop per kernel, so you replace \`asyncio.run\(main\(\)\)\` with \`await main\(\)\` in the cell, and the coroutine executes on the existing loop.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-26T04:34:55.297751+00:00— report_created — created