python
config_error
ai_generated
true
AssertionError: Duplicate route: /items
ID: python/fastapi-duplicate-route
80%Fix Rate
85%Confidence
0Evidence
2024-04-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
FastAPI does not allow defining two route handlers for the same HTTP method and path.
generic中文
FastAPI 不允许为同一 HTTP 方法和路径定义两个路由处理程序。
Workarounds
-
100% success Remove or comment out the duplicate route
# Remove the second @app.get('/items') decorator -
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:
-
Renaming one function without changing the route
100% fail
The route path and method are still duplicated.
-
Adding a prefix to one route manually
80% fail
If the prefix doesn't change the full path, duplication persists.