# AssertionError: Middleware 'CORSMiddleware' must be added before 'SessionMiddleware'

- **ID:** `python/starlette-middleware-order-error`
- **Domain:** python
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Starlette 中间件的添加顺序不正确，违反了依赖关系

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.10 | active | — | — |
| 3.11 | active | — | — |

## Workarounds

1. **按照正确顺序添加中间件** (95% success)
   ```
   app.add_middleware(CORSMiddleware, allow_origins=['*'])
app.add_middleware(SessionMiddleware, secret_key='key')
   ```
2. **查阅文档确认中间件依赖顺序** (90% success)
   ```
   阅读 Starlette 文档中关于中间件顺序的说明
   ```

## Dead Ends

- **删除 SessionMiddleware 并单独使用 CORS** — 会话功能丢失 (60% fail)
- **使用装饰器顺序而非 add_middleware** — 装饰器顺序也可能导致相同问题 (80% fail)
