Skip to main content

Overview

swarms.structs.ma_blocks is a small set of helper functions for composing multi-agent workflows without instantiating a full swarm class. aggregate, run_agent, and find_agent_by_name are exported from the top-level swarms package. The rest must be imported from swarms.structs.ma_blocks. Reach for these when you want quick composability — a function call instead of ConcurrentWorkflow(...).run(...).

Installation

aggregate()

Run every worker on the same task concurrently, then hand the combined transcript to an aggregator agent for synthesis.
List[Callable]
required
Agents (or any callables matching the Agent interface) to run on the task.
str
required
Task passed to every worker.
HistoryOutputType
default:"\"all\""
Output format passed to history_output_formatter.
str
default:"\"anthropic/claude-3-sonnet-20240229\""
Model used by the synthesizing aggregator agent.
Raises: ValueError if task is None, workers is None, or workers is not a list of callables. Behavior:
  1. All workers run concurrently via run_agents_concurrently.
  2. Each worker’s result is added to a shared Conversation, keyed by worker.agent_name.
  3. A new Aggregator agent runs with AGGREGATOR_SYSTEM_PROMPT and produces a ~3,000-word synthesis of the worker outputs.
  4. The aggregator’s response is appended to the conversation.
  5. The full conversation is returned formatted per type.

run_agent()

Run a single agent on a task with type-checking and error wrapping. Thin convenience over agent.run(task).
Agent
required
Must be an instance of swarms.structs.agent.Agent.
str
required
Task passed to the agent.
HistoryOutputType
default:"\"all\""
Accepted but not currently consumed beyond the call — present for API parity with aggregate.
Raises: Returns: Whatever agent.run(task) returns.

find_agent_by_name()

Look up an agent by name. Builds a name -> agent index the first time it is called for a given agents list — matching against both .agent_name and .name (if .name is set and differs from .agent_name) — and caches that index for subsequent lookups against the same list, turning repeated calls from O(n) into O(1).
List[Union[Agent, Callable]]
required
Non-empty list of agent-like objects.
str
required
Name to match. Checked against each agent’s .agent_name first, then .name if set and different.
Raises:

find_agent_by_id()

Linear search for an agent by its .id attribute. Unlike find_agent_by_name, this does not raise on a miss.
List[Union[Agent, Callable]]
required
List of agent-like objects to search through.
str
required
The .id value to match.
Returns: The matching agent, or None if no agent has that .id. Import: not exported from top-level swarms — use from swarms.structs.ma_blocks import find_agent_by_id.

find_multiple_agents_by_name()

Look up several agents by .agent_name in one call.
List[Union[Agent, Callable]]
required
List of agent-like objects to search through.
List[str]
required
Names to match against each agent’s .agent_name.
Returns: The subset of agents whose .agent_name is in agent_names. Names with no match are silently dropped — no exception is raised. Import: not exported from top-level swarms — use from swarms.structs.ma_blocks import find_multiple_agents_by_name.

return_all_agent_names()

Return every agent’s .agent_name.
List[Union[Agent, Callable]]
required
List of agent-like objects.
Returns: List[str]agent.agent_name for every agent, in order. Import: not exported from top-level swarms — use from swarms.structs.ma_blocks import return_all_agent_names.

Usage Examples

Aggregate Concurrent Analyses

The returned value is the full conversation — three analyst responses plus the aggregator’s synthesis.

Safe Single-Agent Run

Look Up an Agent by Name

find_agent_by_name matches against .agent_name first, and falls back to .name if an agent has a .name attribute set that differs from its .agent_name.

Other Lookup Helpers

Source Code

View the source on GitHub.