Overview
TheMajorityVoting module provides a sophisticated multi-loop consensus building system for agents. Unlike simple majority voting, this system enables iterative consensus building where agents can refine their responses across multiple loops, with each subsequent loop considering the previous consensus. This approach leads to more robust and well-reasoned final decisions by leveraging the collective intelligence of multiple specialized agents.
Installation
Architecture
Key Concepts
- Multi-Loop Consensus Building: An iterative process where agents can refine their responses across multiple loops, with each loop building upon the previous consensus.
- Agents: Specialized entities (e.g., models, algorithms) that provide expert responses to tasks or queries.
- Consensus Agent: An automatically created agent that analyzes and synthesizes responses from all agents to determine the final consensus.
- Conversation History: A comprehensive record of all agent interactions, responses, and consensus building across all loops.
- Concurrent Execution: Agents run simultaneously for improved performance and efficiency.
Attributes
Unique identifier for the majority voting system. When omitted, one is generated as
majority-voting-<32 hex characters>.When
True, each run writes the conversation to conversation_history.json in a workspace directory, {WORKSPACE_DIR}/swarms/MajorityVoting/{name}-{timestamp}/. The directory path is available as swarm_workspace_dir.Output format passed to
history_output_formatter, for example "dict", "list", "str", "json", or "final". "dict" returns the message list, including the task.A comms channel the vote posts to: the task as
user, each voter’s answer, then the consensus agent’s answer. Any channel set on a member agent is detached with a warning, so each message is posted once. See Comms Channels.The constructor validates the configuration and raises
ValueError if agents is empty or None, or if max_loops is 0 or less.Methods
run()
Executes the multi-loop majority voting system for a single task and returns the consensus result.task(str): The task or question to be analyzed by the agent panelstreaming_callback(Optional[Callable[[str, str, bool], None]]): Optional callback invoked as(agent_name, chunk, is_final)while the consensus agent streams its response. Only the consensus agent’s output is streamed, andis_finalis alwaysFalse.*args,**kwargs: Accepted for compatibility and ignored. They are not forwarded to the agents.
output_type
Process Flow:
- Starts a fresh conversation and adds the task to it, so a reused instance never carries an earlier task’s votes
- For each loop (up to
max_loops):- Runs all voters concurrently. Each voter receives the shared conversation as typed chat turns: the task as a
userturn, its own earlier answers asassistantturns, and peers’ answers and earlier consensus asuserturns labelled with the speaker’s name - Records each voter’s final answer (not its whole transcript) under the voter’s name. If a voter raises,
run()raises that error instead of counting it as a vote - Runs the consensus agent on the same typed history and records its answer
- Runs all voters concurrently. Each voter receives the shared conversation as typed chat turns: the task as a
- Saves the conversation to the workspace when
autosave=True - Returns the conversation in the specified output format
arun()
Async version ofrun(). Each voter is awaited through its own Agent.arun() on the event loop, so the vote can run inside an existing asyncio application without a thread pool of its own.
task(str): The task or question to be analyzed by the agent panelmax_concurrency(Optional[int]): Most agents running at once.None(the default) runs every voter at once
output_type, as run() returns
Raises:
ValueError: Ifmax_concurrencyis less than1- Whatever a voter or the consensus agent raised
run(): a fresh conversation, every voter’s answer recorded under its name, then the consensus agent’s answer, for max_loops loops, with the conversation saved when autosave=True. It takes no streaming_callback, and the consensus agent’s streaming is turned off.
batch_run()
Executes the majority voting system for multiple tasks sequentially. Each task starts from a fresh conversation.tasks(List[str]): List of tasks or questions to be processed*args(Any): Variable length argument list passed to each task execution**kwargs(Any): Arbitrary keyword arguments passed to each task execution
run_concurrently()
Executes the majority voting system for multiple tasks concurrently in a thread pool. Each task runs on its own shallow copy of the instance, so the tasks never share a conversation. The copies share the same voter and consensus agents.tasks(List[str]): List of tasks or questions to be processedmax_workers(int, keyword): Number of threads. Defaults toos.cpu_count()*args,**kwargs: Forwarded to each task’sruncall
tasks[i]
reliability_check()
Performs validation checks on the majority voting system configuration.ValueError: If agents list is empty or NoneValueError: Ifmax_loopsis less than or equal to0
Consensus Agent
The MajorityVoting system automatically creates a specialized consensus agent that analyzes and synthesizes responses from all participating agents. This consensus agent:- Comprehensively evaluates each agent’s response across accuracy, depth of analysis, relevance, clarity, and unique perspectives
- Performs comparative analysis by identifying overlapping themes, divergent viewpoints, and strengths/weaknesses
- Builds consensus by identifying the most effective responses and synthesizing best elements
- Delivers actionable results that are fair, balanced, evidence-based, and well-supported
consensus_agent_prompt: Custom system promptconsensus_agent_name: Name for the agentconsensus_agent_description: Descriptionconsensus_agent_model_name: Model to useadditional_consensus_agent_kwargs: Additional configuration
Usage Examples
Financial Analysis with Specialized Agents
Investment Analysis with Consensus Agent
Content Creation with Batch Processing
Research Analysis with Concurrent Processing
Majority Voting with Custom Streaming
The callback receives the consensus agent’s output as it streams. Voters are not streamed.Usage Patterns
Single Task Analysis
Agent or pass them to the consensus agent through additional_consensus_agent_kwargs. run ignores extra keyword arguments.