🎁 Get the FREE AI Skills Starter Guide β€” Subscribe β†’
BytesAgainBytesAgain
πŸ¦€ ClawHub

FastAPI

by @ivangdavila

Build fast, production-ready Python APIs with type hints, validation, and async support.

Versionv1.0.0
Downloads4,681
Stars⭐ 7
TERMINAL
clawhub install fastapi

πŸ“– About This Skill


name: FastAPI description: Build fast, production-ready Python APIs with type hints, validation, and async support. metadata: {"clawdbot":{"emoji":"⚑","requires":{"bins":["python3"]},"os":["linux","darwin","win32"]}}

FastAPI Patterns

Async Traps

  • Mixing sync database drivers (psycopg2, PyMySQL) in async endpoints blocks the event loop β€” use async drivers (asyncpg, aiomysql) or run sync code in run_in_executor
  • time.sleep() in async endpoints blocks everything β€” use await asyncio.sleep() instead
  • CPU-bound work in async endpoints starves other requests β€” offload to ProcessPoolExecutor or background workers
  • Async endpoints calling sync functions that do I/O still block β€” the entire call chain must be async
  • Pydantic Validation

  • Default values in models become shared mutable state: items: list = [] shares the same list across requests β€” use Field(default_factory=list)
  • Optional[str] doesn't make a field optional in the request β€” add = None or use Field(default=None)
  • Pydantic v2 uses model_validate() not parse_obj(), and model_dump() not .dict() β€” v1 methods are deprecated
  • Use Annotated[str, Field(min_length=1)] for reusable validated types instead of repeating constraints
  • Dependency Injection

  • Dependencies run on every request by default β€” use lru_cache on expensive dependencies or cache in app.state for singletons
  • Depends() without an argument reuses the type hint as the dependency β€” clean but can confuse readers
  • Nested dependencies form a DAG β€” if A depends on B and C, and both B and C depend on D, D runs once (cached per-request)
  • yield dependencies for cleanup (DB sessions, file handles) β€” code after yield runs even if the endpoint raises
  • Lifespan and Startup

  • @app.on_event("startup") is deprecated β€” use lifespan async context manager
  • Store shared resources (DB pool, HTTP client) in app.state during lifespan, not as global variables
  • Lifespan runs once per worker process β€” with 4 Uvicorn workers you get 4 DB pools
  • from contextlib import asynccontextmanager
    @asynccontextmanager
    async def lifespan(app):
        app.state.db = await create_pool()
        yield
        await app.state.db.close()
    app = FastAPI(lifespan=lifespan)
    

    Request/Response

  • Return dict from endpoints, not Pydantic models directly β€” FastAPI handles serialization and it's faster
  • Use status_code=201 on POST endpoints returning created resources β€” 200 is the default but semantically wrong
  • Response with media_type="text/plain" for non-JSON responses β€” returning a string still gets JSON-encoded otherwise
  • Set response_model_exclude_unset=True to omit None fields from response β€” cleaner API output
  • Error Handling

  • raise HTTPException(status_code=404) β€” don't return Response objects for errors, it bypasses middleware
  • Custom exception handlers with @app.exception_handler(CustomError) β€” but remember they don't catch HTTPException
  • Use detail= for user-facing messages, log the actual error separately β€” don't leak stack traces
  • Background Tasks

  • BackgroundTasks runs after the response is sent but still in the same process β€” not suitable for long-running jobs
  • Tasks execute sequentially in order added β€” don't assume parallelism
  • If a background task fails, the client never knows β€” add your own error handling and alerting
  • Security

  • OAuth2PasswordBearer is for documentation only β€” it doesn't validate tokens, you must implement that in the dependency
  • CORS middleware must come after exception handlers in middleware order β€” or errors won't have CORS headers
  • Depends(get_current_user) in path operation, not in router β€” dependencies on routers affect all routes including health checks
  • Testing

  • TestClient runs sync even for async endpoints β€” use httpx.AsyncClient with ASGITransport for true async testing
  • Override dependencies with app.dependency_overrides[get_db] = mock_db β€” cleaner than monkeypatching
  • TestClient context manager ensures lifespan runs β€” without with TestClient(app) as client: startup/shutdown hooks don't fire