["python","pydantic"]
How to parse list of models with Pydantic

I use Pydantic to model the requests and responses to an API. I defined a User class: from pydantic import BaseModel class User(BaseModel): name: str age: int My API returns a list of users which I retrieve with requests and convert into a dict: users = [{"name": "user1", "age": 15}, {"name": "user2", "age": 28}] How can I convert this dict to a list of User instances? My solution for now is user_list = [] for user in users: user_list.append(User(**user))
Pydantic V1: This is now possible using parse_obj_as. from pydantic import parse_obj_as users = [ {"name": "user1", "age": 15}, {"name": "user2", "age": 28} ] m = parse_obj_as(List[User], users) Pydantic V2: Use Type Adapter. from pydantic import TypeAdapter users = [ {"name": "user1", "age": 15}, {"name": "user2", "age": 28} ] ta = TypeAdapter(List[User]) m = ta.validate_python(users)