Home / Function/ solve_dependencies() — fastapi Function Reference

solve_dependencies() — fastapi Function Reference

Architecture documentation for the solve_dependencies() function in utils.py from the fastapi codebase.

Entity Profile

Dependency Diagram

graph TD
  fc3220b9_b8bc_99f6_605f_911723c78183["solve_dependencies()"]
  9e602cbf_3139_86ae_5666_97b8806942de["utils.py"]
  fc3220b9_b8bc_99f6_605f_911723c78183 -->|defined in| 9e602cbf_3139_86ae_5666_97b8806942de
  ad8d62d9_53da_514b_c06f_53293eb1e5ae["get_request_handler()"]
  ad8d62d9_53da_514b_c06f_53293eb1e5ae -->|calls| fc3220b9_b8bc_99f6_605f_911723c78183
  a0d87950_5de5_3ab2_72f7_0a20a6e00434["get_websocket_app()"]
  a0d87950_5de5_3ab2_72f7_0a20a6e00434 -->|calls| fc3220b9_b8bc_99f6_605f_911723c78183
  097a7e41_8095_d61f_b849_128514c58040["get_dependant()"]
  fc3220b9_b8bc_99f6_605f_911723c78183 -->|calls| 097a7e41_8095_d61f_b849_128514c58040
  234ce98e_561d_d168_f588_50a82002939a["_solve_generator()"]
  fc3220b9_b8bc_99f6_605f_911723c78183 -->|calls| 234ce98e_561d_d168_f588_50a82002939a
  0b6a77bd_97c7_05f8_56cd_a3e6fa8af276["request_params_to_args()"]
  fc3220b9_b8bc_99f6_605f_911723c78183 -->|calls| 0b6a77bd_97c7_05f8_56cd_a3e6fa8af276
  b8125a22_3ce8_c209_1d2b_1aaf5de6f8b2["request_body_to_args()"]
  fc3220b9_b8bc_99f6_605f_911723c78183 -->|calls| b8125a22_3ce8_c209_1d2b_1aaf5de6f8b2
  style fc3220b9_b8bc_99f6_605f_911723c78183 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

fastapi/dependencies/utils.py lines 568–705

async def solve_dependencies(
    *,
    request: Union[Request, WebSocket],
    dependant: Dependant,
    body: Optional[Union[dict[str, Any], FormData]] = None,
    background_tasks: Optional[StarletteBackgroundTasks] = None,
    response: Optional[Response] = None,
    dependency_overrides_provider: Optional[Any] = None,
    dependency_cache: Optional[dict[DependencyCacheKey, Any]] = None,
    # TODO: remove this parameter later, no longer used, not removing it yet as some
    # people might be monkey patching this function (although that's not supported)
    async_exit_stack: AsyncExitStack,
    embed_body_fields: bool,
) -> SolvedDependency:
    request_astack = request.scope.get("fastapi_inner_astack")
    assert isinstance(request_astack, AsyncExitStack), (
        "fastapi_inner_astack not found in request scope"
    )
    function_astack = request.scope.get("fastapi_function_astack")
    assert isinstance(function_astack, AsyncExitStack), (
        "fastapi_function_astack not found in request scope"
    )
    values: dict[str, Any] = {}
    errors: list[Any] = []
    if response is None:
        response = Response()
        del response.headers["content-length"]
        response.status_code = None  # type: ignore
    if dependency_cache is None:
        dependency_cache = {}
    for sub_dependant in dependant.dependencies:
        sub_dependant.call = cast(Callable[..., Any], sub_dependant.call)
        call = sub_dependant.call
        use_sub_dependant = sub_dependant
        if (
            dependency_overrides_provider
            and dependency_overrides_provider.dependency_overrides
        ):
            original_call = sub_dependant.call
            call = getattr(
                dependency_overrides_provider, "dependency_overrides", {}
            ).get(original_call, original_call)
            use_path: str = sub_dependant.path  # type: ignore
            use_sub_dependant = get_dependant(
                path=use_path,
                call=call,
                name=sub_dependant.name,
                parent_oauth_scopes=sub_dependant.oauth_scopes,
                scope=sub_dependant.scope,
            )

        solved_result = await solve_dependencies(
            request=request,
            dependant=use_sub_dependant,
            body=body,
            background_tasks=background_tasks,
            response=response,
            dependency_overrides_provider=dependency_overrides_provider,
            dependency_cache=dependency_cache,
            async_exit_stack=async_exit_stack,
            embed_body_fields=embed_body_fields,
        )
        background_tasks = solved_result.background_tasks
        if solved_result.errors:
            errors.extend(solved_result.errors)
            continue
        if sub_dependant.use_cache and sub_dependant.cache_key in dependency_cache:
            solved = dependency_cache[sub_dependant.cache_key]
        elif (
            use_sub_dependant.is_gen_callable or use_sub_dependant.is_async_gen_callable
        ):
            use_astack = request_astack
            if sub_dependant.scope == "function":
                use_astack = function_astack
            solved = await _solve_generator(
                dependant=use_sub_dependant,
                stack=use_astack,
                sub_values=solved_result.values,
            )
        elif use_sub_dependant.is_coroutine_callable:
            solved = await call(**solved_result.values)

Subdomains

Frequently Asked Questions

What does solve_dependencies() do?
solve_dependencies() is a function in the fastapi codebase, defined in fastapi/dependencies/utils.py.
Where is solve_dependencies() defined?
solve_dependencies() is defined in fastapi/dependencies/utils.py at line 568.
What does solve_dependencies() call?
solve_dependencies() calls 4 function(s): _solve_generator, get_dependant, request_body_to_args, request_params_to_args.
What calls solve_dependencies()?
solve_dependencies() is called by 2 function(s): get_request_handler, get_websocket_app.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free