{"record":{"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/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/fastapi/dependencies/utils.py#L296-L332","documentation":"DependencyScopeError (a FastAPIError/RuntimeError) raised while building the dependency tree in get_dependant. It fires when a dependency whose computed scope is \"request\" (typically a generator/async-generator dependency, or one declared with Depends(scope=\"request\")) lists a sub-dependency explicitly scoped to \"function\". A request-scoped dependency outlives the per-function teardown, so depending on something that is torn down at function scope would close resources too early; FastAPI forbids the combination at app construction.","triggerScenarios":"Declaring a yielding dependency at request scope (e.g. Depends(get_db, scope=\"request\") where get_db is a generator) and having it depend on another Depends(..., scope=\"function\"). The check at dependencies/utils.py:304-317 raises when _get_computed_scope(dependant) == \"request\" and param_details.depends.scope == \"function\".","commonSituations":"Mixing FastAPI dependency scopes (newer scope= feature); a request-scoped DB session dependency that Depends on a function-scoped settings/auth dependency; upgrading to a FastAPI version that enforces scope rules on previously-loose code.","solutions":["Make the sub-dependency request-scoped too: Depends(get_thing, scope=\"request\"), so scopes are compatible.","Or remove the explicit scope from the parent so its computed scope becomes \"function\".","Reorder so the function-scoped dependency is injected at the endpoint level, not under the request-scoped one.","Audit all Depends(...) calls in the chain and align their scope= values."],"exampleFix":"# before\ndef get_auth(token: str = Depends(oauth2)) -> User: ...  # scope=\"function\" (default)\n\ndef get_db(db=Depends(get_auth, scope=\"request\")):  # request depends on function\n    yield db\n# -> DependencyScopeError\n\n# after\ndef get_auth(token: str = Depends(oauth2, scope=\"request\")) -> User: ...\n\ndef get_db(db=Depends(get_auth, scope=\"request\")):\n    yield db","handlingStrategy":"validation","validationCode":"# Walk your dependency graph at startup and reject invalid scope mixes\nfrom fastapi import Depends\n\ndef get_depends_chain(func):\n    import inspect\n    deps = []\n    for p in inspect.signature(func).parameters.values():\n        if isinstance(p.default, Depends):\n            deps.append((p.name, getattr(p.default, \"scope\", None)))\n    return deps\n\n# rule: a request-scoped parent must not depend on a function-scoped child\nfor parent_name, parent_scope in get_depends_chain(my_endpoint):\n    assert parent_scope != \"request\" or not has_function_scope_child(parent_name), (\n        f\"{parent_name} is request-scoped but depends on a function-scoped dep\"\n    )","typeGuard":null,"tryCatchPattern":"from fastapi.exceptions import DependencyScopeError\ntry:\n    app.include_router(router)  # dependency tree is built here\nexcept DependencyScopeError as e:\n    print(\"scope mismatch in dependency tree:\", e)\n    raise","preventionTips":["Keep dependency scopes consistent across a chain (all request or all function).","Avoid mixing Depends(..., scope=...) values without auditing the whole subtree.","Inject function-scoped deps at the endpoint, not under a request-scoped parent.","Add a startup test that imports the app to catch scope errors in CI."],"tags":["dependencies","scope","dependency-injection","fastapi","app-startup"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}