Skip to content

Commit 2e1a9c7

Browse files
authored
Retry multiple times when M1 selects an invalid agent. Make agent sel… (microsoft#5079)
Retry multiple times when M1 selects an invalid agent. Make agent selection deterministic when the team is a singleton (corner case).
1 parent b02965e commit 2e1a9c7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_magentic_one/_magentic_one_orchestrator.py

+20
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,23 @@ async def _orchestrate_step(self, cancellation_token: CancellationToken) -> None
286286
try:
287287
assert isinstance(ledger_str, str)
288288
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
289298
required_keys = [
290299
"is_request_satisfied",
291300
"is_progress_being_made",
292301
"is_in_loop",
293302
"instruction_or_question",
294303
"next_speaker",
295304
]
305+
296306
key_error = False
297307
for key in required_keys:
298308
if (
@@ -303,6 +313,15 @@ async def _orchestrate_step(self, cancellation_token: CancellationToken) -> None
303313
):
304314
key_error = True
305315
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+
306325
if not key_error:
307326
break
308327
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
313332
if key_error:
314333
raise ValueError("Failed to parse ledger information after multiple retries.")
315334
await self._log_message(f"Progress Ledger: {progress_ledger}")
335+
316336
# Check for task completion
317337
if progress_ledger["is_request_satisfied"]["answer"]:
318338
await self._log_message("Task completed, preparing final answer...")

0 commit comments

Comments
 (0)