SyntaxError: await Outside Async Function

SyntaxError: 'await' outside function

Quick Answer

You are using await outside of an async function. Wrap your code in an async def function and run it with asyncio.run().

Why This Happens

The await keyword is only valid inside async def functions. Using it in regular functions or at module level (outside Jupyter) causes a SyntaxError.

The Problem

import asyncio
result = await asyncio.sleep(1)

The Fix

import asyncio

async def main():
    await asyncio.sleep(1)

asyncio.run(main())

Step-by-Step Fix

  1. 1

    Create an async function

    Move the await call inside an async def.

  2. 2

    Use asyncio.run()

    Call asyncio.run(main()) to execute.

  3. 3

    Use Jupyter for top-level await

    Jupyter supports top-level await directly.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free