Building a Multi-File FastAPI Application with Zene
Modern web applications are complex, requiring multiple modules for models, schemas, and database logic. Zene handles this complexity effortlessly, creating a fully structured, multi-file FastAPI project in one go.
In this example, we ask Zene to build a complete Todo List API with SQLite storage.
The Task
python
task = """
Create a RESTful API for a Todo List application using FastAPI and SQLite.
The structure should be modular:
1. `models.py`: SQLAlchemy models for the `Todo` table (id, title, description, completed).
2. `schemas.py`: Pydantic schemas for request/response validation (TodoCreate, TodoResponse).
3. `database.py`: Database connection and session management.
4. `crud.py`: Functions to create, read, update, and delete todos.
5. `main.py`: The FastAPI application with endpoints for CRUD operations.
Finally, create a test script `test_api.py` using `requests` to verify that we can create and list todos.
"""Execution Process
1. Planning
The Planner (DeepSeek) identifies the dependencies between files:
- Dependencies: Start by creating
requirements.txt(fastapi, uvicorn, sqlalchemy). - Database Layer: Create
database.pyfirst, as other files depend on the session. - Models: Create
models.pynext, defining the table structure. - Schemas: Define Pydantic models in
schemas.pyfor API validation. - CRUD Logic: Implement
crud.pyusing the models and database session. - API Endpoints: Wire everything together in
main.py. - Verification: Write and run
test_api.py.
2. Execution & Reflection
- Step 1 (Setup): Zene creates the project structure and installs dependencies.
- Step 3 (Models): The Executor writes the SQLAlchemy model. The Reflector checks for correct imports and table definitions.
- Step 6 (Main): The Executor implements the API endpoints. It correctly handles dependency injection for the database session (
Depends(get_db)). - Step 7 (Test): Zene writes a test script that:
- Starts the FastAPI server in a background thread.
- Sends a POST request to create a todo.
- Sends a GET request to verify it was saved.
- Asserts the response status codes and JSON content.
The Result
A complete, production-ready project structure is generated:
workspace/
├── database.py
├── models.py
├── schemas.py
├── crud.py
├── main.py
└── test_api.pymain.py Snippet:
python
from fastapi import FastAPI, Depends, HTTPException
from sqlalchemy.orm import Session
from . import crud, models, schemas
from .database import SessionLocal, engine
models.Base.metadata.create_all(bind=engine)
app = FastAPI()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.post("/todos/", response_model=schemas.TodoResponse)
def create_todo(todo: schemas.TodoCreate, db: Session = Depends(get_db)):
return crud.create_todo(db=db, todo=todo)Key Takeaway
Zene understands software architecture. It doesn't just dump code into a single file; it respects separation of concerns, creates modular components, and manages dependencies between files intelligently.
