python
config_error
ai_generated
true
AssertionError: 'allow_origins' must be a list of strings or a single string
ID: python/fastapi-cors-middleware-origin
80%Fix Rate
85%Confidence
0Evidence
2025-01-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Passing an invalid type to CORSMiddleware allow_origins, such as a set or tuple.
generic中文
向 CORSMiddleware 的 allow_origins 传递了无效类型,如集合或元组。
Workarounds
-
100% success
Use a list: allow_origins=['*'] or allow_origins=['http://example.com']
-
80% success
If you need dynamic origins, use a function that returns a list
Dead Ends
Common approaches that don't work:
-
90% fail
FastAPI expects a list; set is not accepted.
-
30% fail
It's allowed but may cause issues with credentials; the assertion is for non-string types.