A seamless solution for using FastAPI's dependency injection system outside of route handlers, enabling reuse in CLI tools, background tasks, and other non-HTTP contexts.
FastAPI Injectable is a Python library that enables developers to use FastAPI's `Depends()` dependency injection system outside of HTTP route handlers. It solves the problem of code duplication by allowing the same dependency logic to be reused in CLI tools, background workers, Celery tasks, and other non-web contexts without introducing a second DI framework.
Python developers building applications with FastAPI who need to reuse dependency logic in non-HTTP contexts like CLI tools, background jobs, or scheduled tasks.
Developers choose FastAPI Injectable because it provides a seamless, drop-in solution that works with existing FastAPI dependencies, requires minimal code changes, and avoids the complexity of maintaining multiple dependency injection systems in a single project.
A seamless solution for using FastAPI's dependency injection system outside of route handlers, enabling painless reuse of dependencies in CLI tools, background tasks, and other non-HTTP contexts.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
The @injectable decorator works as a drop-in extension for existing FastAPI Depends() functions, allowing immediate reuse in non-route contexts without refactoring, as demonstrated in the quick start example.
Handles synchronous, asynchronous, and mixed dependency chains seamlessly, with utilities like run_coroutine_sync() for async code in sync contexts, detailed in the async support section.
Built-in lifecycle management for generator dependencies includes cleanup functions like cleanup_exit_stack_of_func() and graceful shutdown via signals, ensuring proper resource handling in long-running processes.
Includes a dedicated mypy plugin for full type-checking support, making it safe for large codebases with static analysis, though it requires additional configuration.
Requires configuring the mypy plugin and managing event loop strategies (e.g., 'isolated' vs 'background_thread'), which adds complexity compared to simpler DI solutions or native FastAPI usage.
Tied specifically to FastAPI's dependency injection system, so it lacks advanced features like dynamic binding or modular containers found in frameworks like Dependency Injector, as acknowledged in the FAQ.
Managing event loops for async dependencies can be tricky, especially with strategies like 'isolated' or 'background_thread', requiring careful handling to avoid runtime errors in mixed environments.