{"record":{"id":"10f7ea50cb85bffc","repo":"can1357/oh-my-pi","slug":"top-level-await-is-not-supported-from-synchronous","errorCode":null,"errorMessage":"top-level await is not supported from synchronous magic execution","messagePattern":"top-level await is not supported from synchronous magic execution","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/eval/py/runner.py","lineNumber":1063,"sourceCode":"\n_install_builtins(_STATE.user_ns)\n\n\n# ---------------------------------------------------------------------------\n# Source execution (split last expression for rich display)\n# ---------------------------------------------------------------------------\n\n\n_TLA_FLAG = getattr(ast, \"PyCF_ALLOW_TOP_LEVEL_AWAIT\", 0x2000)\n\n\ndef _await_sync(coro) -> Any:\n    try:\n        running_loop = asyncio.get_running_loop()\n    except RuntimeError:\n        running_loop = None\n    if running_loop is not None and running_loop.is_running():\n        raise RuntimeError(\n            \"top-level await is not supported from synchronous magic execution\"\n        )\n    return asyncio.run(coro)\n\n\ndef _run_compiled_sync(code, ns: dict, *, want_value: bool) -> Any:\n    \"\"\"Synchronous execution path used by nested magic helpers.\"\"\"\n    if code.co_flags & inspect.CO_COROUTINE:\n        result = _await_sync(eval(code, ns))\n        return result if want_value else None\n    if want_value:\n        return eval(code, ns)\n    exec(code, ns)\n    return None\n\n\nasync def _run_compiled_async(code, ns: dict, *, want_value: bool) -> Any:\n    \"\"\"Execute a code object in the persistent event loop.","sourceCodeStart":1045,"sourceCodeEnd":1081,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/eval/py/runner.py#L1045-L1081","documentation":"_await_sync drains a coroutine with asyncio.run for synchronous magic execution, but if called from inside a running event loop it refuses — asyncio.run cannot nest. It raises RuntimeError: top-level await is not supported from synchronous magic execution.","triggerScenarios":"Calling a magic (or sync-exec path) whose result is a coroutine while already inside a running loop — e.g. invoking %run / a magic from async cell code, or from async magic execution where a sync path is taken with a coroutine argument.","commonSituations":"Using await from within a sync magic, mixing sync and async exec paths, or an async function's coroutine reaching _run_compiled_sync via a magic.","solutions":["Use the async execution path for code containing top-level await","Inside a running loop, await the coroutine directly instead of routing through the sync magic path","Avoid calling sync-only magics from async contexts; rewrite the cell as async","If you have a coroutine in sync code outside a loop, plain asyncio.run works — the error only fires inside a loop"],"exampleFix":"// before (inside async cell)\nresult = %run fetch_data.py  # magic returns coroutine, sync path -> error\n// after\nresult = await run_async_flow()","handlingStrategy":"try-catch","validationCode":"try:\n    asyncio.get_running_loop()\n    in_loop = True\nexcept RuntimeError:\n    in_loop = False\n# if in_loop, avoid sync magic paths with coroutines; await directly","typeGuard":null,"tryCatchPattern":"try:\n    result = run_magic_sync(...)\nexcept RuntimeError as e:\n    if \"top-level await\" in str(e):\n        result = await run_magic_async(...)  # switch to async path","preventionTips":["Inside async cells, await coroutines directly","Do not call sync-only magics from within a running loop","Keep async code on the async exec path end-to-end"],"tags":["python","asyncio","runtimeerror"],"backgroundTag":"asyncio-run-inside-running-loop","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}