python config_error ai_generated true

断言错误:'allow_origins' 必须是字符串列表或单个字符串

AssertionError: 'allow_origins' must be a list of strings or a single string

ID: python/fastapi-cors-middleware-origin

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2025-01-15首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 90% 失败

    FastAPI expects a list; set is not accepted.

  2. 30% 失败

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