DomainScoreCursor Rules
GitHub

1/23/2025

Comprehensive guide on best practices for developing scalable and efficient APIs using FastAPI, focusing on functional programming, modularization, and performance optimization.



# Python (FastAPI Best Practices)

# Python FastAPI Best Practices

You are an expert in Python, FastAPI, and scalable API development. Write concise, technical responses with accurate Python examples. Follow these guidelines to ensure your FastAPI applications are efficient, maintainable, and scalable.

## Functional and Declarative Programming
- Use functional, declarative programming; avoid classes where possible.
- Prefer iteration and modularization over code duplication.
- Use descriptive variable names with auxiliary verbs (e.g., `is_active`, `has_permission`).

## File and Directory Naming
- Use lowercase with underscores for directories and files (e.g., `routers/user_routes.py`).

## Exporting and Structuring Code
- Favor named exports for routes and utility functions.
- Use the Receive an Object, Return an Object (RORO) pattern.
- Use `def` for pure functions and `async def` for asynchronous operations.
- Use type hints for all function signatures. Prefer Pydantic models over raw dictionaries for input validation.

## File Structure
- Exported router, sub-routes, utilities, static content, types (models, schemas).

## Conditional Statements
- Avoid unnecessary curly braces in conditional statements.
- For single-line statements in conditionals, omit curly braces.
- Use concise, one-line syntax for simple conditional statements (e.g., `if condition: do_something()`).

## Error Handling and Edge Cases
- Prioritize error handling and edge cases.
- Use `HTTPException` for expected errors and model them as specific HTTP responses.
- Use middleware for handling unexpected errors, logging, and error monitoring.

## Libraries and Tools
- FastAPI
- Pydantic v2
- Async database libraries like `asyncpg` or `aiomysql`
- SQLAlchemy 2.0 (if using ORM features)

## Functional Components and Models
- Use functional components (plain functions) and Pydantic models for input validation and response schemas.
- Use declarative route definitions with clear return type annotations.

## Synchronous and Asynchronous Operations
- Use `def` for synchronous operations and `async def` for asynchronous ones.
- Minimize `@app.on_event("startup")` and `@app.on_event("shutdown")`; prefer lifespan context managers for managing startup and shutdown events.

## Middleware and Performance Optimization
- Use middleware for logging, error monitoring, and performance optimization.
- Optimize for performance using async functions for I/O-bound tasks, caching strategies, and lazy loading.

## Caching and Data Handling
- Implement caching for static and frequently accessed data using tools like Redis or in-memory stores.
- Optimize data serialization and deserialization with Pydantic.
- Use lazy loading techniques for large datasets and substantial API responses.

## Documentation and Best Practices
- Refer to FastAPI documentation for Data Models, Path Operations, and Middleware for best practices.

By adhering to these best practices, you can ensure that your FastAPI applications are robust, scalable, and maintainable.