'FastAPI array of JSON in Request Body for Machine Learning prediction

I’m working with FastAPI for Model inference in Machine Learning, so I need to have as inputs an array of JSON like this:

[
  {
    "Id":"value",
    "feature1":"value",
    "feature2":"value",
    "feature3":"value"
  },
  {
    "Id":"value",
    "feature1":"value",
    "feature2":"value",
    "feature3":"value"
  },
  {
    "Id":"value",
    "feature1":"value",
    "feature2":"value",
    "feature3":"value"
  }
]

The output (result of prediction) should look like this :

[
  {
    "Id":"value",
    "prediction":"value"
  },
  {
    "Id":"value",
    "prediction":"value"
  },
  {
    "Id":"value",
    "prediction":"value"
  }
]

How to implement this with FastAPI in Python?



Solution 1:[1]

You can declare a request JSON body using a Pydantic model (let's say Item), as described here, and use List[Item] to accept a JSON array (a Python List), as documented here. In a similar way, you can define a Response model. Example below:

from pydantic import BaseModel
from typing import List

class ItemIn(BaseModel):
    Id: str
    feature1: str
    feature2: str
    feature3: str

class ItemOut(BaseModel):
    Id: str
    prediction: str
    
@app.post('/predict', response_model=List[ItemOut])
def predict(items: List[ItemIn]):
    return [{"Id":  "value", "prediction": "value"}, {"Id":  "value", "prediction": "value"}]

Update

You can send the data to the predict() function, as described in this answer. Example below:

@app.post('/predict', response_model=List[ItemOut])
def predict(items: List[ItemIn]):
    for item in items:
        pred = model.predict([[item.feature1, item.feature2, item.feature3]])[0] 

or, use the following, as described here (Option 3), to avoid looping over the items and calling the predict() function multiple times:

import pandas as pd

@app.post('/predict', response_model=List[ItemOut])
def predict(items: List[ItemIn]):
    df = pd.DataFrame([i.dict() for i in items])
    pred = model.predict(df)

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