From e37b60e0ace2a3a6142aacc1ab4a81b156829e8a Mon Sep 17 00:00:00 2001 From: Rajat Ahuja Date: Fri, 20 Jun 2025 11:53:24 -0400 Subject: [PATCH 1/2] fix streaming from peer.chat --- src/routers/peers.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/routers/peers.py b/src/routers/peers.py index ec89cfb9..52817553 100644 --- a/src/routers/peers.py +++ b/src/routers/peers.py @@ -1,6 +1,7 @@ import logging from typing import Optional +from anthropic import AsyncMessageStreamManager from fastapi import ( APIRouter, BackgroundTasks, @@ -9,6 +10,8 @@ from fastapi import ( Path, Query, ) +from fastapi.exceptions import HTTPException +from fastapi.responses import StreamingResponse from fastapi_pagination import Page from fastapi_pagination.ext.sqlalchemy import paginate @@ -172,10 +175,33 @@ async def chat( db, workspace_name=workspace_id, peers=[schemas.PeerCreate(name=peer_id)] ) - response = await agent.chat( - workspace_id, peer_id, options.session_id, options.queries, options.stream - ) - return response + if not options.stream: + return await agent.chat( + workspace_id, peer_id, options.session_id, options.queries, options.stream + ) + + else: + + async def parse_stream(): + try: + stream = await agent.chat( + workspace_id, + peer_id, + options.session_id, + options.queries, + stream=True, + ) + if type(stream) is AsyncMessageStreamManager: + async with stream as stream_manager: + async for text in stream_manager.text_stream: + yield text + except Exception as e: + logger.error(f"Error in stream: {str(e)}") + raise HTTPException(status_code=500, detail=str(e)) from e + + return StreamingResponse( + content=parse_stream(), media_type="text/event-stream", status_code=200 + ) @router.post( From fd1f1bfede2a7659c9b769db0600966415b4c9a7 Mon Sep 17 00:00:00 2001 From: Rajat Ahuja Date: Fri, 20 Jun 2025 12:01:04 -0400 Subject: [PATCH 2/2] CR comments --- src/routers/peers.py | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/routers/peers.py b/src/routers/peers.py index 52817553..1bd67427 100644 --- a/src/routers/peers.py +++ b/src/routers/peers.py @@ -180,28 +180,26 @@ async def chat( workspace_id, peer_id, options.session_id, options.queries, options.stream ) - else: + async def parse_stream(): + try: + stream = await agent.chat( + workspace_id, + peer_id, + options.session_id, + options.queries, + stream=True, + ) + if isinstance(stream, AsyncMessageStreamManager): + async with stream as stream_manager: + async for text in stream_manager.text_stream: + yield text + except Exception as e: + logger.error(f"Error in stream: {str(e)}") + raise HTTPException(status_code=500, detail=str(e)) from e - async def parse_stream(): - try: - stream = await agent.chat( - workspace_id, - peer_id, - options.session_id, - options.queries, - stream=True, - ) - if type(stream) is AsyncMessageStreamManager: - async with stream as stream_manager: - async for text in stream_manager.text_stream: - yield text - except Exception as e: - logger.error(f"Error in stream: {str(e)}") - raise HTTPException(status_code=500, detail=str(e)) from e - - return StreamingResponse( - content=parse_stream(), media_type="text/event-stream", status_code=200 - ) + return StreamingResponse( + content=parse_stream(), media_type="text/event-stream", status_code=200 + ) @router.post(