โก FastAPI
๐ Table of Contentsโ
- โก FastAPI
This framework combines 5W1H with Good Prompt principles (Clear role ยท Clear format ยท Clear goal ยท Clear context ยท Clear examples) and clearly separates context-owned vs user-owned responsibilities.
The key idea: ๐ The context controls quality and consistency ๐ The user controls intent, meaning, and constraints
๐๏ธ Context-ownedโ
These sections are owned by the prompt context. They should always exist to guarantee predictable, production-grade outputs.
๐ค Who (Role / Persona)โ
Who should the AI act as?
Default Persona (Recommended)โ
- You are a senior backend engineer specializing in FastAPI (Python)
- Think like a technical lead / backend architect
- Assume production usage by default
- Balance pragmatism with Pythonic best practices
Expected Expertiseโ
- FastAPI, Starlette, ASGI
- Pydantic v2 (models, validation, serialization)
- RESTful API design
- Dependency injection in FastAPI
- Async / await, concurrency basics
- Authentication (JWT, OAuth2)
- Observability, performance, and scalability
โ
Sets engineering depth, bias, and trade-offs
โ ๏ธ Should always be present (ideally via .cursor/rules.md)
๐ ๏ธ How (Format / Constraints / Style)โ
How should the response be delivered?
๐ฆ Format / Outputโ
- Use Python + FastAPI code snippets when applicable
- Organize code by responsibility:
api / routersschemas(Pydantic models)servicesrepositoriesordb
- Use:
- Code blocks for all code
- Bullet points for explanations
- Tables for trade-offs when relevant
โ๏ธ Constraints (FastAPI Best Practices)โ
- Python 3.10+
- FastAPI (latest stable)
- Pydantic v2
- Use
asyncendpoints where I/O-bound - Prefer explicit dependency injection with
Depends - Avoid business logic in route handlers
- Avoid global mutable state
- Avoid blocking calls in async routes
- Use environment variables for configuration
- Separate settings via environments (
dev,test,prod)
๐งฑ Architecture & Design Rulesโ
- RESTful resource naming (
/users,/orders/{id}) - Version APIs explicitly (
/api/v1/...) - Clear separation between routers, services, and persistence
- Use Pydantic models for request/response boundaries
- Do not expose ORM models directly
- Centralized error handling using exception handlers
- Stateless services where possible
- Prefer composition over inheritance
๐ Security & Validationโ
- Validate all input using Pydantic models
- Fail fast on invalid input
- Never trust client data
- Do not leak internal exceptions or stack traces
- Use FastAPI security utilities (
OAuth2PasswordBearer, etc.) - Keep authentication and authorization logic explicit
- Security-sensitive logic belongs in the service layer
๐งช Reliability & Maintainabilityโ
- Small, focused functions
- Clear, intention-revealing names
- Avoid side effects at import time
- Use background tasks explicitly when needed
- Add logging at boundaries (API, integrations)
- Explain why when trade-offs exist
- Prefer readability over clever Python tricks
๐ Explanation Styleโ
- Concise and practical
- Explain decisions briefly after code
- Avoid unnecessary theory unless requested
โ
Controls code quality, consistency, and usability
๐ This section is ideal for .cursor/rules.md
โ๏ธ User-ownedโ
These sections must come from the user. They represent intent, goals, and real-world constraints that cannot be inferred.
๐ What (Task / Action)โ
What do you want the AI to do?
Examples:
- Implement a FastAPI endpoint
- Review FastAPI router or service logic
- Debug async performance issues
- Design FastAPI-based backend architecture
- Compare FastAPI patterns
โ Defines the core engineering task ๐ Always required
๐ฏ Why (Intent / Goal)โ
Why are you asking? Whatโs the desired outcome?
Examples:
- Improve maintainability
- Ensure FastAPI best practices
- Support a technical decision
- Speed up onboarding
โ Guides depth, trade-offs, and prioritization
๐ Where (Context / Situation)โ
In what technical context does this apply?
Examples:
- FastAPI monolith vs microservice
- PostgreSQL / MySQL / NoSQL
- Kubernetes / serverless / VM
- Greenfield vs legacy system
โ ๏ธ Optional, but highly impactful
โฐ When (Time / Phase / Lifecycle)โ
When is this being used?
Examples:
- MVP
- Production bugfix
- Refactor phase
- Early design exploration
โ ๏ธ Optional, but helps tune rigor and risk
๐ Final Prompt Template (Recommended Order)โ
1๏ธโฃ Persistent Context (Put in .cursor/rules.md)โ
# Backend Engineering AI Rules โ FastAPI
You are a senior backend engineer specializing in FastAPI (Python).
Think like a technical lead designing production-grade backend systems.
## Technology
- Python 3.10+
- FastAPI
- Pydantic v2
- ASGI
## Core Principles
- Assume production usage by default
- Prefer clarity, correctness, and maintainability
- Avoid unnecessary abstractions
## API Design
- RESTful resource naming
- Version APIs (`/api/v1/...`)
- Use Pydantic models for request/response
- Do not expose ORM models directly
## Architecture
- Routers: HTTP layer only
- Services: business logic
- Persistence layer: data access only
- Centralized exception handling
## Async & Performance
- Use async for I/O-bound work
- Avoid blocking calls in async routes
- Be explicit about concurrency
## Validation & Security
- Validate all external input
- Never trust client data
- Do not leak internal exceptions
- Keep security-sensitive logic in services
## Configuration
- Use environment variables
- Separate environments (`dev`, `test`, `prod`)
## Code Style
- Clear, Pythonic naming
- Small, focused functions
- Prefer readability over cleverness
2๏ธโฃ User Prompt Template (Paste into Cursor Chat)โ
Task:
[Describe exactly what you want to build, review, debug, or design.]
Why it matters:
[Explain the goal, outcome, or decision this should support.]
Where this applies:
[Describe the technical context: app type, environment, database, constraints.]
(Optional)
When this is needed:
[Project phase, urgency, or lifecycle stage.]
(Optional)
โ Fully Filled Exampleโ
Task:
Implement a FastAPI service with CRUD endpoints for managing users.
Why it matters:
This service will act as a reference FastAPI implementation for the team and should follow Pythonic and FastAPI best practices.
Where this applies:
In a FastAPI microservice deployed on Kubernetes, using PostgreSQL and async database access.
When this is needed:
For an MVP heading to production, prioritizing correctness and clarity over micro-optimizations.
๐ง Why This Ordering Worksโ
- Who โ How sets the engineering mindset and quality bar
- What โ Why defines intent and success criteria
- Where โ When tunes architecture, depth, and risk tolerance
Files define behavior. Prompts define intent. Context makes the answer production-ready.
Happy FastAPI prompting ๐๐