guides/python/google-adk/code-generator-agent/gemini/README.md
This example demonstrates how to build a Google ADK agent that generates and verifies code using Daytona sandboxes. The agent uses the DaytonaPlugin to execute code in an isolated environment, enabling automated code generation workflows with built-in testing.
In this example, the agent is tasked with writing a TypeScript groupBy function that takes an array and a key function, then groups array elements by the key. The agent generates the implementation, creates test cases, executes them in the sandbox, and iterates until all tests pass before returning the verified code.
[!TIP] It's recommended to use a virtual environment (
venvorpoetry) to isolate project dependencies.
To run this example, you need to set the following environment variables:
DAYTONA_API_KEY: Required for access to Daytona sandboxes. Get it from Daytona DashboardGOOGLE_API_KEY: Required for Gemini model access. Get it from Google AI StudioSee the .env.example file for the exact structure. Copy .env.example to .env and fill in your API keys before running.
Before proceeding, complete the following steps:
.env.example to .env and add your API keysCreate and activate a virtual environment:
python3.10 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
Install dependencies:
pip install -U google-adk daytona-adk python-dotenv
Run the example:
python main.py
AGENT_INSTRUCTION constant in main.py defines the agent's behavior. You can customize this to change how the agent approaches code generation and testing.When you run the example, the agent follows this workflow:
When the agent completes the task, you'll see output like:
AGENT RESPONSE:
------------------------------------------------------------
```typescript
function groupBy<T, K extends keyof any>(
array: T[],
keyFn: (item: T) => K
): Record<K, T[]> {
return array.reduce((result, item) => {
const key = keyFn(item);
if (!result[key]) {
result[key] = [];
}
result[key].push(item);
return result;
}, {} as Record<K, T[]>);
}
```
============================================================
App closed, sandbox cleaned up. Done!
The agent has already tested this code in the sandbox before returning it, so you can trust that the implementation works correctly.
For the complete API reference, see the daytona-adk documentation.
See the main project LICENSE file for details.