fastapi-injectable
Use FastAPI's Depends() anywhere — in CLI tools, Celery tasks, background workers, and more. No refactoring needed.
What it is and what it does
fastapi-injectable is a lightweight decorator and function-based wrapper that extends FastAPI's Depends() system to work outside route handlers. It solves a long-standing limitation (FastAPI issue #1105) where developers had to duplicate dependency logic or introduce a second DI framework when moving from routes into CLI commands, background tasks, Celery jobs, or scheduled workers.
The package provides three main entry points: the @injectable decorator for marking functions, get_injected_obj() for synchronous contexts, and async_get_injected_obj() for code running in an active event loop. It handles full async support, resource cleanup via generator dependencies, optional caching, and graceful shutdown. A bundled mypy plugin enables type-checking of @injectable calls. The single runtime dependency is fastapi itself.
Use it for:
- Reuse FastAPI route dependencies in CLI commands without refactoring or duplication.
- Inject dependencies into Celery tasks or background workers using the same Depends() syntax as routes.
- Access FastAPI app.state and registered dependencies in scheduled jobs (cron, APScheduler).
- Write testable functions with dependency overrides using manual mock injection.
- Handle async dependencies in Kafka consumers or streaming callbacks with async_get_injected_obj().
- Manage resource lifecycle (database connections, file handles) via generator-based dependencies in non-route contexts.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Enables FastAPI's Depends() dependency injection system to work outside route handlers—in CLI tools, background workers, Celery tasks, and scheduled jobs—without duplicating dependency logic.
Yes. The package is actively maintained, has no known vulnerabilities, and solves a real problem for FastAPI users who need dependency injection outside routes. Low install friction, permissive license, and production-stable status make it a straightforward choice. The main gotcha is cache management in loops—read the long-running worker documentation before deploying to background tasks.
Install
fastapi-injectable on PyPI
pip
pip install fastapi-injectableuv
uv add fastapi-injectablepoetry
poetry add fastapi-injectableInstalling fastapi-injectable
Before you install
Low friction install with a single runtime dependency (fastapi). Active maintenance: last commit 2026-08-01, 294 repository stars, and production-stable status since 2024-12-18.
License in practice
MIT license (permissive) — you can use, modify, and distribute this package freely in commercial and open-source projects with minimal restrictions.
Quickstart
pip install fastapi-injectable
from typing import Annotated
from fastapi import Depends
from fastapi_injectable import injectable
@injectable
def process_data(db: Annotated[Database, Depends(get_db)]) -> str:
return db.query()
result = process_data()
Requires Python 3.10 or higher and FastAPI >=0.112.4,<1.0.0. Process-global dependency cache persists across calls in loops; use injectable_scope() or clear_dependency_cache() + cleanup_all_exit_stacks() to reset state per iteration in long-running workers.
Verify before relying
- Whether the mypy plugin works correctly with all mypy versions and configurations beyond the documented setup.
- Performance characteristics when dependency graphs are deeply nested or contain many generator-based dependencies.
- Compatibility behavior with FastAPI versions at the upper boundary (0.112.4 through 1.0.0 pre-release).
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — fastapi |
| Maintenance | actively maintained — 54 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 83,496/month — #14,072 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: fastapi_injectable-1.6.0-py3-none-any.whl
Keywords: async, background-tasks, celery, clean-architecture, cli, dependency-injection, depends, di, fastapi, inversion-of-control, ioc, service-layer, testing
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
fast-dependsFastDepends provides a standalone dependency…
permissive · top 5,000 on PyPI
fastapi-decoratorsConverts functions into FastAPI-compatible…
permissive · top 15,000 on PyPI
typer-injectorAdds FastAPI-style dependency injection to…
permissive · top 15,000 on PyPI
uncalled-forProvides async dependency injection for Python…
permissive · top 1,000 on PyPI
taskiq-dependenciesProvides FastAPI-style dependency injection for…
unclear · top 5,000 on PyPI
fastapi-injectorIntegrates the injector dependency-injection…
permissive · top 15,000 on PyPI
FastAPI-SQLAlchemyProvides middleware and helpers to integrate…
permissive · top 15,000 on PyPI
taskiq-fastapiIntegrates FastAPI request and dependency…
permissive · top 15,000 on PyPI
fastapi-healthAdds a health check endpoint to FastAPI…
permissive · top 15,000 on PyPI
fastapi-lifespan-managerProvides a lifespan manager for FastAPI that…
permissive · top 15,000 on PyPI