Last modified: Apr 25, 2026 By Alexander Williams
Fix async_generator Not Iterable Error
Encountering a TypeError: 'async_generator' object is not iterable can be confusing. This error happens when you treat an async generator like a regular one. This article explains why it occurs and how to fix it.
Async generators use async for loops, not regular for loops. The error message is Python's way of telling you that your code is trying to iterate synchronously over an asynchronous object. Let's break it down.
What Is an Async Generator?
An async generator is a function defined with async def that contains yield. It produces values asynchronously. You must iterate it with async for inside an async function.
Regular generators use def and yield. They work with normal for loops. Async generators are different because they can await operations between yields.
# Example: This is an async generator function
async def count_up_to(n):
for i in range(n):
await asyncio.sleep(0.1) # Simulate async work
yield i
# This is a regular generator
def sync_count_up_to(n):
for i in range(n):
yield i
Why the Error Happens
The error occurs when you try to use an async generator in a regular for loop. Python sees the object is an async_generator and raises TypeError because it doesn't support synchronous iteration.
This often happens when you forget to use async for or call the generator outside an async context. Beginners frequently make this mistake when migrating from synchronous to asynchronous code.
import asyncio
async def fetch_data():
for i in range(3):
await asyncio.sleep(0.1)
yield i
# Wrong way: using regular for loop
# for value in fetch_data(): # This raises the error
# print(value)
# Correct way: using async for
async def main():
async for value in fetch_data():
print(value)
asyncio.run(main())
# Output when running the correct code
0
1
2
Common Scenarios
1. Using list() or tuple() on an Async Generator
Calling list() on an async generator triggers the error because list() uses synchronous iteration. You must collect items with async for or use asyncio.gather() if appropriate.
import asyncio
async def generate_numbers():
for i in range(5):
await asyncio.sleep(0.1)
yield i
async def main():
# Wrong: list() expects synchronous iteration
# numbers = list(generate_numbers()) # TypeError
# Correct: collect using async for
numbers = []
async for num in generate_numbers():
numbers.append(num)
print(numbers)
asyncio.run(main())
[0, 1, 2, 3, 4]
2. Passing Async Generator to Synchronous Functions
If you pass an async generator to a function that expects a regular iterable, you'll get this error. Always check if the function supports asynchronous iteration.
import asyncio
async def async_range(n):
for i in range(n):
await asyncio.sleep(0.01)
yield i
def process_items(items):
for item in items: # This expects sync iterable
print(item)
async def main():
# Wrong: process_items expects sync iterable
# process_items(async_range(3)) # TypeError
# Correct: iterate async first, then pass list
items = [item async for item in async_range(3)]
process_items(items)
asyncio.run(main())
0
1
2
How to Fix the Error
Here are the three main solutions:
Use async for instead of for. This is the most common fix. Always iterate async generators with async for inside an async function.
Convert to a list with an async comprehension. Use [item async for item in async_gen] to collect all items into a list. This works inside async functions.
Use asyncio.to_thread() if you must run sync code. For complex cases, you can run the async iteration in a separate thread, but this is rarely needed.
import asyncio
async def data_stream():
for i in range(3):
await asyncio.sleep(0.1)
yield f"data-{i}"
async def main():
# Method 1: async for loop
print("Method 1: async for")
async for item in data_stream():
print(item)
# Method 2: async list comprehension
print("\nMethod 2: async comprehension")
items = [item async for item in data_stream()]
print(items)
# Method 3: Using asyncio.to_thread (advanced)
print("\nMethod 3: to_thread")
def sync_collect():
# This runs in a thread with its own event loop
loop = asyncio.new_event_loop()
return loop.run_until_complete(
[item async for item in data_stream()]
)
result = await asyncio.to_thread(sync_collect)
print(result)
asyncio.run(main())
Method 1: async for
data-0
data-1
data-2
Method 2: async comprehension
['data-0', 'data-1', 'data-2']
Method 3: to_thread
['data-0', 'data-1', 'data-2']
Best Practices
Always check if a function returns an async generator. Read the documentation or inspect the return type. Use typing.AsyncGenerator for type hints.
Keep your async code in async functions. Avoid mixing sync and async iteration unless necessary. This prevents the TypeError and improves code clarity.
If you see this error frequently, review your Python TypeError: Causes and Fixes guide for broader understanding of type errors in Python.
Debugging Tips
When you encounter this error, check the line number. Look for the for loop or list() call that triggers it. Verify if the object is defined with async def and yield.
Use type() to inspect the object. Async generators show as . Regular generators show as . This quick check helps identify the issue.
import asyncio
async def async_gen():
yield 1
def sync_gen():
yield 1
print(type(async_gen())) #
print(type(sync_gen())) # Conclusion
The TypeError: 'async_generator' object is not iterable error is straightforward to fix. Remember to use async for instead of for when working with async generators. Always run async code with asyncio.run() or similar event loop runners.
Practice with the examples above to build confidence. As you work with more asynchronous Python code, this error will become easy to spot and resolve. For more help with Python errors, check our Python TypeError: Causes and Fixes article.
Stay consistent with async patterns. Use async comprehensions for collecting items. Avoid mixing sync and async iteration. These habits will save you from this error and make your code more robust.