Skip to content

feat: add arun_stream and run_stream to HierarchicalSwarm - #1611

Merged
kyegomez merged 2 commits into
kyegomez:masterfrom
Steve-Dusty:feat/hierarchical-streaming
May 28, 2026
Merged

feat: add arun_stream and run_stream to HierarchicalSwarm#1611
kyegomez merged 2 commits into
kyegomez:masterfrom
Steve-Dusty:feat/hierarchical-streaming

Conversation

@Steve-Dusty

@Steve-Dusty Steve-Dusty commented May 13, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds arun_stream(task, img, with_events) async generator to HierarchicalSwarm
  • Adds run_stream(task, img, with_events) sync generator wrapper
  • Bridges the existing streaming_callback plumbing in run() to generator-based streaming via asyncio.Queue
  • with_events=True emits structured agent_start / token / agent_end dicts (same format as AgentRearrange and SequentialWorkflow)
  • Without events, yields (agent_name, token) tuples

How it works

arun_stream runs the existing self.run(streaming_callback=...) in a background thread. The callback pushes (agent_name, chunk, is_final) items to an asyncio.Queue via call_soon_threadsafe. The async generator yields from the queue, accumulating chunks per agent to include the full output in agent_end events.

Parallel worker execution interleaves naturally since multiple threads push to the same queue concurrently.

Test plan

  • python3 -c "from swarms import HierarchicalSwarm; import inspect; print(inspect.signature(HierarchicalSwarm.arun_stream))"
  • Integration test via swarms-api streaming endpoint

🤖 Generated with Claude Code


📚 Documentation preview 📚: https://swarms--1611.org.readthedocs.build/en/1611/

Bridge the existing callback-based streaming in run() to async/sync
generators so HierarchicalSwarm can be consumed the same way as
AgentRearrange and SequentialWorkflow.  arun_stream(with_events=True)
emits agent_start / token / agent_end structured events.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Steve-Dusty
Steve-Dusty force-pushed the feat/hierarchical-streaming branch from b1021d1 to eb8e7ff Compare May 13, 2026 21:41
Rewrite arun_stream to stream every phase of the hierarchical swarm:
- Director plan tokens stream as produced
- Workers stream with parallel interleaving via asyncio.Queue
- Aggregator (feedback director / judge) streams its synthesis
- with_events=True emits swarm_start, director_start/token/end,
  worker_start/token/end, aggregator_start/token/end, swarm_end
- Every event tagged with role and loop index
- Fix call_single_agent to enable streaming_on when callback provided

Add 5 pytest tests covering plain streaming, event types, parallel
interleaving, aggregator feedback, and token metadata.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added the tests label May 14, 2026
@kyegomez
kyegomez merged commit c83735a into kyegomez:master May 28, 2026
5 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants