python
runtime_error
ai_generated
true
运行时错误:依赖缓存键错误
RuntimeError: Dependency cache key error
ID: python/fastapi-runtimeerror-dependency-cache-key-error
80%修复率
83%置信度
0证据数
2025-08-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
FastAPI的依赖注入缓存存在冲突,通常是由于在依赖函数中使用了可变默认参数。
English
FastAPI's dependency injection cache has a conflict, often due to using mutable default arguments in dependency functions.
解决方案
-
90% 成功率 Use immutable defaults or None
def dependency(items: list = None): if items is None: items = [] -
50% 成功率 Clear dependency cache manually
from fastapi import Depends # Not recommended; better to fix dependency function
无效尝试
常见但无效的做法:
-
Ignoring mutable defaults in dependencies
70% 失败
Mutable defaults are shared across requests, causing cache issues.
-
Using global variables in dependencies
60% 失败
Global state can cause cache key collisions.