0

I want to pass in a list of the class declared in the FastAPI service, something like :

@router.post("/predict")
async def predict(json_list: List[my_class])-> dict:

And I got exception: 'my_class' object is not subscriptable

What is the correct way to pass in a list of class with FastAPI? TIA!

Frank
  • 156
  • 2
  • 6
  • Does [this](https://stackoverflow.com/questions/60844846/read-a-body-json-list-with-fastapi) answer your question? – M.O. Oct 13 '22 at 17:04
  • What is `my_class` defined as? Is it actual Pydantic class? On what line does the exception get raised? It seems like you're trying to access a list index on `my_class`, not on `json_list`? – MatsLindh Oct 13 '22 at 20:17
  • M.O. - Thanks for the link. In that way, I need to add many lists. Each for a data column. To make it simple, I even think using a str to pass in json as a str. – Frank Oct 14 '22 at 21:04
  • MatsLindh - my_class is as following: class my_class(BaseModel): is_checked: bool longitude: float latitude: float – Frank Oct 14 '22 at 21:05
  • It seems to be okay, if BaseModel was imported from pydantic and List from typing. Either this is not the line where you got the exception or you just need to save your code before executing it. – Shashi Kant Oct 15 '22 at 12:36

0 Answers0