Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

291
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda