@@ -278,14 +278,8 @@ def _execute_step(self) -> bool:
278278 self ._on_step_start (self ._step_index )
279279
280280 # 1. Infer next speaker
281+ self ._switch_speaker_if_needed ()
281282 speaker = self .current_speaker
282- if not speaker .can_handle (self ):
283- logger .debug (
284- "Speaker %s cannot handle current state, switching to default" ,
285- self .current_speaker .get_name (),
286- )
287- self .switch_speaker (self .speakers .default_speaker )
288- speaker = self .speakers [self .current_speaker .get_name ()]
289283
290284 # 2. Get next message(s) from speaker and add to history
291285 logger .debug ("Executing step with speaker: %s" , speaker .get_name ())
@@ -303,9 +297,9 @@ def _execute_step(self) -> bool:
303297 continue_loop = True # we always continue after a tool was called
304298
305299 # 4. Check if conversation should continue and switch speaker if necessary
306- # Note: _handle_result_status must always be called (not short-circuited)
300+ # Note: _handle_continue_conversation must always be called (not short-circuited)
307301 # because it has side effects (e.g., triggering speaker switches).
308- status_continue = self ._handle_result_status (result )
302+ status_continue = self ._handle_continue_conversation (result )
309303 continue_loop = continue_loop or status_continue
310304
311305 self ._on_step_end (self ._step_index , result )
@@ -396,6 +390,15 @@ def _handle_continue_conversation(self, result: SpeakerResult) -> bool:
396390 # status == "continue"
397391 return True
398392
393+ def _switch_speaker_if_needed (self ) -> None :
394+ """Switch to default speaker if current one cannot handle."""
395+ if not self .current_speaker .can_handle (self ):
396+ logger .debug (
397+ "Speaker %s cannot handle current state, switching to default" ,
398+ self .current_speaker .get_name (),
399+ )
400+ self .switch_speaker (self .speakers .default_speaker )
401+
399402 @tracer .start_as_current_span ("switch_speaker" )
400403 def switch_speaker (
401404 self ,
0 commit comments