python config_error ai_generated true

ValueError: Duplicate route path: /items

ID: python/fastapi-valueerror-duplicate-route-path

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Two route decorators define the same path without differentiating methods.

generic

中文

两个路由装饰器定义了相同的路径而没有区分方法。

Workarounds

  1. 95% success Use different HTTP methods for same path
    @app.get('/items')
    async def list_items(): pass
    @app.post('/items')
    async def create_item(): pass
  2. 90% success Use different paths
    @app.get('/items')
    async def list_items(): pass
    @app.get('/items/{item_id}')
    async def get_item(item_id: int): pass

Dead Ends

Common approaches that don't work:

  1. Removing one route entirely 50% fail

    May lose needed functionality.

  2. Changing function name only 70% fail

    Path remains duplicate.