from praisonaiagents import Agentfrom praisonaiagents.streaming.events import StreamEventTypeagent = Agent(name="Assistant")def on_stream_event(event): if event.type == StreamEventType.TOOL_CALL_END: print(f"Tool '{event.tool_call['name']}' completed") print(f"Duration: {event.metadata.get('duration_ms')}ms")agent.set_stream_callback(on_stream_event)agent.start("Use the search tool to find information about Python")
2
UI Integration
Use tool events for progress indicators in your UI:
class ToolProgressUI: def __init__(self): self.active_tools = {} def handle_stream_event(self, event): if event.type == StreamEventType.TOOL_CALL_START: self.active_tools[event.tool_call['id']] = { 'name': event.tool_call['name'], 'status': 'running' } self.update_progress_ui() elif event.type == StreamEventType.TOOL_CALL_END: tool_id = event.tool_call['id'] if tool_id in self.active_tools: self.active_tools[tool_id]['status'] = 'completed' self.update_progress_ui()
Don’t rely on TOOL_CALL_RESULT for completion detection. TOOL_CALL_END is the definitive marker:
# Good - reliable completion detectionif event.type == StreamEventType.TOOL_CALL_END: mark_tool_completed(event.tool_call['id'])# Avoid - TOOL_CALL_RESULT may not always fireif event.type == StreamEventType.TOOL_CALL_RESULT: mark_tool_completed(event.tool_call['id'])
Handle tool ID mapping
Track tool calls by their unique IDs, not just names:
# Tools may have same name but different IDstool_tracker = {}def handle_event(event): tool_id = event.tool_call['id'] # Always use ID tool_name = event.tool_call['name'] if event.type == StreamEventType.TOOL_CALL_START: tool_tracker[tool_id] = {'name': tool_name, 'start': time.time()}
Gracefully handle missing events
Network issues may cause missed events. Implement timeouts:
import timeclass RobustToolTracker: def __init__(self, timeout=30): self.tools = {} self.timeout = timeout def cleanup_stale_tools(self): now = time.time() stale = [ tool_id for tool_id, tool in self.tools.items() if tool['status'] == 'running' and now - tool['start_time'] > self.timeout ] for tool_id in stale: self.tools[tool_id]['status'] = 'timeout' self.on_tool_timeout(tool_id)