python config_error ai_generated true

ValueError: Duplicate path operation: GET /items/{item_id}

ID: python/fastapi-path-operation-conflict

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

FastAPI中定义了多个相同HTTP方法和路径的路由。

generic

中文

FastAPI中定义了多个相同HTTP方法和路径的路由。

Workarounds

  1. 95% success 确保每个路径和方法组合唯一
    @app.get('/items/{item_id}') async def read_item(item_id: int): return {'item_id': item_id}; @app.post('/items/{item_id}') async def update_item(item_id: int): return {'updated': item_id}
  2. 90% success 使用不同的路径或添加版本前缀
    @app.get('/v1/items/{item_id}') async def read_item_v1(item_id: int): return {'item_id': item_id}

Dead Ends

Common approaches that don't work:

  1. 尝试覆盖路由而不删除旧路由 90% fail

    FastAPI不允许重复注册。

  2. 使用不同的函数名但相同的路径和方法 80% fail

    路径和方法是唯一标识。