python config_error ai_generated true

AssertionError: allow_origins must be a list or tuple of strings

ID: python/fastapi-cors-middleware-misconfiguration

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

The CORS middleware expects allow_origins as a list, but a string or other type is provided.

generic

中文

CORS 中间件期望 allow_origins 为列表,但提供了字符串或其他类型。

Workarounds

  1. 100% success
    Use a list: allow_origins=["*"] or allow_origins=["https://example.com"]
  2. 95% success
    Use allow_origin_regex for pattern matching: allow_origin_regex="https?://.*"

Dead Ends

Common approaches that don't work:

  1. 100% fail

    Starlette's CORSMiddleware requires a list, even for wildcard.

  2. 90% fail

    The assertion checks each element is a string, so non-strings cause failure.