Streaming Responses
In Agency Swarm, streaming is handled through theget_response_stream method. The framework returns StreamEvent objects as they are returned by OpenAI, providing direct access to the underlying streaming events.
Streaming from Tools
Tools can inject events into the parent SSE stream while they execute. This is especially useful when a tool runs a sub-agent or a long-running operation and you want the client to receive live progress updates alongside the normal agent output.Accessing the Streaming Context
Inside anyBaseTool.run() method, two properties give you access to the live stream:
streaming_ctx is None when the request is not streaming, so always guard before calling put_event.
Any object passed to streaming_ctx.put_event(event) is forwarded into the same stream your client is already consuming. There are three ways to use this, depending on how informative you want the events to be.
Method 1: Raw values
The simplest option — emit any plain value directly. Useful for quick progress strings during development.Method 2: Existing SDK event types
While neither the OpenAI API nor theagents SDK contains event types specifically designed for streaming output from tool execution, you can construct a RawResponsesStreamEvent wrapping a ResponseTextDeltaEvent directly inside the tool. The internal tracking fields (item_id, content_index, etc.) just need placeholder values — the consumer’s existing delta-handling code treats it identically to a normal LLM text delta.
item_id you set on the event: