RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /How-to
  5. /How to use prompt chaining with a code generation pipeline to scaffold a full CRUD API
HOW-TO · DEV

How to use prompt chaining with a code generation pipeline to scaffold a full CRUD API

advanced·35 min·By Fredoline Eruo
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

  1. Define the domain schema: list the entities, fields, and data types for the API. Write this as a structured JSON or Markdown spec.
  2. Phase 1 — Generate data models: create a prompt that takes the domain schema and outputs SQLAlchemy or Pydantic model classes.
  3. Execute Phase 1 prompt, capture the model code, and validate that all entity fields are represented.
  4. Phase 2 — Generate repository layer: pass the Phase 1 models to a prompt that outputs CRUD database operations (create, get, update, delete functions).
  5. Execute Phase 2 prompt and save the repository module. Validate function signatures match Phase 1 model imports.
  6. 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.
  7. Execute Phase 3 prompt and save the routes module. Validate that each endpoint uses correct HTTP methods.
  8. Phase 4 — Generate Pydantic schemas: create a prompt that outputs request/response Pydantic schemas from the domain schema.
  9. Execute Phase 4 prompt and save schemas. Wire schemas into route handlers in a final integration pass.
  10. Write a main.py that imports all modules and mounts routes. Validate the application starts with uvicorn or 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_compile on output before writing.

Related guides

  • chain-prompts-complex-tasks-sequential
  • build-decision-tree-prompt-chain
← All how-to guidesCourses →