Encountering the syntaxerror: await outside function error messages are inevitable.
Sometimes, this error message it’s quite frustrating, especially, if you don’t know how to fix it.
Fortunately, in this article, we’ll explore how to fix this error, and you’ll know why await is only valid in async functions.
What is “await” keyword?
The “await” keyword is used in a type of programming called asynchronous programming. It’s used when we want different tasks to run at the same time, which helps the computer use its resources more efficiently.
But remember, you can only use the “await” keyword inside a special kind of function called an asynchronous function.
What is “syntaxerror: ‘await’ outside function” error message?
The error message syntaxerror: ‘await’ outside function occurs when we are trying to use an await keyword outside of an async function or method.
Await is used in an async function or method to wait on other asynchronous tasks.
For example:
import asyncio
async def my_async_function():
await asyncio.sleep(1)
return "Hello, Welcome to Itsourcecode!"
result = await my_async_function()
print(result)Output:
File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 7
result = await my_async_function()
^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'await' outside function
Therefore, if you are calling the “await” keyword outside of an async function or method, SyntaxError will arise.
So, in simple words, this error typically occurs when the await keyword is used outside of an asynchronous function.
Why does the “syntaxerror: await outside function” occur?
This error message occurs due to several reasons, such as:
This error message occurs due to several reasons, such as:
❌Using await in a Synchronous Function
When using await, it is essential to remember that it can only be used within asynchronous functions. If you mistakenly use await in a synchronous function, the interpreter will raise SyntaxError.
❌ Misplaced await Statement
Placing the await keyword outside of any function. In Python, every awaits statement should be enclosed within an asynchronous function.
❌ Using await in a Regular Block
Await keyword cannot be used in regular blocks such as if statements or loops. The error will be raised if you try to use it in such contexts.
Why can I only use the await keyword inside of async function?
The reason you can only use the await keyword inside of an async function because it ensures proper flow control and synchronization between asynchronous tasks.
If you mark a function as async and use the await keyword within it, you indicate to the interpreter that the function contains asynchronous operations and should be treated accordingly.
How to fix the “syntaxerror: await outside function”?
To fix the syntaxerror ‘await’ outside function, ensure that the await keyword is used inside an async function or method.
If you are calling an async function or method outside of an async function or method, you need to use the asyncio.run() method to call the async function or method.
Use the asyncio.run() method to call the async function or method
Incorrect code:
❌ import asyncio
async def my_async_function():
await asyncio.sleep(1)
return "Hello, Welcome to Itsourcecode!"
result = await my_async_function()
print(result)Corrected code:
✅ import asyncio
async def my_async_function():
await asyncio.sleep(1)
return "Hello, Welcome to Itsourcecode!"
result = asyncio.run(my_async_function())
print(result)Output:
Hello, Welcome to Itsourcecode!Conclusion
In conclusion, the error message syntaxerror: ‘await’ outside function occurs when we are trying to use an await keyword outside of an async function or method.
Await is used in an async function or method to wait on other asynchronous tasks.
To fix this error, ensure that the await keyword is used inside an async function or method.
This article already discussed what this error is all about and multiple ways to resolve this error.
By executing the solutions above, you can master this SyntaxError with the help of this guide.
You could also check out other SyntaxError articles that may help you in the future if you encounter them.
- Pip install syntaxerror: invalid syntax
- Syntaxerror: f-string expression part cannot include a backslash
- Syntaxerror: cannot assign to literal here in Python
We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊
Why async/await SyntaxError happens
async and await are context-sensitive keywords. Using await outside an async function, or async syntax in Python 3.4 or older, raises SyntaxError.
Common triggers
- await outside async def.
await coro()at module level or in a sync function fails. - Python too old.
async defadded in 3.5;async for/within 3.6. - yield inside async.
yieldin async def was allowed in 3.6+ for async generators. - await in generator. Regular generators cannot use await.
- Missing asyncio.run(). To start an async program, wrap top-level await in
asyncio.run(coro()).
Diagnostic pattern
# BAD — await at module level
await fetch() # SyntaxError: 'await' outside async function
# GOOD — wrap in asyncio.run
import asyncio
async def main():
return await fetch()
asyncio.run(main())
# BAD — yield in async without generator context
async def gen():
yield 1 # ok in Py3.6+ for async generators
result = await another() # ok
return result # SyntaxError: async generator cannot return value
# GOOD — separate concerns
async def worker():
return await another() # ok — regular async function
Best practices
- Use Python 3.11+. Big improvements to asyncio and TaskGroup.
- Wrap top-level await with asyncio.run. Standard entry point.
- Use TaskGroup (3.11+) for structured concurrency.
- Consider anyio for cross-runtime async code (asyncio + trio).
Official documentation
Frequently Asked Questions
What is Python SyntaxError and what causes it?
SyntaxError is raised when Python’s parser can’t understand your code. Common causes: missing colon (def foo() instead of def foo():), unmatched parentheses or brackets, incorrect indentation, mixing tabs and spaces, missing comma in a list, or using a Python 3 feature in a Python 2 interpreter (or vice versa). The error points to a line, but the actual issue is often on the line BEFORE.
How do I fix ‘unexpected EOF while parsing’?
Python ran out of code while expecting more, usually unclosed parenthesis, bracket, brace, or quote. Scroll up from the error line and count opening vs closing pairs. Use an IDE with bracket matching (VS Code, PyCharm) to highlight pairs. Common gotcha: a triple-quoted string that’s missing its closing triple-quote.
Why does my syntax error point to a line that looks correct?
The actual error is often on the line BEFORE the one Python reports. Python reads code until something doesn’t parse, then reports the position where it gave up, not where the user’s mistake started. Check the line above the reported error for missing colons, commas, or closing brackets.
Why does ‘print x’ raise SyntaxError in Python 3?
Python 3 made print a function, so print(x) is required. The bare ‘print x’ syntax was Python 2 only. If you’re following an old tutorial, check whether it targets Python 2 (released 2008) or Python 3 (your current interpreter). Update all print statements to print(…) function calls.
Where can I find more SyntaxError fixes?
Browse the SyntaxError reference hub for 48+ specific fixes (Python and JavaScript). For Python fundamentals see the Python Tutorial hub. For JavaScript syntax errors see the JavaScript Tutorial hub.
