Posts Tagged: fastapi
-
FastAPI 0.115 to 0.120: New Lifespan Events Replace on_startup and on_shutdown
FastAPI 0.115-0.120 migration: Replace deprecated @app.on_event('startup'/'shutdown') with lifespan=asynccontextmanager(app). DB pools, ML models, uvicorn reload safe. Code before/after, pytest verify. Targets 'fastapi lifespan migration', 'fastapi startup shutdown deprecated', 'fastapi 0.115 lifespan events'.
-
FastAPI Background Tasks vs Celery: When to Use BackgroundTasks for Async Email Sending
FastAPI BackgroundTasks vs Celery for async email sending: Zero-setup fire-and-forget vs distributed queues. Benchmarks show BackgroundTasks add 1.2ms latency for 200ms emails (non-blocking). Celery excels at retries, scaling to 10k+/min. Code examples, Redis setup, when to choose each. Keywords: 'fastapi background tasks celery', 'fastapi async email celery', 'backgroundtasks vs celery fastapi', 'fastapi email background'.
-
FastAPI Dependency Injection: When Depends() Causes Circular Import Errors
FastAPI Depends() circular import errors fix: Move to central deps.py, string refs Depends('get_db'), lazy imports in func, app factories. Common router→model→db cycles. Code + pytest verification. Targets 'fastapi circular import depends', 'fastapi dependency injection error', 'ImportError partially initialized module fastapi'.
-
FastAPI Framework: Python ASGI Microframework Tutorial
FastAPI tutorial: installation, hello world app, Pydantic models, databases, dependencies, testing, deployment. Benchmarks vs Flask and Django.
-
FastAPI OpenAPI Schema Generation: Customizing Swagger UI with Custom CSS and Logo
FastAPI OpenAPI schema generation & Swagger UI customization: Add custom CSS, logo to /docs. Override schema info/tags, serve static files, production uvicorn. Benchmarks: Custom CSS adds 0.2ms latency. Code examples, pydantic models, swagger_ui_css_url. Keywords: 'fastapi custom swagger ui', 'fastapi swagger logo css', 'openapi schema customize fastapi', 'fastapi docs customization', 'swagger ui custom css fastapi'.
-
Scaling FastAPI WebSockets to 10,000 Concurrent Connections with Uvicorn
Configure Uvicorn to handle up to 10,000 concurrent WebSocket connections in FastAPI applications. Covers multi-worker setup, system tuning (ulimit, sysctl), benchmarks showing 5ms p99 latency and 50k msg/s throughput on Python 3.13, verified with websocket-bench. Addresses common issues like connection refused and too many open files.
-
Flask vs FastAPI for Real-Time WebSocket Applications: Latency Benchmarks
Flask vs FastAPI WebSocket benchmarks on Python 3.13 M2 Mac: compare latency and throughput for realtime apps. Includes code setups, py-spy analysis, uvicorn/gunicorn+SocketIO configs.
-
How to Fix 422 Unprocessable Entity Validation Errors in FastAPI Request Bodies
Troubleshooting 422 Unprocessable Entity errors in FastAPI, caused by Pydantic validation failures in request bodies. Covers debugging techniques, common causes like type mismatches and missing fields, and practical solutions including optional fields, unions, validators, and error handling.
-
How to Reduce FastAPI JSON Response Time by 40% Using orjson Instead of stdlib json
FastAPI orjson integration: 40% faster JSON responses (28ms → 17ms p99 avg). Custom ORJSONResponse class, benchmarks uvicorn Python 3.13, 10k req/s load. Pydantic v2 auto-detects orjson. Keywords: fastapi orjson, fastapi json performance, reduce fastapi response time, python json optimization.
-
Implementing OAuth2 with Password Flow in FastAPI: Complete JWT Token Example
Implement OAuth2 password flow in FastAPI with JWT tokens: secure authentication using PyJWT and passlib, including login endpoint, protected routes, and curl tests with a fake user database.
-
Pydantic v2 Migration in FastAPI: Fixing model_validator Decorator Breaking Changes
Pydantic v2 @model_validator(mode='before') receives model instances in some cases like field assignment. Fixes for FastAPI migration, including code examples and tests.
-
pytest-cov: Excluding Virtual Environment and Migration Files from Coverage Reports
Configure .coveragerc to omit virtual environments (venv, .tox), migrations, and other non-source files from pytest-cov reports, focusing on application code. Includes configuration example and Django demo.
-
Python 3.13 Performance Gains: Benchmarking FastAPI Endpoints Before and After Upgrade
We benchmark a minimal FastAPI application on Python 3.12 and 3.13 to measure real-world performance differences in RPS and latency for JSON GET and POST endpoints using Locust and uvicorn workers.
-
Reducing Docker Image Size from 1.2GB to 145MB for Python FastAPI Applications
Reduce FastAPI Docker images from 1.2 GB to 145 MB using Alpine multistage builds and uv. Includes benchmarks, production Dockerfile, .dockerignore, and pitfalls.
-
Serving FastAPI Behind Traefik with Automatic HTTPS and Path-Based Routing
Deploy FastAPI behind Traefik using Docker Compose for automatic HTTPS via Let's Encrypt and path-based routing. Covers docker-compose labels, middleware for prefix stripping, and production considerations.
-
Using pytest Fixtures with FastAPI TestClient for Database Transaction Rollbacks
FastAPI pytest fixtures TestClient database transaction rollbacks: Isolate tests, zero data pollution. SQLAlchemy async sessions, pytest-asyncio override dependency. 500+ tests/s clean state. Code examples, conftest.py fixture, session.begin()/rollback(). Keywords: 'fastapi pytest database testing', 'fastapi testclient fixtures rollback', 'pytest fastapi sqlalchemy transaction rollback', 'fastapi testing isolation', 'pytest fixtures testclient'.