{"id":"34ae2121b11055fb","repo":"tiangolo/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/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/fastapi/dependencies/utils.py#L296-L332","documentation":"DependencyScopeError raised at dependencies/utils.py:314 during dependency-graph construction. FastAPI forbids a 'request'-scoped dependency that is a generator/async-generator from depending on a 'function'-scoped sub-dependency, because function-scoped deps are resolved per call while request-scoped generator deps live for the whole request — mixing them would tear down the sub-dependency at the wrong time. The check fires in get_dependant when _get_computed_scope(dependant)=='request' and param_details.depends.scope=='function' for a gen/async-gen callable.","triggerScenarios":"A request-scoped dependency (Depends(..., scope='request')) that is itself a generator (yield), depending via Depends() on another dependency declared with scope='function' (the default). The error is raised while building the route's dependency tree, so it surfaces at app-import/startup, not at request time.","commonSituations":"Adding scope='request' to a session/get_db generator that itself Depends on a function-scoped helper; upgrading FastAPI and adopting explicit scopes inconsistently; refactoring a shared dependency to generator form without bumping its dependents' scopes.","solutions":["Make the sub-dependency request-scoped too: Depends(..., scope='request') so both share the same lifecycle.","Or remove the generator (yield) from the outer dependency so it can be function-scoped and depend on function-scoped deps.","Restructure so the function-scoped dependency is not nested under the request-scoped generator — call it from the path operation directly.","Audit the dependency tree and align scopes top-down (request deps may only depend on request-scoped deps)."],"exampleFix":"# before\ndef get_db():\n    with Session() as s:\n        yield s\ndef settings = Depends(get_config, scope='function')\ndef session = Depends(get_db, scope='request')  # depends on settings indirectly -> error\n# after\ndef get_db():\n    with Session() as s:\n        yield s\ndef session = Depends(get_db, scope='request')\ndef settings = Depends(get_config, scope='request')  # align scope","handlingStrategy":"validation","validationCode":"def assert_scope_compatible(outer_scope: str, outer_is_gen: bool, sub_scope: str):\n    if outer_scope == 'request' and outer_is_gen and sub_scope == 'function':\n        raise ValueError('request-scoped generator cannot depend on function-scoped dep')","typeGuard":null,"tryCatchPattern":"from fastapi.exceptions import DependencyScopeError\ntry:\n    app = FastAPI()\n    app.include_router(router)  # builds dependency trees\nexcept DependencyScopeError as e:\n    print('align dependency scopes:', e)","preventionTips":["Keep dependency scopes consistent: request-scoped generators may only depend on request-scoped deps.","Avoid mixing scope='function' sub-deps under scope='request' generators.","Run app import in CI to catch this at build time, not request time.","Document each dependency's intended scope in its docstring."],"tags":["dependencies","scope","di","generator","fastapi"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}