Find answers from the community

Updated last week

Streaming Chat Response with FastAPI

i have fastapi like this


@r.get("/chat", tags=["basic"])
async def get_prompt(request: RmsGptRequest = Depends()):
logger.info(f"Getting results with params: {request}")

# Stream the response
handler = tara.run(query=request.prompt,user_email=request.user_email)
return EventSourceResponse(handler.stream_events())


how can i return the stream data as json directly from workflow ?? because I'm getting the data like this in Postman

data: data=' R' source='internal'
L
l
3 comments
The events are pydantic objects. Convert them to dicts or json string
Plain Text
async def gen():
  async for ev in handler.stream_events():
    yield ev.model_dump()
return EventSourceResponse(gen())
ooo, thx @Logan M appreciate it. :LlamaIndex:
Add a reply
Sign up and join the conversation on Discord