# FastAPI 错误：响应字段的参数无效

- **ID:** `python/fastapi-dependency-injection-error`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

FastAPI 依赖注入中的响应模型参数类型不匹配

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.9 | active | — | — |
| 3.10 | active | — | — |

## 解决方案

1. **检查并修正依赖函数的返回类型** (90% 成功率)
   ```
   def get_db() -> Session:
    return Session()
@app.get('/items', response_model=List[Item])
def get_items(db: Session = Depends(get_db)):
    ...
   ```
2. **使用正确的依赖注入语法** (95% 成功率)
   ```
   from fastapi import Depends
@app.get('/')
def root(db: Session = Depends(get_db)):
    ...
   ```

## 无效尝试

- **删除响应模型参数但保留依赖** — 依赖注入仍然会解析，但响应格式可能错误 (60% 失败率)
- **将所有参数改为 Any 类型** — 失去了类型安全，可能隐藏错误 (70% 失败率)
