python
type_error
ai_generated
true
RuntimeError: 依赖缓存键必须是可哈希的
RuntimeError: Dependency cache key must be hashable
ID: python/fastapi-runtimeerror-dependency-cache-key-must-be-hashable
80%修复率
84%置信度
0证据数
2024-08-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
使用可变对象(如列表或字典)作为 FastAPI 尝试缓存的依赖参数。
English
Using a mutable object (like a list or dict) as a dependency parameter that FastAPI tries to cache.
解决方案
-
90% 成功率 Use immutable types like tuple instead of list.
from typing import Tuple async def dependency(items: Tuple[int, ...] = (1, 2, 3)): return items -
95% 成功率 Use Pydantic models for complex parameters.
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.
80% 失败
Lists are mutable and unhashable, causing caching issues.
-
Using a dict as a query parameter.
70% 失败
Query parameters must be hashable; dicts are not.