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 their answers to an aggregator agent for synthesis.
List[Agent]
required
Agents to run on the task. Each worker’s agent_name labels its turn in the conversation, and its answer is read from its own conversation history.
str
required
Task passed to every worker.
HistoryOutputType
default:"\"all\""
Output format passed to history_output_formatter.
str
default:"\"claude-sonnet-5\""
Model used by the synthesizing aggregator agent. The aggregator sets no max_tokens, temperature or top_p, so it uses the model’s own output limit and the provider’s sampling defaults.
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 final answer, not its full transcript, is added to a new Conversation as one turn, with the worker’s agent_name as the role ("Worker" when the name is empty).
  3. A new Aggregator agent runs with AGGREGATOR_SYSTEM_PROMPT and output_type="final". It receives the worker turns as separate chat messages, each labelled with the worker’s name, plus an instruction to write a ~3,000-word synthesis.
  4. The aggregator’s response is appended to the conversation.
  5. The conversation, one turn per worker plus the aggregator’s turn, 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. Performs a plain linear scan over agents, matching each agent’s .agent_name against agent_name and returning the first match — O(n) per call, with no caching and no fallback to .name.
List[Union[Agent, Callable]]
required
Non-empty list of agent-like objects.
str
required
Name to match against each agent’s .agent_name.
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.

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. With type="dict" it is a list of message dicts with role and content keys.

Safe Single-Agent Run

Look Up an Agent by Name

find_agent_by_name matches only against .agent_name — there is no fallback to .name.

Other Lookup Helpers

Source Code

View the source on GitHub.