python config_error ai_generated true

fastapi.exceptions.FastAPIError: Duplicate parameter name: 'path' in function 'catch_all'

ID: python/fastapi-route-order-matters

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-05-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Defining a catch-all route like `@app.get('/{path}')` after specific routes can cause FastAPI to interpret path parameters incorrectly, leading to duplicate parameter errors or wrong matching.

generic

中文

在特定路由之后定义通配路由(如`@app.get('/{path}')`)可能导致FastAPI错误解释路径参数,引发重复参数错误或错误匹配。

Workarounds

  1. 90% success
    Define specific routes before catch-all routes, and ensure catch-all uses a distinct parameter name like `{path:path}` with a converter.
  2. 85% success
    Use `@app.api_route('/{path}', methods=['GET'], include_in_schema=False)` for catch-all to avoid conflicts.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Reordering routes doesn't fix the duplicate parameter error if the catch-all is too broad.

  2. 75% fail

    Using a different parameter name in the catch-all still conflicts with other routes.