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

289
Views
How to have response model personalized which include a status code and a StreamingResponse on FastAPI?

I want my post request response on this format, I'm using FastAPI:

{
  "message": "",
  "status": 200,
  "data": {}
}

So I declare a class to specify my response format and send this model in the response_model parameter. The thing is that in my case data key is a CSV file that's why on post request I have this:

@app.post('/some-path', response_model = ResponseBase)
async def some_path():
    ....

    new_df = pd.DataFrame(final_data, columns=header)
    stream = io.StringIO()
    new_df.to_csv(stream, index=False)
    my_data: Response = StreamingResponse(
        iter([stream.getvalue()]), media_type="text/csv"
    )

    return ResponseBase(
        message="Some message",
        status = status.HTTP_200_OK,
        data = my_data
    )

Response model class, should be something like this:

class ResponseBase(BaseModel):
    message: str
    status: 
    data: 

But I wonder how to specify that status comes from HTTP status code module and data comes from StreamingResponse class.

over 4 years ago · Santiago Trujillo
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!