fix(server): FastAPI 0.137 compatibility — skip route deletion when router is referenced#22277
Conversation
FastAPI 0.137 changed include_router to keep a reference to the original router via _IncludedRouter instead of copying routes. The existing optimisation that deletes routes from source routers after inclusion causes an AttributeError on every request because the app still references the original router for request matching. Gate the deletion behind a feature check for _IncludedRouter so that: - FastAPI < 0.137: routes are still deleted (memory savings preserved) - FastAPI >= 0.137: routes are kept (no duplication occurs anyway) Closes #22276 Co-authored-by: Alexander Streed <alex.s@prefect.io>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Merging this PR will not alter performance
Comparing Footnotes
|
| # FastAPI < 0.137 copies routes when including a router; 0.137+ keeps a | ||
| # reference to the original via _IncludedRouter. | ||
| _FASTAPI_COPIES_ROUTES_ON_INCLUDE: bool = not hasattr( | ||
| fastapi.routing, "_IncludedRouter" | ||
| ) |
There was a problem hiding this comment.
Could we make this check version-based rather than relying on the presence of a private class?
There was a problem hiding this comment.
Good call — switched to packaging.version.Version(importlib.metadata.version("fastapi")) < Version("0.137.0") in a75209e.
Per review feedback, use packaging.version.Version to compare the installed FastAPI version against 0.137.0 rather than checking for the private _IncludedRouter class. Co-authored-by: Alexander Streed <alex.s@prefect.io>
closes #22276
FastAPI 0.137 changed
include_routerto wrap the original router in an_IncludedRouterthat references it for request matching, instead of copying routes into the parent. The existing memory optimisation (del router.routesafter inclusion) deletes the routes that_IncludedRouterstill needs, causingAttributeError: 'PrefectRouter' object has no attribute 'routes'on every request.The fix gates the deletion behind a version check:
Checklist
<link to issue>"mint.json.Link to Devin session: https://app.devin.ai/sessions/cc191d244cbf43b48183c85d17dc8870