✈️ CodePilot — From Requirements to Deployable FastAPI Backend
An automation agent that reads natural-language requirements and existing FastAPI code, reverse-engineers DB schemas, generates migrations, creates databases, and produces deployment configs — all with zero external dependencies.
🎯 What It Does
CodePilot is an automation agent that reads natural-language requirements and existing FastAPI code, then generates the entire backend stack: database schema, MySQL DDL / Alembic migrations, Docker + docker-compose config, GitHub Actions CI/CD, and deployment blueprints for Railway and Render.
$ codepilot all --requirements requirements.md --code-dir ./app
┌─────────────────────────────────────────────────────┐
│ 📄 Parsing requirements... │
│ 🔍 Analyzing existing code... │
│ 💾 Generating schema... │
│ 🗄️ Creating database... │
│ 🚀 Generating deployment configs... │
│ ✅ Pipeline complete! │
└─────────────────────────────────────────────────────┘
🧱 Tech Stack
| Component | Technology |
|---|---|
| Language | Python 3.10+ (stdlib only — no runtime deps) |
| Code analysis | ast module (Python standard library) |
| CLI | argparse (standard library) |
| Schema | MySQL DDL / Alembic auto-generation |
| Deploy | Docker, docker-compose, GitHub Actions, Railway, Render |
The key design decision: zero external runtime dependencies. Everything uses Python’s standard library — ast for code analysis, argparse for CLI, pathlib for paths, dataclasses for data structures.
🏗️ Architecture
agent_pkg/
├── main.py # argparse CLI: parse, analyze, schema, db, deploy, all
├── requirements_parser.py # Regex-based NL requirements parser
├── code_analyzer.py # AST walker: extracts models, routes, auth, deps
├── db_builder.py # Schema generation, MySQL creation, Alembic setup
├── deployer.py # Docker, docker-compose, GHA, Railway, Render configs
└── utils.py # pretty_print, env helpers, config load/save
📝 Requirements Parser
The requirements_parser.py uses regex patterns to extract structured information from natural-language requirements documents:
# E-Commerce API
## Entities
- User: id, name, email, password_hash, created_at
- Product: id, name, price, stock, category_id
- Order: id, user_id, total, status, created_at
## Endpoints
- POST /users/register
- POST /users/login
- GET /products
- POST /orders
- GET /orders/:id
## Auth
- JWT-based authentication
- Protected routes
Extracted output:
- Project name (e.g., “E-Commerce API”)
- Entities with fields and inferred types
- Endpoints with HTTP methods and paths
- Auth requirements (JWT, OAuth, API key)
- Relationships between entities (FK references)
🔍 Code Analyzer
The code_analyzer.py walks the Python AST of an existing FastAPI project to extract:
| Extraction | What it finds |
|---|---|
| SQLAlchemy models | Column(...), relationship(...), Table(...) |
| Route decorators | @app.get(), @router.post(), etc. |
| DB engine type | create_engine() or async_engine() |
| Auth libraries | jose, passlib, fastapi.security, oauth2 |
| Dependencies | FROM/IMPORT statements |
The AST approach is more reliable than regex-based code analysis — it understands the actual structure of the Python code rather than treating it as text.
💾 Schema Builder
The db_builder.py performs two functions:
- DDL Generation: Creates
CREATE TABLEstatements with auto-increment primary keys, timestamps, foreign keys with indexes - MySQL Execution: Connects via
mysqlCLI to create the database and apply schema - Alembic Integration: Optionally sets up Alembic with initial migration
Schema fields from requirements that are missing in the analyzed code are automatically added (schema enrichment).
🚀 Deployer
Generates complete deployment configuration:
| Output | Description |
|---|---|
Dockerfile | Multi-stage Python 3.11 build |
docker-compose.yml | App + MySQL 8.0 with health check |
.github/workflows/deploy.yml | CI/CD with test → build → deploy |
railway.json | Railway deployment config |
render.yaml | Render blueprint |
The docker-compose includes a MySQL 8.0 service with health check and proper dependency ordering.
🚀 Quick Start
# Full pipeline
codepilot all --requirements spec.md --code-dir ./app
# Individual steps
codepilot parse --requirements spec.md
codepilot analyze --code-dir ./app
codepilot schema --requirements spec.md --code-dir ./app
codepilot db --schema output/schema.sql
codepilot deploy --output-dir ./deploy
💡 Why It’s Interesting
CodePilot solves a practical problem — connecting the dots between natural-language specs, existing code, and production infrastructure. The zero-dependency design (stdlib only) means it installs instantly and runs anywhere. The AST-based code analysis is more robust than regex approaches. And the schema enrichment feature (merging requirements fields with discovered code fields) demonstrates how automated tooling can bridge the gap between what’s specified and what’s implemented. It’s a focused, practical automation tool that does one thing well.