# RuntimeError: 依赖缓存键必须是可哈希的

- **ID:** `python/fastapi-runtimeerror-dependency-cache-key-must-be-hashable`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

使用可变对象（如列表或字典）作为 FastAPI 尝试缓存的依赖参数。

## 版本兼容性

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

## 解决方案

1. **Use immutable types like tuple instead of list.** (90% 成功率)
   ```
   from typing import Tuple
async def dependency(items: Tuple[int, ...] = (1, 2, 3)):
    return items
   ```
2. **Use Pydantic models for complex parameters.** (95% 成功率)
   ```
   from pydantic import BaseModel
class FilterParams(BaseModel):
    tags: list[str] = []

async def dependency(params: FilterParams):
    return params
   ```

## 无效尝试

- **Using a list as a default value for a dependency.** — Lists are mutable and unhashable, causing caching issues. (80% 失败率)
- **Using a dict as a query parameter.** — Query parameters must be hashable; dicts are not. (70% 失败率)
