python runtime_error ai_generated true

运行时错误:依赖缓存键错误

RuntimeError: Dependency cache key error

ID: python/fastapi-runtimeerror-dependency-cache-key-error

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 90% 成功率 Use immutable defaults or None
    def dependency(items: list = None):
        if items is None:
            items = []
  2. 50% 成功率 Clear dependency cache manually
    from fastapi import Depends
    # Not recommended; better to fix dependency function

无效尝试

常见但无效的做法:

  1. Ignoring mutable defaults in dependencies 70% 失败

    Mutable defaults are shared across requests, causing cache issues.

  2. Using global variables in dependencies 60% 失败

    Global state can cause cache key collisions.