'Can FastAPI/Pydantic individually validate input items in a list?

I have a FastAPI post method:

from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
from typing import List

import pandas as pd

class InputItem(BaseModel):
    Feature: str

class Item(BaseModel):
    Feature: str
    Result: str

app = FastAPI()

@app.post("/", response_model=List[OutputItem])
def my_function(input: List[InputItem]): 
    df = pd.DataFrame(jsonable_encoder(input))
    result = df.apply(another_method)
    return result.to_dict(orient=records)

My question is, if I pass it a list like this:

[
  {"NOTFeature":"value"},
  {"Feature":"value"},
  {"Feature":"value"}
]

or if one of the values is of a different data type, the whole thing currently fails and returns an error. Is there a way to get it to handle the error so that the failing entry is skipped, and the API function is still carried out for items in the list which do pass validation?

Incidentally, if there's a smoother way to handle the dataframe conversion which still uses the dataframe (these are essential for the other data handling done in the functions), this would be very helpful to know as well!



Solution 1:[1]

Union with dict as a passthrough seemed to work for me, i.e.:

from typing import List, Union

@app.post("/", response_model=List[OutputItem])
def my_function(input: List[Union[InputItem, dict]]): 
    df = pd.DataFrame(jsonable_encoder(input))
    result = df.apply(another_method)
    return result.to_dict(orient=records)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1