You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add self-debugging loop to CodeExecutionAgent (#6306)
## Why are these changes needed?
This PR introduces a baseline self-debugging loop to the
`CodeExecutionAgent`.
The loop automatically retries code generation and execution up to a
configurable number of attempts (n) until the execution succeeds or the
retry limit is reached.
This enables the agent to recover from transient failures (e.g., syntax
errors, runtime errors) by using its own reasoning to iteratively
improve generated code—laying the foundation for more robust autonomous
behavior.
## Related issue number
Closes#6207
## Checks
- [x] I've included any doc changes needed for
<https://microsoft.github.io/autogen/>. See
<https://github.com/microsoft/autogen/blob/main/CONTRIBUTING.md> to
build and test documentation locally.
- [x] I've added tests (if relevant) corresponding to the changes
introduced in this PR.
- [x] I've made sure all auto checks have passed.
---------
Signed-off-by: Abhijeetsingh Meena <abhijeet040403@gmail.com>
Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
"""(Experimental) An agent that generates and executes code snippets based on user instructions.
63
66
@@ -91,6 +94,8 @@ class CodeExecutorAgent(BaseChatAgent, Component[CodeExecutorAgentConfig]):
91
94
(:py:class:`~autogen_ext.code_executors.docker.DockerCommandLineCodeExecutor` recommended. See example below)
92
95
model_client (ChatCompletionClient, optional): The model client to use for inference and generating code.
93
96
If not provided, the agent will only execute code blocks found in input messages.
97
+
Currently, the model must support structured output mode, which is required for
98
+
the automatic retry mechanism to work.
94
99
model_client_stream (bool, optional): If `True`, the model client will be used in streaming mode.
95
100
:meth:`on_messages_stream` and :meth:`BaseChatAgent.run_stream` methods will
96
101
also yield :class:`~autogen_agentchat.messages.ModelClientStreamingChunkEvent`
@@ -103,6 +108,8 @@ class CodeExecutorAgent(BaseChatAgent, Component[CodeExecutorAgentConfig]):
103
108
This is useful when the agent is part of a group chat and you want to limit the code execution to messages from specific agents.
104
109
If not provided, all messages will be checked for code blocks.
105
110
This is only used if `model_client` is not provided.
111
+
max_retries_on_error (int, optional): The maximum number of retries on error. If the code execution fails, the agent will retry up to this number of times.
112
+
If the code execution fails after this number of retries, the agent will yield a reflection result.
# Step 11: If exit code is non-zero and retries are available then
516
+
# make an inference asking if we should retry or not
517
+
chat_context=awaitmodel_context.get_messages()
487
518
488
-
# Add the code execution result to the model context
489
-
awaitmodel_context.add_message(
490
-
UserMessage(
491
-
content=execution_result.result.output,
492
-
source=agent_name,
519
+
retry_prompt= (
520
+
f"The most recent code execution resulted in an error:\n{execution_result.output}\n\n"
521
+
"Should we attempt to resolve it? Please respond with:\n"
522
+
"- A boolean value for 'retry' indicating whether it should be retried.\n"
523
+
"- A detailed explanation in 'reason' that identifies the issue, justifies your decision to retry or not, and outlines how you would resolve the error if a retry is attempted."
0 commit comments