SyntaxError: 'await' outside function (tested)

SyntaxError: 'await' outside function (or outside async function) means await was used somewhere that isn't an async def. We measured each variant on Python 3.13.5, the fix, and the tempting non-fix.

1. await at the top level of a script

import asyncio

await asyncio.sleep(0)

Output:

  File "/tmp/aw/b1.py", line 3
    await asyncio.sleep(0)
    ^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'await' outside function

Measured on Python 3.13.5: a plain script can't use await outside a function. (Jupyter and python3 -m asyncio allow top-level await; we piped the same await asyncio.sleep(0, result=...) into python3 -m asyncio and it printed the result. A .py file run normally does not.)

2. await inside a normal def

import asyncio

def fetch():
    await asyncio.sleep(0)
    return "data"

Output:

  File "/tmp/aw/b2.py", line 4
    await asyncio.sleep(0)
    ^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'await' outside async function

Measured: inside a regular def the wording changes to 'await' outside async function. The function needs to be async def, and then its callers must await it too.

3. Lambdas can't be async; comprehensions can

import asyncio

async def main():
    data = [await asyncio.sleep(0, result=i) for i in range(3)]
    print(data)
    squares = lambda x: await asyncio.sleep(0, result=x * x)

Output:

  File "/tmp/aw/b3.py", line 6
    squares = lambda x: await asyncio.sleep(0, result=x * x)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'await' outside async function

Measured: the error points at the lambda on line 6, not the list comprehension on line 4. Inside an async def, await in a comprehension is allowed; a lambda is always a sync function, so await in it never works. Use a small async def instead.

4. Fix: put the code in async def main() and call asyncio.run()

import asyncio

async def fetch():
    await asyncio.sleep(0)
    return "data"

async def main():
    print(await fetch())

asyncio.run(main())

Output:

data

asyncio.run() is the one bridge from sync code into async code: call it once at the entry point, and use await everywhere inside.

5. Don't “fix” it by deleting await

import asyncio

async def fetch():
    await asyncio.sleep(0)
    return "data"

result = fetch()          # "fixing" the SyntaxError by dropping await
print(result == "data", type(result).__name__)

Output:

False coroutine
<sys>:0: RuntimeWarning: coroutine 'fetch' was never awaited

Measured: the SyntaxError goes away, but calling an async def without await doesn't run it. You get a coroutine object (not "data") and a RuntimeWarning: coroutine 'fetch' was never awaited at exit, which is easy to miss.

Frequently asked questions

How do I fix SyntaxError: 'await' outside function?

Move the code into async def main() and run it with asyncio.run(main()). We measured that working on Python 3.13.5.

What's the difference between 'await' outside function and 'await' outside async function?

The first is await at module level; the second is await inside a regular def. We measured both messages; the fix for the second is making that def async.

Can I use await in a lambda?

No. Lambdas are always synchronous; we measured SyntaxError: 'await' outside async function. Await in a list comprehension inside an async def is fine.

What happens if I call an async function without await?

It doesn't run. We measured it returning a coroutine object instead of the value, plus RuntimeWarning: coroutine was never awaited.

Written with AI assistance. Measured on 2026-10-09 with Python 3.13.5 on Linux: top-level await in a script and in python3 -m asyncio, await in a regular def, in a lambda and in a comprehension inside async def, the asyncio.run fix, and calling an async function without await. Every block was run verbatim and its output pasted from that run. PlainHeaders is our extension; everything else here is built-in Python.