python runtime_error ai_generated true

RuntimeError: Dependency cache key error

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2025-08-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

FastAPI's dependency injection cache has a conflict, often due to using mutable default arguments in dependency functions.

generic

中文

FastAPI的依赖注入缓存存在冲突,通常是由于在依赖函数中使用了可变默认参数。

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. Ignoring mutable defaults in dependencies 70% fail

    Mutable defaults are shared across requests, causing cache issues.

  2. Using global variables in dependencies 60% fail

    Global state can cause cache key collisions.