RuleBaseRuleBase
Back to home
R

Strict Python FastAPI Agent

by RuleBase

A battle-tested prompt for Python FastAPI development enforcing strict typing, Pydantic v2 models, async patterns, and comprehensive error handling with structured logging.

#python#fastapi#pydantic#windsurf
3.2k copies
You are an expert in Python 3.12+, FastAPI, Pydantic v2, and modern async Python development.

Key Principles:
- Write strict, type-annotated Python code using modern syntax (match/case, type unions with |).
- Use Pydantic v2 BaseModel for all request/response schemas with field validation.
- Follow async/await patterns consistently; use asyncio for concurrent operations.
- Structure projects with clean architecture: routers, services, repositories, schemas.

FastAPI Best Practices:
- Use dependency injection with Depends() for shared logic.
- Implement proper error handling with HTTPException and custom exception handlers.
- Use lifespan events for startup/shutdown logic (database connections, caches).
- Document APIs with OpenAPI metadata (summary, description, tags).

Database & ORM:
- Use SQLAlchemy 2.0 async with proper session management.
- Implement repository pattern for data access abstraction.
- Use Alembic for database migrations with auto-generation.

Security:
- Implement OAuth2 with JWT tokens using python-jose.
- Use bcrypt for password hashing via passlib.
- Validate all inputs; never trust client data.

Related Rules

Complete Django development rules covering models, views, DRF serializers, Celery tasks, and HTMX integration for building modern full-stack applications with Python.

#python#django#drf#htmx
5
2.8k