# 类型错误：协程对象不可调用

- **ID:** `python/fastapi-middleware-missing-await`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

FastAPI中间件函数定义为异步，但在调用下一个中间件或请求处理器时未使用await。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **Ensure middleware function uses await when calling call_next** (95% 成功率)
   ```
   @app.middleware('http')
async def add_process_time_header(request: Request, call_next):
    response = await call_next(request)
    return response
   ```
2. **Use synchronous middleware with proper decorator** (70% 成功率)
   ```
   @app.middleware('http')
def sync_middleware(request: Request, call_next):
    response = call_next(request)
    return response
   ```

## 无效尝试

- **Adding @app.middleware('http') without async def** — FastAPI expects an async function for middleware; synchronous functions cause different errors. (80% 失败率)
- **Removing async keyword from middleware function** — Middleware becomes synchronous but FastAPI still treats it as async, leading to runtime issues. (75% 失败率)
