ai-lawyer-agent/core/stream_response.py
2025-12-15 23:21:10 +01:00

14 lines
626 B
Python

from agents import Agent, Runner, SQLiteSession
from openai.types.responses import ResponseTextDeltaEvent
async def stream_response(agent: Agent, prompt: str, session: SQLiteSession):
"""Stream agent response and update the UI."""
try:
result = Runner.run_streamed(agent, input=prompt, session=session)
async for event in result.stream_events():
if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
yield event.data.delta # <-- sends the next piece of response text
except Exception as e:
yield f"⚠️🖨️ Error: {e}"