PluginBench
Rule

py fast api

via PatrickJS/awesome-cursorrules

Expert FastAPI backend development with async patterns, Pydantic validation, and scalable API design.

What is py fast api?

Comprehensive guide for building production-grade Python FastAPI applications. Emphasizes async/await patterns, functional programming, type safety with Pydantic v2, and performance optimization through proper error handling, dependency injection, and non-blocking I/O operations.

  • Enforce async-first patterns for I/O-bound operations and database calls
  • Provide Pydantic v2 validation and RORO (Receive Object, Return Object) patterns for request/response handling
  • Structure routes, utilities, and models with clear file organization and named exports
  • Implement guard clauses and early returns for readable error handling and edge-case management
  • Optimize performance via caching strategies, lazy loading, and middleware for logging and monitoring
  • Apply FastAPI dependency injection for state management and shared resource handling

Applies to

File patterns this rule matches.

**/*
Rule definition (reference)

Source of truth, from the repository.

You are an expert in Python, FastAPI, and scalable API development.

Key Principles

  • Write concise, technical responses with accurate Python examples.
  • 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).
  • Use lowercase with underscores for directories and files (e.g., routers/user_routes.py).
  • Favor named exports for routes and utility functions.
  • Use the Receive an Object, Return an Object (RORO) pattern.

Python/FastAPI

  • 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).
  • 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 Validation

  • Prioritize error handling and edge cases:
    • Handle errors and edge cases at the beginning of functions.
    • Use early returns for error conditions to avoid deeply nested if statements.
    • Place the happy path last in the function for improved readability.
    • Avoid unnecessary else statements; use the if-return pattern instead.
    • Use guard clauses to handle preconditions and invalid states early.
    • Implement proper error logging and user-friendly error messages.
    • Use custom error types or error factories for consistent error handling.

Dependencies

  • FastAPI
  • Pydantic v2
  • Async database libraries like asyncpg or aiomysql
  • SQLAlchemy 2.0 (if using ORM features)

FastAPI-Specific Guidelines

  • Use functional components (plain functions) and Pydantic models for input validation and response schemas.
  • Use declarative route definitions with clear return type annotations.
  • 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.
  • 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.
  • Use HTTPException for expected errors and model them as specific HTTP responses.
  • Use middleware for handling unexpected errors, logging, and error monitoring.
  • Use Pydantic's BaseModel for consistent input/output validation and response schemas.

Performance Optimization

  • Minimize blocking I/O operations; use asynchronous operations for all database calls and external API requests.
  • 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.

Key Conventions

  1. Rely on FastAPI’s dependency injection system for managing state and shared resources.
  2. Prioritize API performance metrics (response time, latency, throughput).
  3. Limit blocking operations in routes:
    • Favor asynchronous and non-blocking flows.
    • Use dedicated async functions for database and external API operations.
    • Structure routes and dependencies clearly to optimize readability and maintainability.

Refer to FastAPI documentation for Data Models, Path Operations, and Middleware for best practices.

Related rules

PyQt6 GUI development with EEG signal processing and real-time analysis integration.

**/*
41k
via PatrickJS/awesome-cursorrules

PySpark ETL development with idiomatic code style, joins, window functions, and Iceberg patterns.

**/*
41k
via PatrickJS/awesome-cursorrules

Python best practices for Flask and SQLite projects with modern tooling and patterns

**/*.py +2
41k
via PatrickJS/awesome-cursorrules

Expert Python and TypeScript coding with Django, React, Tailwind, and Inertia.js best practices.

**/*
41k
via PatrickJS/awesome-cursorrules

Python 3.12 FastAPI development with Pydantic, SQLAlchemy, and authentication best practices.

**/*
41k
via PatrickJS/awesome-cursorrules

Python development with containerization best practices and integration.

**/*
41k
via PatrickJS/awesome-cursorrules