FastAPI v0.140.5, released as a patch, addresses a performance regression introduced in earlier versions. The key fix avoids flattening dependencies for body fields, streamlining request handling.
- Refactored dependency processing for body fields: The core change in PR #16071 by @tiangolo prevents FastAPI from flattening dependency trees when the dependency is used specifically on a body field. In previous releases, even trivial dependencies (like
Depends()with no extra logic) triggered a flatten operation, which could increase processing time proportional to the number of dependencies. - Minimal performance improvement: For most applications, the impact will be small, but for APIs with many body fields (e.g., complex nested models) or heavy dependency graphs, this reduces unnecessary computation during request validation.
- Backward-compatible: No API or behavior changes; existing applications continue to work seamlessly after upgrading.
Why this matters: While FastAPI already offers excellent performance, eliminating redundant operations ensures it scales efficiently with complex data models. Developers using Pydantic v2 models with deeply nested fields will notice slightly faster request parsing, especially under load. This patch aligns with FastAPI's philosophy of "zero-cost abstractions"—removing overhead that doesn't contribute to functionality.
To upgrade:
pip install 'fastapi>=0.140.5'
Source: https://github.com/fastapi/fastapi/releases/tag/0.140.5