In FastAPI, how do you return a StreamingResponse, but also do something after the streaming is done?

48 Views Asked by At

In my FastAPI route, I need to return a StreamingResponse to the user as each chunk of a text sentence gets generated.

However, the complete text sentence needs to be inserted into a database. Is there a way to do this?

@app.post("/foo_stream")
def foo_stream(input: Input, db: Session = Depends(get_db)):
    return StreamingResponse(
        generate_streaming_response(input),
        media_type="text/plain",
    )

    # Now that the streaming is done, how do you insert the entire text into our database?
0

There are 0 best solutions below