skillfed

fastapi-injectable

Use FastAPI's Depends() anywhere — in CLI tools, Celery tasks, background workers, and more. No refactoring needed.

fastapi-injectable v1.6.0 83.5K downloads/30d#14,072 on PyPI294
Permissive license MIT Active released

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-injectable

uv

uv add fastapi-injectable

poetry

poetry add fastapi-injectable

Installing 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

Development Status :: 5 - Production/StableFramework :: FastAPIIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: Free Threading :: 3 - StableTopic :: Software Development :: Libraries :: Application FrameworksTopic :: Software Development :: Libraries :: Python ModulesTyping :: Typed

Tags

fastapi dependency injection outside routesuse fastapi depends in clibackground worker dependency injectioncelery task fastapi dependenciesreuse fastapi dependenciesdecorator for fastapi dependsscheduled job dependency injection
dependency-injectionfastapi-extensioncli-tools

More Python Modules packages