3.4 KiB
🛠️ Project Tech & Environment Rules
- Python Version:
3.12 - Dependency Management:
uv(fast, deterministic, PEP 582-compatible) - Backend Framework:
FastAPI - ORM:
SQLAlchemy 2.x - Migrations:
Alembic - Authentication:
fastapi-users+fastapi-jwt-auth - Rate Limiting:
fastapi-limiter - Caching:
fastapi-cache - Email Service:
fastapi-mail - Pagination:
fastapi-pagination - LLM Layer:
litellm - Embedding Models:
Hugging Face Transformers - Vector Store:
pgvectorwith PostgreSQL
Use pure dataclasses between in-app layers (as a transport objects). Use pydantic to validate user (external) input like web APIs.
🧑💻 Python & Backend Code Quality Rules
📦 Structure & Conventions
-
Follow modern
SQLAlchemy 2.0best practices (useasync engine,DeclarativeBase,SessionLocal()pattern). -
Separate concerns clearly:
models/: SQLAlchemy modelsschemas/: Pydantic modelsapi/routes/: FastAPI routersservices/: Business logiccore/: Settings, config, and utilitiestests/: Test suite
🧹 Clean Code Principles
-
Use Meaningful Names: Functions, classes, variables, and routes should clearly communicate their intent.
-
Avoid Overengineering: YAGNI (You Aren’t Gonna Need It) — keep your code minimal, testable, and readable.
-
Follow PEP 8 + Black Formatting: Auto-format with
ruff, lint withrufforflake8. -
Use Type Hints Everywhere: Both function arguments and return types must use type annotations.
-
Use Docstrings:
- One-liner for simple functions.
- Full docstring for public APIs and complex logic.
-
Write Isolated, Testable Logic: Favor pure functions where possible, especially in
services/. -
Handle Exceptions Gracefully:
- Use
HTTPExceptionfor expected FastAPI errors. - Log unexpected errors using
structlog.
- Use
-
Use Dependency Injection: Use
Depends()for shared logic (e.g., current user, DB session, rate limiter).
🧪 Testing Rules
-
Use
pytestas your testing framework. -
Coverage should include:
- CRUD operations
- API endpoints
- Embedding & RAG pipeline logic
-
Use
pytest-asynciofor async route testing. -
Use fixtures for test data setup.
🔒 Security Practices
- Never store plaintext passwords — use hashing (
argon2,bcryptviafastapi-users). - Sanitize file uploads & inputs — protect against injection.
- Use CORS middleware correctly (
allow_credentials,allow_methods, etc.). - Enable rate limiting on sensitive routes like login & upload.
🚀 Performance & Observability
-
Add
structlogstructured logging to:- API entry/exit points
- Query vector lookup latency
- LLM response times
-
Cache results where appropriate (
fastapi-cache) — especially static vector responses. -
Stream LLM responses via FastAPI's
StreamingResponse.