1313)
1414from swarms .utils .loguru_logger import initialize_logger
1515from swarms .utils .output_types import OutputType
16+ from typing import Callable , Optional
1617
1718logger = initialize_logger (log_folder = "majority_voting" )
1819
@@ -65,9 +66,16 @@ def default_consensus_agent(
6566 system_prompt : str = None ,
6667 description : str = "An agent that uses consensus to generate a final answer." ,
6768 model_name : str = "gpt-4.1" ,
69+ streaming_callback : Optional [Callable [[str ], None ]] = None ,
6870 * args ,
6971 ** kwargs ,
7072):
73+ # If streaming_on is not None, force it to True; else, set to False
74+ if streaming_callback is not None :
75+ streaming_on_value = True
76+ else :
77+ streaming_on_value = False
78+
7179 return Agent (
7280 agent_name = name ,
7381 agent_description = description ,
@@ -76,6 +84,7 @@ def default_consensus_agent(
7684 system_prompt = system_prompt ,
7785 dynamic_context_window = True ,
7886 dynamic_temperature_enabled = True ,
87+ streaming_on = streaming_on_value ,
7988 * args ,
8089 ** kwargs ,
8190 )
@@ -159,7 +168,7 @@ def reliability_check(self):
159168 title = "Majority Voting" ,
160169 )
161170
162- def run (self , task : str , * args , ** kwargs ) -> List [Any ]:
171+ def run (self , task : str , streaming_callback : Optional [ Callable [[ str , str , bool ], None ]] = None , * args , ** kwargs ) -> List [Any ]:
163172 """
164173 Runs the majority voting system with multi-loop functionality and returns the majority vote.
165174
@@ -179,6 +188,7 @@ def run(self, task: str, *args, **kwargs) -> List[Any]:
179188 )
180189
181190 for i in range (self .max_loops ):
191+
182192 output = run_agents_concurrently (
183193 agents = self .agents ,
184194 task = self .conversation .get_str (),
@@ -190,10 +200,31 @@ def run(self, task: str, *args, **kwargs) -> List[Any]:
190200 role = agent .agent_name ,
191201 content = output ,
192202 )
193-
194- # Now run the consensus agent
203+
204+ # Set streaming_on for the consensus agent based on the provided streaming_callback
205+ self .consensus_agent .streaming_on = streaming_callback is not None
206+
207+ # Instead of a simple passthrough wrapper, match the callback invocation pattern from the provided reference for the consensus agent:
208+ consensus_agent_name = self .consensus_agent .agent_name
209+
210+ if streaming_callback is not None :
211+ def consensus_streaming_callback (chunk : str ):
212+ """Wrapper for consensus agent streaming callback."""
213+ try :
214+ if chunk is not None and chunk .strip ():
215+ streaming_callback (consensus_agent_name , chunk , False )
216+ except Exception as callback_error :
217+ if self .verbose :
218+ logger .warning (
219+ f"[STREAMING] Callback failed for { consensus_agent_name } : { str (callback_error )} "
220+ )
221+ else :
222+ consensus_streaming_callback = None
223+
224+ # Run the consensus agent with the streaming callback, if any
195225 consensus_output = self .consensus_agent .run (
196226 task = (f"History: { self .conversation .get_str ()} " ),
227+ streaming_callback = consensus_streaming_callback ,
197228 )
198229
199230 self .conversation .add (
0 commit comments