Skip to main content
This example demonstrates how to build a complete data analysis system that can load data, perform statistical analysis, create visualizations, and generate reports.

What You’ll Learn

  • Automated data analysis workflows
  • Safe code execution for data processing
  • Visualization generation
  • Automated report creation
  • Error handling and data validation

Prerequisites

1

Install AutoGen with Docker support

2

Install Docker

Install Docker Desktop from docker.com
3

Set your OpenAI API key

Architecture

The data analysis system uses:
  • Data Analyst: Plans analysis approach
  • Code Writer: Generates Python analysis code
  • Code Executor: Runs code safely in Docker
  • Report Writer: Synthesizes findings into reports

Code Example

Run the Example

First, create sample data:
Run the analysis:

Expected Output

---------- code_executor ---------- Code executed successfully. Revenue by Category: category Widgets 245,678Gadgets245,678 Gadgets 198,432
Tools $87,234
Top 10 Products: [… output …] Visualization saved: sales_analysis.png ---------- report_writer ----------

SALES ANALYSIS REPORT

Executive Summary

Total revenue analyzed: 531,344across1,000transactionsTopcategory:Widgets(46.2Highestperformingproduct:WidgetA(531,344 across 1,000 transactions Top category: Widgets (46.2% of revenue) Highest performing product: Widget A (128,456)

Key Findings

  • Widgets category dominates with nearly half of total revenue
  • Strong seasonality observed with peaks in Q2 and Q4
  • Regional distribution relatively balanced
  • Top 3 products account for 52% of total revenue

Recommendations

  1. Increase inventory for Widget products during peak seasons
  2. Investigate underperformance in Tools category
  3. Expand successful product lines
See sales_analysis.png for detailed visualizations.

Automated Insights

Key Concepts

Code Generation

Agents write Python code for data analysis tasks.

Safe Execution

Docker isolation ensures safe code execution.

Automation

End-to-end automated analysis pipeline.

Visualization

Automatic generation of charts and graphs.

Best Practices

  1. Data Validation: Always validate input data before analysis
  2. Error Handling: Wrap code in try-except blocks
  3. Reproducibility: Set random seeds for consistent results
  4. Documentation: Comment code and document methodology
  5. Resource Limits: Set timeouts and memory limits
  6. Version Control: Track analysis scripts and results

Production Enhancements

Add Caching

Add Scheduling

Add Notifications

Troubleshooting

Package Installation Fails

Use custom Docker image with pre-installed packages:

Out of Memory

Increase container memory limit:

Code Execution Timeout

Increase timeout for large datasets:

Next Steps

Code Execution

Learn more about code execution patterns

Tools

Explore tool creation and integration