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.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.Model used by the synthesizing aggregator agent. The aggregator sets no
max_tokens, temperature or top_p, so it uses the Agent default of 16,384 output tokens and the provider’s sampling defaults.ValueError if task is None, workers is None, or workers is not a list of callables.
Behavior:
- All workers run concurrently via
run_agents_concurrently. - Each worker’s final answer, not its full transcript, is added to a new
Conversationas one turn, with the worker’sagent_nameas the role ("Worker"when the name is empty). - A new
Aggregatoragent runs withAGGREGATOR_SYSTEM_PROMPTandoutput_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. - The aggregator’s response is appended to the conversation.
- 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 overagent.run(task).
Accepted but not currently consumed beyond the call — present for API parity with
aggregate.
Returns: Whatever
agent.run(task) returns.
find_agent_by_name()
Look up an agent by name. Performs a plain linear scan overagents, 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.
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.
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[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
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.