You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar question and didn't find it.
I searched the FastAPI documentation, with the integrated search.
I already searched in Google "How to X in FastAPI" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to FastAPI but to Pydantic.
I already checked if it is not related to FastAPI but to Swagger UI.
I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
I commit to help with one of those options 👆
Example Code
classEnvDependency:
def__init__(self, env_var: str):
self.env_var=env_vardef__call__(self):
importosreturnos.getenv(self.env_var)
classSomeDependency:
def__init__(self, name: str):
self.name=namedef__call__(self):
returnself.nameADependency=Annotated[Callable[[], None], Depends(SomeDependency("a"))]
BDependency=Annotated[Callable[[], None], Depends(SomeDependency("b"))]
defget_dep_a(dep: ADependency):
return"A"defget_dep_b(dep: BDependency):
return"B"importosos.environ["CONFIG"] ="a"# The problem with this setup is that I have to anticipate ahead of time that# I will either need dependency A or dependency B# This is roughly what we are already doingdefconfigurable_dependency(
config_value: Annotated[str, Depends(EnvDependency("CONFIG"))],
a_dep: ADependency,
b_dep: BDependency,
):
print("config_value", config_value)
ifconfig_value=="a":
returnget_dep_a(a_dep)
else:
returnget_dep_b(b_dep)
defconfigurable_dependency_ideal(
config_value: Annotated[str, Depends(EnvDependency("CONFIG"))],
):
...
# dynamically resolve the dependency(using e.g. importlib) at runtime# and just return the dependency, letting FastAPI resolve it furtherifconfig_value=="a":
returnADependencyelse:
returnBDependencyConfigurableDependency=Annotated[
Callable[[], None], Depends(configurable_dependency)
]
Description
Hi.
We are trying to build a modular application which can be deployed for different customers in different configurations. The idea is that we will have a library of various dependencies for each different customer and situation. Customer 1 may require dependencies A, B and C and customer 2 may only require dependency C.
A developer would build the customer-specific module which would use the dependencies it requires, and then this module would be loaded and created as a dependency at runtime based on, say, environment variables.
The problem with our current approach is that we will always need to know all the dependencies that may be required ahead of time, if I'm not mistaken. I've attempted to demonstrate this in the example code.
Ideally, it would be possible for us to have a dependency which itself returns a Depends(dynamic_dependency), and then FastAPI would realize that it needs to resolve this dependency further and automatically resolve all the parameters to dynamic_dependency(since they would all be Annotated[something, Depends(..)]).
I've pored over the docs and also searched online as well as in the repo for something similar, but I've not found something exactly like this. Is there a way to accomplish this? Can I perhaps manually invoke the FastAPI dependency resolution functionality somehow?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
Hi.
We are trying to build a modular application which can be deployed for different customers in different configurations. The idea is that we will have a library of various dependencies for each different customer and situation. Customer 1 may require dependencies A, B and C and customer 2 may only require dependency C.
A developer would build the customer-specific module which would use the dependencies it requires, and then this module would be loaded and created as a dependency at runtime based on, say, environment variables.
The problem with our current approach is that we will always need to know all the dependencies that may be required ahead of time, if I'm not mistaken. I've attempted to demonstrate this in the example code.
Ideally, it would be possible for us to have a dependency which itself returns a
Depends(dynamic_dependency)
, and then FastAPI would realize that it needs to resolve this dependency further and automatically resolve all the parameters todynamic_dependency
(since they would all beAnnotated[something, Depends(..)]
).I've pored over the docs and also searched online as well as in the repo for something similar, but I've not found something exactly like this. Is there a way to accomplish this? Can I perhaps manually invoke the FastAPI dependency resolution functionality somehow?
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.104.1
Pydantic Version
1.10.13
Python Version
3.11.7
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions