Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
Extract a list from the request body sent to FastAPI application

I have a FastAPI application which makes a prediction based on the HTTP POST data sent to it. The code is as follows

import tensorflow as tf
from typing import List
from fastapi import FastAPI, Response
from pydantic import BaseModel

class ItemList(BaseModel):
    instances: List[List[float]]

app = FastAPI()

model = tf.keras.models.load_model('trainedmodel/1')

@app.post('/v1/models/my_model:predict')
def predict(inputdata: ItemList):
    # Extract [[1.5,1.65,2,0.5,-2,1.5,0.1,2,0.2]]
    result = {
                 "predictions": [
                         model.predict(modelInput).flatten().tolist()
                 ]
    }
    return result

The request type to the application is application/json and the request body contains {"instances":[[1.5,1.65,2,0.5,-2,1.5,0.1,2,0.2]]}

I need to extract [[1.5,1.65,2,0.5,-2,1.5,0.1,2,0.2]] from the request body so that I can input it to the model (as modelInput in the above code). In Flask I used request.json['instances'] to do so. Can someone please let me know how I can do the same in FastAPI?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

To access request body you can just use inputdata.instances

class ItemList(BaseModel):
    instances: List[List[float]]

@app.post('/v1/models/my_model:predict')
def predict(inputdata: ItemList):
    # Extract [[1.5,1.65,2,0.5,-2,1.5,0.1,2,0.2]]
    extracted_list = inputdata.instances
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!