unionai-oss / pandera

A light-weight, flexible, and expressive statistical data testing library
https://www.union.ai/pandera
MIT License
3.27k stars 305 forks source link

FastAPI integration #690

Closed cosmicBboy closed 2 years ago

cosmicBboy commented 2 years ago

Is your feature request related to a problem? Please describe.

FastAPI supports pydantic models as part of request bodies and responses. Pandera now integrates with pydantic, but there are a few more things needed to integrate with FastAPI in a more seamless manner. Doing this will create a nicer experience when requests/responses involve dataframes:

Describe the solution you'd like

At a high level, we want to:

Support DataFrame[Schema] types in http method function definitions:

class Transactions(pa.SchemaModel):
    id: pa.typing.Series[int]
    cost: pa.typing.Series[float]

    class Config:
        coerce = True

@app.post("/path/")
def create_transactions(transactions: pa.typing.DataFrame[Transactions]):
    return transactions

Support response_model for serialization of dataframes to different formats

class TransactionsResponse(Transactions):
    class Config:
        format = "json"

@app.post("/path/", response_model=pa.typing.DataFrame[TransactionsResponse])
def create_transactions(transactions: pa.typing.DataFrame[Transactions]):
    return transactions

Support File Request endpoints

With the response_model argument, users should be able to upload compressed binaries, e.g. parquet or feather format:

from pydantic import BaseModel

class ResponseModel(BaseModel):
    filename: str
    df: pa.typing.DataFrame[TransactionsResponse]

@app.post("/uploadfile/", response_model=ResponseModel)
async def create_upload_file(file: UploadFile = File(...)):
    return {"filename": file.filename, "df": file}  # pandera should handle the "df" file and serialize it to json format
cosmicBboy commented 2 years ago

fixed by #741

deepaerial commented 1 year ago

Does this PR supports FastAPI HTTP 422 error responses when schema raises SchemaError?

cosmicBboy commented 1 year ago

hey @deepaerial I'm not certain but it'll use whatever error response routine that fastapi uses, which I believe is pydantic... but please feel free to add a reproducible code snippet if you need help debugging