HOW-TO · DEV
How to use prompt chaining with a code generation pipeline to scaffold a full CRUD API
Target environment
Ubuntu 24.04 · Python 3.12Ubuntu 24.04 · Python 3.12
PREREQUISITES
AI API with chat completion support, chosen web framework (FastAPI, Flask, or similar), Python 3.10+, project directory with write access
What this does
A prompt chaining code generation pipeline synthesizes a complete CRUD (Create, Read, Update, Delete) API by executing a sequence of focused prompts, each producing one layer of the application. Chain phases include: entity model definition, data access layer, route handlers, request validation schemas, and a main application entry point. Each phase consumes the output of the prior phase to ensure consistency across layers.
Steps
- Define the domain schema: list the entities, fields, and data types for the API. Write this as a structured JSON or Markdown spec.
- Phase 1 — Generate data models: create a prompt that takes the domain schema and outputs SQLAlchemy or Pydantic model classes.
- Execute Phase 1 prompt, capture the model code, and validate that all entity fields are represented.
- Phase 2 — Generate repository layer: pass the Phase 1 models to a prompt that outputs CRUD database operations (create, get, update, delete functions).
- Execute Phase 2 prompt and save the repository module. Validate function signatures match Phase 1 model imports.
- Phase 3 — Generate route handlers: pass Phase 1 models and Phase 2 repository to a prompt that outputs FastAPI or Flask route handlers for each CRUD operation.
- Execute Phase 3 prompt and save the routes module. Validate that each endpoint uses correct HTTP methods.
- Phase 4 — Generate Pydantic schemas: create a prompt that outputs request/response Pydantic schemas from the domain schema.
- Execute Phase 4 prompt and save schemas. Wire schemas into route handlers in a final integration pass.
- Write a
main.pythat imports all modules and mounts routes. Validate the application starts withuvicornor the chosen server.
Verification
cd /tmp/crud-scaffold && python3 -c "
from fastapi import FastAPI
app = FastAPI()
print('FastAPI imported successfully, version:', __import__('fastapi').__version__)
" && echo "Application structure validated"
Expected output:
FastAPI imported successfully, version: 0.115.0
Application structure validated
Common failures
- Model class names conflict between phases: Phase 2 repository imports conflict with Phase 3 route names. Solution: use a consistent naming convention with module prefixes (e.g.,
models_user.py,repo_user.py). - Missing import statements in generated code: each phase may omit necessary imports. Solution: add an "import checklist" instruction to each phase prompt requiring explicit import declaration.
- Schema mismatch between Pydantic models and repository functions: field types differ across phases. Solution: inject the complete Phase 1 model code verbatim into every subsequent phase prompt.
- API fails to start due to syntax errors in generated files: model generation produces invalid Python. Solution: wrap each phase execution in a compile check that runs
python3 -m py_compileon output before writing.