# Fix Pydantic Value Is Not a Valid List in FastAPI

> Resolve the Pydantic validation error by aligning your FastAPI route return value with the response_model type, returning a list instead of a dictionary

**URL:** https://sentry.io/answers/fastapi-pydantic-error-value-is-not-a-valid-list/

---

## The Problem

One of the routes in my FastAPI project produces the following error whenever I call it:

```
Input should be a valid list [type=list_type, input_value={'posts': [...]}, input_type=dict]
```

Here is the FastAPI route function:

```python
@router.get('', response_model = List[schemas.PostResponse])
def get_posts(db : Session = Depends(get_db)):
    posts = db.query(models.Post).all()
    return {"posts" : posts}
```

Here is the definition of the `PostResponse` Pydantic model:

```python
from pydantic import BaseModel, ConfigDict
from datetime import datetime

class PostResponse(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    user_id: int
    post_id: str
    created_at: datetime
    user: UserResponse
```

What is causing the error and how can I fix it?

## The Solution

The [`response_model`](https://fastapi.tiangolo.com/tutorial/response-model/#response_model-parameter) keyword argument in `@router.get` indicates that the route function should return a value of type `List[schemas.PostResponse]`. However, the function's `return` statement returns a dictionary instead (`{"posts": posts}`). To fix this error, we must either change the function to return the expected type or change the `response_model` argument to reflect the function's return value.

Changing the return type:

```python
@router.get('', response_model = List[schemas.PostResponse])
def get_posts(db : Session = Depends(get_db)):
    posts = db.query(models.Post).all()
    return posts # <-- changed return value
```

Changing `response_model`:

```python
@router.get('', response_model = List[schemas.PostListResponse]) # <-- changed return type
def get_posts(db : Session = Depends(get_db)):
    posts = db.query(models.Post).all()
    return {"posts" : posts}
```

We must also create a `PostListResponse` Pydantic model to match our return value:

```python
class PostListResponse(BaseModel):
    posts: list[schemas.PostResponse]
```

---

*Source: [sentry.io/answers/fastapi-pydantic-error-value-is-not-a-valid-list/](https://sentry.io/answers/fastapi-pydantic-error-value-is-not-a-valid-list/)*
