Use FastAPI's Depends() anywhere - even outside FastAPI routes
Installation: pip install fastapi-injectable
Documentation: https://fastapi-injectable.readthedocs.io/en/latest/
Basic Example
from typing import Annotated
from fastapi import Depends
from fastapi_injectable import injectable
class Database:
def query(self) -> str:
return "data"
def get_db() -> Database:
return Database()
@injectable
def process_data(db: Annotated[Database, Depends(get_db)]) -> str:
return db.query()
result = process_data()
print(result)
Key Features
- Flexible Injection: Use decorators, function wrappers, or utility functions.
- Full Async Support: Works with both sync and async code.
- Resource Management: Built-in cleanup for dependencies.
- Dependency Caching: Optional caching for better performance.
- Graceful Shutdown: Automatic cleanup on program exit.
- Event Loop Management: Control the event loop to ensure the objects created by
fastapi-injectable
are executed in the right loop.
Overview
fastapi-injectable
is a lightweight package that enables seamless use of FastAPI's dependency injection system outside of route handlers. It solves a common pain point where developers need to reuse FastAPI dependencies in non-FastAPI contexts like CLI tools, background tasks, or scheduled jobs, allowing you to use FastAPI's dependency injection system anywhere!
Requirements
- Python
3.10
or higher
- FastAPI
0.112.4
or higher
Usage
fastapi-injectable
provides several powerful ways to use FastAPI's dependency injection outside of route handlers. Let's explore the key usage patterns with practical examples.
Basic Injection
The most basic way to use dependency injection is through the @injectable
decorator. This allows you to use FastAPI's Depends
in any function, not just route handlers.
from typing import Annotated
from fastapi import Depends
from fastapi_injectable.decorator import injectable
class Database:
def __init__(self) -> None:
pass
def query(self) -> str:
return "data"
def get_database():
return Database()
@injectable
def process_data(db: Annotated[Database, Depends(get_database)]):
return db.query()
result = process_data()
print(result)
Function-based Approach
The function-based approach provides an alternative way to use dependency injection without decorators. This can be useful when you need more flexibility or want to avoid modifying the original function.
Here's how to use it:
from fastapi_injectable.util import get_injected_obj
class Database:
def __init__(self) -> None:
pass
def query(self) -> str:
return "data"
def process_data(db: Annotated[Database, Depends(get_database)]):
return db.query()
result = get_injected_obj(process_data)
print(result)
Generator Dependencies with Cleanup
When working with generator dependencies that require cleanup (like database connections or file handles), fastapi-injectable
provides built-in support for controlling dependency lifecycles and proper resource management with error handling.
Here's an example showing how to work with generator dependencies:
from collections.abc import Generator
from fastapi_injectable.util import cleanup_all_exit_stacks, cleanup_exit_stack_of_func
from fastapi_injectable.exception import DependencyCleanupError
class Database:
def __init__(self) -> None:
self.closed = False
def query(self) -> str:
return "data"
def close(self) -> None:
self.closed = True
class Machine:
def __init__(self, db: Database) -> None:
self.db = db
def get_database() -> Generator[Database, None, None]:
db = Database()
yield db
db.close()
@injectable
def get_machine(db: Annotated[Database, Depends(get_database)]):
machine = Machine(db)
return machine
machine = get_machine()
assert machine.db.closed is False
await cleanup_exit_stack_of_func(get_machine)
assert machine.db.closed is True
try:
await cleanup_exit_stack_of_func(get_machine, raise_exception=True)
except DependencyCleanupError as e:
print(f"Cleanup failed: {e}")
assert machine.db.closed is False
await cleanup_all_exit_stacks()
assert machine.db.closed is True
Async Support
fastapi-injectable
provides full support for both synchronous and asynchronous dependencies, allowing you to mix and match them as needed. You can freely use async dependencies in sync functions and vice versa. For cases where you need to run async code in a synchronous context, we provide the run_coroutine_sync
utility function.
from collections.abc import AsyncGenerator
class AsyncDatabase:
def __init__(self) -> None:
self.closed = False
async def query(self) -> str:
return "data"
async def close(self) -> None:
self.closed = True
async def get_async_database() -> AsyncGenerator[AsyncDatabase, None]:
db = AsyncDatabase()
yield db
await db.close()
@injectable
async def async_process_data(db: Annotated[AsyncDatabase, Depends(get_async_database)]):
return await db.query()
result = await async_process_data()
print(result)
from fastapi_injectable.concurrency import run_coroutine_sync
result = run_coroutine_sync(async_process_data())
print(result)
Dependency Caching Control
By default, fastapi-injectable
caches dependency instances to improve performance and maintain consistency. This means when you request a dependency multiple times, you'll get the same instance back.
You can control this behavior using the use_cache
parameter in the @injectable
decorator:
use_cache=True
(default): Dependencies are cached and reused
use_cache=False
: New instances are created for each dependency request
Using use_cache=False
is particularly useful when:
- You need fresh instances for each request
- You want to avoid sharing state between different parts of your application
- You're dealing with stateful dependencies that shouldn't be reused
from typing import Annotated
from fastapi import Depends
from fastapi_injectable.decorator import injectable
class Mayor:
pass
class Capital:
def __init__(self, mayor: Mayor) -> None:
self.mayor = mayor
class Country:
def __init__(self, capital: Capital) -> None:
self.capital = capital
def get_mayor() -> Mayor:
return Mayor()
def get_capital(mayor: Annotated[Mayor, Depends(get_mayor)]) -> Capital:
return Capital(mayor)
@injectable
def get_country(capital: Annotated[Capital, Depends(get_capital)]) -> Country:
return Country(capital)
country_1 = get_country()
country_2 = get_country()
country_3 = get_country()
assert country_1.capital is country_2.capital is country_3.capital
assert country_1.capital.mayor is country_2.capital.mayor is country_3.capital.mayor
@injectable(use_cache=False)
def get_country(capital: Annotated[Capital, Depends(get_capital)]) -> Country:
return Country(capital)
country_1 = get_country()
country_2 = get_country()
country_3 = get_country()
assert country_1.capital is not country_2.capital is not country_3.capital
assert country_1.capital.mayor is not country_2.capital.mayor is not country_3.capital.mayor
Type Hinting
fastapi-injectable
will prepare the dependency objects of injected functions for you, but static type checkers like mypy
haven't known about the dependency object existence since they are normally injected via Annotated[Type, Depends(get_dependency_func)]
, when using this kind of expression, static type checkers will complain if you don't explicitly provide the dependency object when using the function, example error codes (call-arg).
@injectable
def get_country(capital: Annotated[Capital, Depends(get_capital)]) -> Country:
return Country(capital)
country = get_country()
To make the mypy
happy, you can enable the fastapi-injectable.mypy
plugin in your mypy.ini
file, or add fastapi_injectable.mypy
to your pyproject.toml
file.
[tool.mypy]
plugins = ["fastapi_injectable.mypy"]
@injectable
def get_country(capital: Annotated[Capital, Depends(get_capital)]) -> Country:
return Country(capital)
country = get_country()
Event Loop Management
fastapi-injectable
includes a powerful loop management system to handle asynchronous code execution in different contexts. This is particularly useful when working with async code in synchronous environments or when you need controlled event loop execution.
from fastapi_injectable.concurrency import loop_manager, run_coroutine_sync
loop_manager.set_loop_strategy("isolated")
loop = loop_manager.get_loop()
result = run_coroutine_sync(async_process_data())
Loop strategies explained:
-
current
(default): Uses the current thread's event loop. This is the simplest option and meets most needs.
- Limitation: Fails if no loop is running in the current thread.
- Perfect when your code runs in synchronous functions within the main thread with a runnable event loop.
import asyncio
my_loop = asyncio.get_event_loop()
loop_manager.set_loop_strategy("current")
assert my_loop is loop_manager.get_loop()
result = run_coroutine_sync(async_process_data())
-
isolated
: Creates a separate isolated loop.
- Benefit: Works even when no loop is running in the current thread.
- Ideal when you need control over the loop lifecycle or need to ensure all injected objects come from the same event loop (important for objects like
aiohttp.ClientSession
that must execute in the same loop where they were instantiated).
import asyncio
from fastapi_injectable import get_injected_obj
async def get_aiohttp_session():
return aiohttp.ClientSession()
loop_manager.set_loop_strategy("isolated")
aiohttp_session = get_injected_obj(get_aiohttp_session)
original_loop = asyncio.get_event_loop()
loop = loop_manager.get_loop()
assert original_loop is not loop
original_loop.run_until_complete(aiohttp_session.get("https://www.google.com"))
loop.run_until_complete(aiohttp_session.get("https://www.google.com"))
-
background_thread
: Runs a dedicated background thread with its own event loop.
- Best for: Long-running applications where you need to run async code from sync contexts.
- Benefit: Allows async code to run from any thread without blocking.
- Perfect when you're uncertain about your environment's event loop availability and don't use objects that assume they run in the same event loop.
loop_manager.set_loop_strategy("background_thread")
result = run_coroutine_sync(async_process_data())
Logging Configuration
fastapi-injectable
provides a simple way to configure logging for the package. This is useful for debugging or monitoring the package's behavior.
import logging
from fastapi_injectable import configure_logging
configure_logging(level=logging.DEBUG)
configure_logging(
level=logging.INFO,
format_="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
file_handler = logging.FileHandler("fastapi_injectable.log")
configure_logging(level=logging.WARNING, handler=file_handler)
Graceful Shutdown
If you want to ensure proper cleanup when the program exits, you can register cleanup functions with error handling:
import signal
from fastapi_injectable import setup_graceful_shutdown
from fastapi_injectable.exception import DependencyCleanupError
setup_graceful_shutdown()
setup_graceful_shutdown(raise_exception=True)
setup_graceful_shutdown(
signals=[signal.SIGTERM],
raise_exception=True
)
App Registration for State Access
If your dependencies need access to the FastAPI app state (like database connections or other services), you can register your app with fastapi-injectable
:
from fastapi import FastAPI, Request, Depends
from fastapi_injectable import injectable, register_app
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession
def get_db_engine(*, request: Request) -> AsyncEngine:
return request.app.state.db_engine
DBEngine = Annotated[AsyncEngine, Depends(get_db_engine)]
async def get_db(*, db_engine: DBEngine) -> AsyncIterator[AsyncSession]:
session = async_sessionmaker(db_engine)
async with session.begin() as session:
yield session
DB = Annotated[AsyncSession, Depends(get_db)]
@contextlib.asynccontextmanager
async def lifespan(app: FastAPI):
await register_app(app)
app.state.db_engine = create_async_engine("postgresql+asyncpg://...")
yield
await app.state.db_engine.dispose()
app = FastAPI(lifespan=lifespan)
@injectable
async def process_data(db: DB) -> str:
result = await db.execute(...)
return result
result = await process_data()
This is particularly useful when:
- Your dependencies need access to shared services in
app.state
- You're using third-party libraries that call your code internally
- You want to maintain a single source of truth for long-running services
Advanced Scenarios
If the basic examples don't cover your needs, check out our test files - they're basically a cookbook of real-world scenarios:
1. test_injectable.py
- Shows all possible combinations of:
- Sync/async functions
- Decorator vs function wrapping
- Caching vs no caching
- Resource cleanup
- Generator dependencies
- Mixed sync/async dependencies
- Multiple dependency chains
These test cases mirror common development patterns you'll encounter. They show how to handle complex dependency trees, resource management, and mixing sync/async code - stuff you'll actually use in production.
The test files are written to be self-documenting, so browsing through them will give you practical examples for most scenarios you'll face in your codebase.
Real-world Examples
We've collected some real-world examples of using fastapi-injectable
in various scenarios:
This example demonstrates several key patterns for using dependency injection in background workers:
Please refer to the Real-world Examples for more details.
Frequently Asked Questions
Why would I need this package?
A: If your project heavily relies on FastAPI's Depends()
as the sole DI system and you don't want to introduce additional DI packages (like Dependency Injector or FastDepends), fastapi-injectable
is your friend.
It allows you to reuse your existing FastAPI built-in DI system anywhere, without the need to refactor your entire codebase or maintain multiple DI systems.
Life is short, keep it simple!
Why not directly use other DI packages like Dependency Injector or FastDepends?
A: You absolutely can if your situation allows you to:
- Modify large amounts of existing code that uses
Depends()
- Maintain multiple DI systems in your project
fastapi-injectable
focuses solely on extending FastAPI's built-in Depends()
beyond routes. We're not trying to be another DI system - we're making the existing one more useful!
For projects with hundreds of dependency functions (especially with nested dependencies), this approach is more intuitive and requires minimal changes to your existing code.
Choose what works best for you!
Can I use it with existing FastAPI dependencies?
A: Absolutely! That's exactly what this package was built for! fastapi-injectable
was created to seamlessly work with FastAPI's dependency injection system, allowing you to reuse your existing Depends()
code anywhere - not just in routes.
Focus on what matters instead of worrying about how to get your existing dependencies outside of FastAPI routes!
Does it work with all FastAPI dependency types?
A: Yes! It supports:
- Regular dependencies
- Generator dependencies (with cleanup utility functions)
- Async dependencies
- Sync dependencies
- Nested dependencies (dependencies with sub-dependencies)
What happens to dependency cleanup in long-running processes?
A: You have three options:
- Manual cleanup per function:
await cleanup_exit_stack_of_func(your_func)
- Cleanup everything:
await cleanup_all_exit_stacks()
- Automatic cleanup on shutdown:
setup_graceful_shutdown()
Can I mix sync and async dependencies?
A: Yes! You can freely mix them. For running async code in sync contexts, use the provided run_coroutine_sync()
utility.
Are type hints fully supported for injectable()
and get_injected_obj()
?
A: Currently, type hint support is available if you are using mypy
as your static type checker, you can enable the fastapi-injectable.mypy
plugin in your mypy.ini
file, or add fastapi_injectable.mypy
to your pyproject.toml
file, see Type Hinting for more details.
How does caching work?
A: By default, dependencies are cached like in FastAPI routes. You can disable caching with @injectable(use_cache=False)
if you need fresh instances.
Is it production-ready?
A: Yes! The package has:
- 100% test coverage
- Type checking with
mypy
- Comprehensive error handling
- Production use cases documented
Contributing
Contributions are very welcome.
To learn more, see the Contributor Guide.
License
Distributed under the terms of the MIT license,
fastapi-injectable
is free and open source software.
Issues
If you encounter any problems,
please file an issue along with a detailed description.
Credits
Related Issue & Discussion
Bonus
My blog posts about the prototype of this project: