Skip to main content

Overview

SwarmRearrange is a class for orchestrating multiple swarms in a flow pattern. Each swarm is looked up by its name attribute, and its output becomes the next step’s task. Every swarm’s output is also added to arrangement.conversation.

Installation

Attributes

str
default:"generate_id('swarm-rearrange')"
Unique identifier for the swarm arrangement, e.g. swarm-rearrange-<32 hex> — not a UUID.
str
default:"SwarmRearrange"
Name of the swarm arrangement.
str
default:"A swarm of swarms..."
Description of the arrangement.
List[Any]
default:"[]"
List of swarm objects to be managed. Despite the default of [], the constructor runs reliability_checks() and raises ValueError if swarms is empty — a non-empty list is effectively required.
str
default:"None"
Flow pattern for swarm execution. Uses arrow notation to define execution order. Despite the default of None, reliability_checks() raises ValueError if flow is falsy — a flow string is effectively required.
int
default:"1"
Number of passes through the whole flow. Values of 0 or less are replaced with 1.
bool
default:"True"
Stored but not read. Logging happens either way.
bool
default:"False"
Enable human intervention during execution.
Callable
default:"None"
Custom function for human interaction.
bool
default:"False"
Stored but not read. run() always returns a string.
HistoryOutputType
default:"dict-all-except-first"
Currently has no effect: self.output_type is stored but never read. run() never calls history_output_formatter and always returns current_task, a plain string (see run() below).

Methods

add_swarm()

Adds a single swarm to the arrangement.
Parameters:
  • swarm (Any): The swarm object to add

remove_swarm()

Removes a swarm by name from the arrangement.
Parameters:
  • swarm_name (str): Name of the swarm to remove

add_swarms()

Adds multiple swarms to the arrangement.
Parameters:
  • swarms (List[Any]): List of swarm objects to add

validate_flow()

Validates the flow pattern syntax and swarm names.
Returns: True if the flow is valid. Raises: ValueError if "->" is missing from the flow, a referenced swarm name isn’t registered (and isn’t "H"), or the flow contains duplicate swarm names.

set_custom_flow()

Overrides the current flow pattern.
Parameters:
  • flow (str): The new flow pattern string

run()

Executes the swarm arrangement according to the flow pattern.
Parameters:
  • task (str, optional): The task to be executed
  • img (str, optional): Image input for the task
  • custom_tasks (Dict[str, str], optional): Custom tasks mapped to specific swarms. See the warning below
  • *args, **kwargs: Forwarded to each swarm’s run() call, after task and img
Returns: A plain str — always current_task (the final swarm’s textual output), regardless of output_type. run() catches all exceptions internally and, on failure, returns str(e) instead of raising — so callers must check the returned string for error text rather than relying on a try/except around run().
custom_tasks does not work in the current release. Only its first entry is read, and it is matched against the raw flow segments, which keep their surrounding spaces. If it does match, the task text is spliced into the flow as if it were a swarm name, so the run fails and returns the error string. Put the task in task instead.

Flow Pattern Syntax

The flow pattern uses arrow notation (->) to define execution order:
  • Sequential: "SwarmA -> SwarmB -> SwarmC"
  • Fan-in: "SwarmA, SwarmB -> SwarmC". Comma-separated swarms run one after another, not concurrently, each on the same input. Their outputs are joined with "; " and passed to the next step
  • Human intervention: Use "H" in the flow. With human_in_the_loop=True and a custom_human_in_the_loop function, that function receives the current task and its return value becomes the next task. Otherwise run() prompts on stdin with input()
The flow must contain at least one ->, and each swarm name may appear only once.

Usage Examples

Basic Sequential Flow

Human-in-the-Loop

Best Practices

Source Code

View the source code on GitHub