Skip to content

fix(fastapi): handle _IncludedRouter in FastAPI v0.137.0+#4722

Open
muhamedfazalps wants to merge 1 commit into
open-telemetry:mainfrom
muhamedfazalps:fix/fastapi-included-router-path
Open

fix(fastapi): handle _IncludedRouter in FastAPI v0.137.0+#4722
muhamedfazalps wants to merge 1 commit into
open-telemetry:mainfrom
muhamedfazalps:fix/fastapi-included-router-path

Conversation

@muhamedfazalps

Copy link
Copy Markdown

Description

FastAPI v0.137.0 changed router.routes from a flat list to a tree structure. Included routers are now wrapped in _IncludedRouter objects that lack a path attribute, causing AttributeError in _get_route_details().

This fix recurses into the sub-routes of _IncludedRouter (via original_router.routes) to find the actual matching route and its path. Also adds try/except for the PARTIAL match path which had the same latent bug.

Fix

  • When _get_route_details() encounters a route without a path attribute (like _IncludedRouter), it now checks for routes or original_router.routes and iterates through sub-routes to find the matching one
  • Added try/except around the PARTIAL match path access (line 495) which previously had no error handling
  • Backward compatible with older FastAPI versions that don't have _IncludedRouter

Testing

Verified with:

  1. Direct routes (no router) - works as before
  2. Included routers (app.include_router()) - now resolves correctly
  3. OPTIONS requests on included routers (the reported issue scenario)

Fixes #4699


If you find this helpful, consider buying me a coffee

FastAPI v0.137.0 changed router.routes from a flat list to a tree
structure. Included routers are now wrapped in _IncludedRouter objects
that lack a path attribute, causing AttributeError in
_get_route_details().

This fix recurses into the sub-routes of _IncludedRouter (via
original_router.routes) to find the actual matching route and its path.
Also adds try/except for the PARTIAL match path which had the same
latent bug.

Fixes open-telemetry#4699

Assisted-by: Claude Opus 4.6
@muhamedfazalps muhamedfazalps requested a review from a team as a code owner June 20, 2026 11:58
@linux-foundation-easycla

Copy link
Copy Markdown

CLA Not Signed

@dmitrybelyakov

Copy link
Copy Markdown

Can this be merged please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

FastAPI v0.137.0 breaks opentelemetry-instrumentation-fastapi

2 participants