guides/python/ag2/bug-fixer-agent/openai/README.md
This example demonstrates how to build an AI-powered bug fixer using two AG2 agents and Daytona sandboxes. A bug_fixer LLM agent analyzes broken code and proposes fixes, while a code_executor agent runs each fix attempt inside an isolated Daytona sandbox. If execution fails, the bug fixer sees the full error output and retries — looping until the code passes or the turn limit is reached.
The executor supports Python, JavaScript, TypeScript, and Bash — inferring the language automatically from the code block returned by the LLM.
fix_bug returns, regardless of outcomeDAYTONA_API_KEY: Required for access to Daytona sandboxes. Get it from Daytona DashboardOPENAI_API_KEY: Required for GPT-4o-mini access. Get it from OpenAI Platformpython3.10 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install "ag2[daytona,openai]" python-dotenv
.env:DAYTONA_API_KEY=your_daytona_api_key
OPENAI_API_KEY=your_openai_api_key
python main.py
code_executor sends the broken code to bug_fixer as the opening messagebug_fixer (GPT-4o-mini) analyzes it and replies with a fix wrapped in a fenced code blockDaytonaCodeExecutor, and sends the result backbug_fixer sees the error output and proposes a new fixmax_turns is reachedfix_bug returnsThe script runs three examples — one per supported language:
- and /Math.min instead of Math.max============================================================
Example 1: Python — Postfix Expression Evaluator Bug
============================================================
code_executor (to bug_fixer):
Fix this broken code:
...
>>>>>>>> USING AUTO REPLY...
bug_fixer (to code_executor):
```python
def eval_postfix(expression):
...
elif token == '-':
stack.append(a - b) # Fixed order of operands for subtraction
elif token == '/':
stack.append(a // b) # Fixed order of operands for division
...
```
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...
code_executor (to bug_fixer):
exitcode: 0 (execution succeeded)
Code output: All postfix tests passed!
>>>>>>>> USING AUTO REPLY...
bug_fixer (to code_executor):
TERMINATE
For the complete API reference, see the DaytonaCodeExecutor documentation.
See the main project LICENSE file for details.