# Fix FastAPI Coroutine Object Is Not Iterable Error

> Resolve the coroutine object is not iterable TypeError in FastAPI by creating async-compatible decorators that properly handle await in route functions

**URL:** https://sentry.io/answers/fastapi-coroutine-object-is-not-iterable-error/

---

## The Problem

After I add the `async` keyword to a FastAPI function, calling the associated route produces the following error:

```
[TypeError("'coroutine' object is not iterable"), TypeError('vars() argument must have __dict__ attribute')]
```

Here's a simplified version of my application code:

```python
from fastapi import FastAPI
import asyncio

import job # internal module

app = FastAPI()

@app.get("/run-job")
@login_required
async def run_job():
    result = await job.run()
    return {"message": result }
```

I need to run the asynchronous method `job.run` in this endpoint's code, so the function itself has to be asynchronous. What is causing this error and how can I fix it?

## The Solution

The most likely cause of this error is the decorator `@login_required`, which we can assume returns a synchronous function. Even though `run_job` is asynchronous, the actual function that gets executed after the decorator is applied will be synchronous, leading to an error when `await` is called.

To fix this code, we will either need to alter `login_required` so that it returns an asynchronous function or create another decoration (e.g. `login_required_async`) that does so. For example:

```python
def login_required_async(f):
    async def wrapper(*args, **kwargs):
        # ... access control logic goes here ...
        return await f(*args, **kwargs)
    return wrapper
```

Depending on how the rest of our application functions, we may require both synchronous and asynchronous versions of `@login_required`. We can create one function for both using a conditional return that checks whether the passed-in function is synchronous or asynchronous:

```python
from inspect import iscoroutinefunction
from functools import wraps

def login_required(f):
    def access_control(*args, **kwargs):
        # ... access control logic goes here ...
        pass

    @wraps(f)
    def wrapper(*args, **kwargs):
        access_control(*args, **kwargs)
        return f(*args, **kwargs)

    @wraps(f)
    async def async_wrapper(*args, **kwargs):
        access_control(*args, **kwargs)
        return await f(*args, **kwargs)

    return async_wrapper if iscoroutinefunction(f) else wrapper
```

[`iscoroutinefunction`](https://docs.python.org/3/library/inspect.html#inspect.iscoroutinefunction) from Python's built-in `inspect` module will return `True` if the passed-in function is asynchronous and `False` if it is synchronous. This will allow us to use the `@login_required` decorator on synchronous and asynchronous functions alike.

---

*Source: [sentry.io/answers/fastapi-coroutine-object-is-not-iterable-error/](https://sentry.io/answers/fastapi-coroutine-object-is-not-iterable-error/)*
