python config_error ai_generated true

AssertionError: Duplicate route: /items

ID: python/fastapi-duplicate-route

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-04-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

FastAPI does not allow defining two route handlers for the same HTTP method and path.

generic

中文

FastAPI 不允许为同一 HTTP 方法和路径定义两个路由处理程序。

Workarounds

  1. 100% success Remove or comment out the duplicate route
    # Remove the second @app.get('/items') decorator
  2. 100% success Use different HTTP methods or paths
    @app.get('/items')
    def list_items(): ...
    @app.post('/items')
    def create_item(): ...

Dead Ends

Common approaches that don't work:

  1. Renaming one function without changing the route 100% fail

    The route path and method are still duplicated.

  2. Adding a prefix to one route manually 80% fail

    If the prefix doesn't change the full path, duplication persists.