python config_error ai_generated true

AssertionError: Duplicated path operation: GET /items

ID: python/fastapi-path-operation-duplicate

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.9 active
3.10 active

Root Cause

FastAPI 中为同一个路径和方法定义了多个路径操作函数

generic

中文

FastAPI 中为同一个路径和方法定义了多个路径操作函数

Workarounds

  1. 95% success 合并重复的路径操作
    def get_items():
        # 合并逻辑
        pass
    @app.get('/items')
    @app.get('/items/')  # 使用路径别名
  2. 90% success 使用不同的路径
    @app.get('/items')
    @app.get('/products')  # 改为不同路径

Dead Ends

Common approaches that don't work:

  1. 删除其中一个函数但保留逻辑 70% fail

    功能丢失

  2. 使用不同的 HTTP 方法 60% fail

    可能不符合 RESTful 设计