{"record":{"id":"f1c23210a3fc9364","repo":"fastapi/fastapi","slug":"the-dependency-call-name-has-a-scope-of-reque","errorCode":null,"errorMessage":"The dependency \"{call_name}\" has a scope of \"request\", it cannot depend on dependencies with scope \"function\".","messagePattern":"The dependency \"(.+?)\" has a scope of \"request\", it cannot depend on dependencies with scope \"function\"\\.","errorType":"exception","errorClass":"DependencyScopeError","httpStatus":null,"severity":"error","filePath":"fastapi/dependencies/utils.py","lineNumber":314,"sourceCode":"        param_details = analyze_param(\n            param_name=param_name,\n            annotation=param.annotation,\n            value=param.default,\n            is_path_param=is_path_param,\n        )\n        if param_details.depends is not None:\n            assert param_details.depends.dependency\n            if (\n                (\n                    _is_gen_callable(dependant.call)\n                    or _is_async_gen_callable(dependant.call)\n                )\n                and _get_computed_scope(dependant=dependant) == \"request\"\n                and param_details.depends.scope == \"function\"\n            ):\n                assert dependant.call\n                call_name = getattr(dependant.call, \"__name__\", \"<unnamed_callable>\")\n                raise DependencyScopeError(\n                    f'The dependency \"{call_name}\" has a scope of '\n                    '\"request\", it cannot depend on dependencies with scope \"function\".'\n                )\n            sub_own_oauth_scopes: list[str] = []\n            if isinstance(param_details.depends, params.Security):\n                if param_details.depends.scopes:\n                    sub_own_oauth_scopes = list(param_details.depends.scopes)\n            sub_dependant = get_dependant(\n                path=path,\n                call=param_details.depends.dependency,\n                name=param_name,\n                own_oauth_scopes=sub_own_oauth_scopes,\n                parent_oauth_scopes=current_scopes,\n                use_cache=param_details.depends.use_cache,\n                scope=param_details.depends.scope,\n            )\n            dependant.dependencies.append(sub_dependant)\n            continue","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/fastapi/fastapi/blob/a1fa70d4237d50aae6586a0d9b229df583463d21/fastapi/dependencies/utils.py#L296-L332","documentation":"FastAPI computes a dependency scope for each dependency (function or request). A generator dependency (one using yield) that must run for the whole request gets scope 'request'; if such a dependency declares a sub-dependency with scope 'function' (teardown finishes within the function scope), the lifetimes are incompatible and FastAPI raises DependencyScopeError at app setup time: a request-scoped dependency cannot depend on a function-scoped one, because its teardown could outlive the sub-dependency's.","triggerScenarios":"A dependency with yield marked or resolved as request-scoped (e.g. via dependencies with scope='request', or the default for yield dependencies under the new scoping rules) whose parameters use Depends() on a plain function or a function-scoped dependency. Raised while building the dependency tree, i.e. at include_router/app startup or first request depending on when routes are resolved.","commonSituations":"Upgrading FastAPI to a version introducing dependency scopes and mixing old-style sub-dependencies under request-scoped session dependencies; explicitly setting scope='request' on a yield dependency that consumes settings/config functions declared as function-scoped.","solutions":["Give the sub-dependency the wider scope: declare it as a generator with the same/request scope, or pass scope='request' when applicable","Move the function-scoped logic inline into the request-scoped dependency instead of using Depends","Review the dependency scopes documentation and align the tree so scopes never narrow downward","Update FastAPI and fastapi-related dependencies together; older snippets may predate the scope API"],"exampleFix":"# before\nasync def request_scoped_session():\n    yield session   # scope 'request'\n\ndef read_config(): ...  # scope 'function'\n\nasync def dep(cfg = Depends(read_config),   # DependencyScopeError\n              s = Depends(request_scoped_session)): ...\n\n# after: make the sub-dependency request-scoped too\nasync def read_config():\n    yield load_config()\n\nasync def dep(cfg = Depends(read_config),\n              s = Depends(request_scoped_session)): ...","handlingStrategy":"validation","validationCode":"from fastapi.dependencies.utils import get_dependant\n\ndef verify_scopes(call):\n    dep = get_dependant(path='/', call=call)\n    # walk sub-dependencies; a request-scoped gen dep must not use\n    # function-scoped sub-deps (FastAPI raises DependencyScopeError at setup)\n    return dep\n\n# or simply import the app in a startup check: misaligned scopes fail fast\nimport app  # raises DependencyScopeError at route resolution","typeGuard":null,"tryCatchPattern":"try:\n    from app.main import app\nexcept Exception as e:\n    if 'scope of' in str(e) and 'cannot depend' in str(e):\n        fix_dependency_scope(e)  # widen sub-dependency to request scope\n    else:\n        raise","preventionTips":["Design dependency trees monotonically: sub-dependency scope >= parent scope","Keep pure config helpers callable without Depends when used by request-scoped sessions","Add an import-the-app smoke test in CI to catch setup-time dependency errors"],"tags":["dependencies","yield","scope","startup","dependency-injection"],"backgroundTag":null,"analyzedSha":"a1fa70d4237d50aae6586a0d9b229df583463d21","analyzedAt":"2026-08-14T19:41:58.359Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}