advanced patterns When Serializing Complex JSON Objects with FastAPI and Pydantic
I'm stuck on something that should probably be simple. I'm working with an scenario when trying to serialize complex JSON objects in my FastAPI application using Pydantic. I have a model that includes a nested object and a list of objects, but when I try to return this data from my endpoint, I encounter the behavior: `ValueError: Invalid value for field 'items': expected List[Item]`. I suspect it might be related to how I'm defining my Pydantic models. Hereβs a simplified version of my code: ```python from typing import List from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float class Order(BaseModel): order_id: int items: List[Item] @app.post('/orders/') async def create_order(order: Order): return order ``` When I send a POST request to `/orders/` with the following JSON payload: ```json { "order_id": 123, "items": [ {"name": "Item 1", "price": 19.99}, {"name": "Item 2", "price": 29.99} ] } ``` I get the behavior mentioned above. I've tried modifying the `items` field to be `List[dict]`, but that didn't work either. The FastAPI version I'm using is 0.68.0 and Pydantic is 1.9.0. Could the scenario be with how FastAPI infers types from the request body, or is there something wrong with my data structure? Any guidance would be greatly appreciated! I recently upgraded to Python latest. What's the best practice here? The project is a service built with Python. Could this be a known issue?