@@ -286,13 +286,23 @@ async def _orchestrate_step(self, cancellation_token: CancellationToken) -> None
286
286
try :
287
287
assert isinstance (ledger_str , str )
288
288
progress_ledger = json .loads (ledger_str )
289
+
290
+ # If the team consists of a single agent, deterministically set the next speaker
291
+ if len (self ._participant_topic_types ) == 1 :
292
+ progress_ledger ["next_speaker" ] = {
293
+ "reason" : "The team consists of only one agent." ,
294
+ "answer" : self ._participant_topic_types [0 ],
295
+ }
296
+
297
+ # Validate the structure
289
298
required_keys = [
290
299
"is_request_satisfied" ,
291
300
"is_progress_being_made" ,
292
301
"is_in_loop" ,
293
302
"instruction_or_question" ,
294
303
"next_speaker" ,
295
304
]
305
+
296
306
key_error = False
297
307
for key in required_keys :
298
308
if (
@@ -303,6 +313,15 @@ async def _orchestrate_step(self, cancellation_token: CancellationToken) -> None
303
313
):
304
314
key_error = True
305
315
break
316
+
317
+ # Validate the next speaker if the task is not yet complete
318
+ if (
319
+ not progress_ledger ["is_request_satisfied" ]["answer" ]
320
+ and progress_ledger ["next_speaker" ]["answer" ] not in self ._participant_topic_types
321
+ ):
322
+ key_error = True
323
+ break
324
+
306
325
if not key_error :
307
326
break
308
327
await self ._log_message (f"Failed to parse ledger information, retrying: { ledger_str } " )
@@ -313,6 +332,7 @@ async def _orchestrate_step(self, cancellation_token: CancellationToken) -> None
313
332
if key_error :
314
333
raise ValueError ("Failed to parse ledger information after multiple retries." )
315
334
await self ._log_message (f"Progress Ledger: { progress_ledger } " )
335
+
316
336
# Check for task completion
317
337
if progress_ledger ["is_request_satisfied" ]["answer" ]:
318
338
await self ._log_message ("Task completed, preparing final answer..." )
0 commit comments