from flask import Response def generate_response_stream(response_gen): """Yield the response stream.""" for text in response_gen: yield text @app.route('/query', methods=['POST']) def process_query(): data = request.get_json() user_query = data['query'] response = query_engine.query(user_query) return Response(generate_response_stream(response.response_gen), mimetype='text/plain')