Last modified: Dec 06, 2025 By Alexander Williams
Fix TypeError: 'coroutine' object is not iterable
Python's async features are powerful. But they can cause confusing errors. One common error is 'coroutine' object is not iterable.
This error happens when you treat an async function incorrectly. You try to iterate over it like a list. But it is a coroutine object.
This guide explains the error. It shows you how to fix it. You will learn about async/await in Python.
Understanding the Error Message
The error is clear. Python tells you a coroutine is not iterable. Iterable objects can be looped over.
Examples include lists, tuples, and strings. A coroutine is not one of these. It is a special async object.
You get this error when using for, list(), or next() on a coroutine. You must await it first.
Root Cause: Forgetting the Await Keyword
The main cause is simple. You call an async function but forget to await it. An async function returns a coroutine.
This coroutine is a promise of a future result. It is not the result itself. You must use await to get the value.
Here is a code example that produces the error.
import asyncio
async def fetch_data():
await asyncio.sleep(1)
return [1, 2, 3]
# Wrong: Calling async function without await
coroutine = fetch_data()
for item in coroutine: # This line causes the TypeError
print(item)
TypeError: 'coroutine' object is not iterable
The variable coroutine is not a list. It is a coroutine object. You cannot loop over it directly.
Solution 1: Use Await in an Async Context
The primary fix is to use the await keyword. You must call it inside another async function. Then you can iterate.
Here is the corrected code.
import asyncio
async def fetch_data():
await asyncio.sleep(1)
return [1, 2, 3]
async def main():
# Correct: Await the coroutine to get the result
data = await fetch_data()
for item in data:
print(item)
# Run the async main function
asyncio.run(main())
1
2
3
Now it works. await fetch_data() returns the actual list. You can then iterate over the list.
Remember: await can only be used inside an async function. The top-level entry point is asyncio.run().
Solution 2: Use asyncio.gather for Multiple Coroutines
You often need to run many async tasks. You might try to put them in a list. Then you get the same error.
Use asyncio.gather to run them concurrently. It collects results from multiple coroutines.
import asyncio
async def get_number(num):
await asyncio.sleep(0.5)
return num
async def main():
# Creating a list of coroutine objects
tasks = [get_number(i) for i in range(3)]
# Wrong: Trying to iterate over coroutines
# for task in tasks:
# print(task) # Error!
# Correct: Use asyncio.gather to await all
results = await asyncio.gather(*tasks)
for result in results:
print(result)
asyncio.run(main())
0
1
2
asyncio.gather runs all coroutines. It waits for all to finish. It returns a list of results.
This is perfect for parallel operations. It is a common pattern in async Python code.
Solution 3: Ensure Your Function is Properly Defined
Sometimes the error is subtle. You might define a function without async. But you call it with await.
Or you add async but forget it returns a value. Double-check your function definitions.
This error is similar to other Python TypeErrors. Like Fix TypeError: 'module' object is not callable.
Both errors involve calling something incorrectly. Understanding object types is key.
Common Pitfalls and How to Avoid Them
Here are quick tips to avoid this error.
Always use await on async function calls. Do it inside an async context.
Use asyncio.run() to start your async program. It manages the event loop.
Remember that async def creates a coroutine function. It returns a coroutine when called.
If you see 'coroutine object', you likely need an await. This is a core async concept.
Other iterable errors exist too. Like Fix TypeError: 'set' object is not subscriptable.
Each error teaches you about Python's data types. Mastering them makes you a better coder.
Debugging with Type Checking
Use print(type(obj)) to debug. It shows if you have a coroutine.
This helps confirm the issue. You can see the object type before the error.
import asyncio
async def demo():
return "Hello"
coro = demo()
print(type(coro)) # Prints Seeing await. It is a simple but effective trick.
Similar debugging helps with Fix TypeError: 'method' object is not subscriptable.
Always check the type of your variables. It clarifies many Python errors.
Conclusion
The 'coroutine object is not iterable' error is common. It happens when you forget to await an async function.
The fix is straightforward. Use the await keyword inside an async function. Or use asyncio.gather for multiple tasks.
Understanding async/await is essential. It is a fundamental part of modern Python.
Remember to define functions correctly. Use async def for coroutines. Use await to get their results.
With this knowledge, you can fix this error quickly. You can write efficient, non-blocking Python code.