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

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-01-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Passing an invalid type to CORSMiddleware allow_origins, such as a set or tuple.

generic

中文

向 CORSMiddleware 的 allow_origins 传递了无效类型,如集合或元组。

Workarounds

  1. 100% success
    Use a list: allow_origins=['*'] or allow_origins=['http://example.com']
  2. 80% success
    If you need dynamic origins, use a function that returns a list

Dead Ends

Common approaches that don't work:

  1. 90% fail

    FastAPI expects a list; set is not accepted.

  2. 30% fail

    It's allowed but may cause issues with credentials; the assertion is for non-string types.