FastAPI Injectable
raw JSON → 1.4.7 verified Sat May 09 auth: no python
Use FastAPI's Depends() anywhere — in CLI tools, Celery tasks, background workers, and more. No refactoring needed. Current version: 1.4.7. Python >=3.10. Active development.
pip install fastapi-injectable Common errors
error RuntimeError: You cannot use AsyncToSync in the same thread as an async event loop ↓
cause Calling get_injected_obj() inside an already running async context.
fix
Use async_get_injected_obj() instead: from fastapi_injectable import async_get_injected_obj; result = await async_get_injected_obj(func)
error ImportError: cannot import name 'get_injected_obj' from 'fastapi_injectable.util' ↓
cause Using the internal module path instead of the public API.
fix
Use: from fastapi_injectable import get_injected_obj
error TypeError: 'Depends' object is not callable ↓
cause Passing Depends() as a parameter without annotating with Annotated.
fix
Use Annotated[type, Depends(dependency)] syntax, e.g., DepDB = Annotated[str, Depends(get_db)]
error AttributeError: 'NoneType' object has no attribute 'dependency_overrides' ↓
cause Passing a FastAPI app that is None to get_injected_obj.
fix
Ensure the app parameter is not None when using overrides, or omit it.
Warnings
gotcha get_injected_obj() can fail with 'event loop already running' if called from an async context like an async callback or Kafka consumer. Use async_get_injected_obj() instead when already inside a running event loop. ↓
fix Replace get_injected_obj with async_get_injected_obj when in an async context.
deprecated Direct import from fastapi_injectable.util (e.g., from fastapi_injectable.util import get_injected_obj) is considered internal and may change without notice. ↓
fix Use from fastapi_injectable import get_injected_obj.
breaking In v1.4.5, the internal async exit stack management changed. If you manually managed exit stacks, your code may break. ↓
fix Let fastapi-injectable handle stack management; avoid overriding internal AsyncExitStack.
gotcha Dependency overrides set via app.dependency_overrides are NOT automatically respected unless you use get_injected_obj with the updated override mechanism (v1.4.0+). ↓
fix Use the override parameter of get_injected_obj or ensure app.dependency_overrides is applied before calling.
Imports
- get_injected_obj wrong
from fastapi_injectable.util import get_injected_objcorrectfrom fastapi_injectable import get_injected_obj - async_get_injected_obj wrong
from fastapi_injectable.util import async_get_injected_objcorrectfrom fastapi_injectable import async_get_injected_obj - Injectable wrong
from fastapi_injectable.decorator import Injectablecorrectfrom fastapi_injectable import Injectable
Quickstart
from fastapi import Depends
from fastapi_injectable import get_injected_obj
from typing import Annotated
async def get_db() -> str:
return "db_session"
DepDB = Annotated[str, Depends(get_db)]
# Use in a non-FastAPI context (e.g., Celery task)
def worker(dep: DepDB) -> None:
print(dep)
# Inject dependencies outside FastAPI
obj = get_injected_obj(worker)
print(obj)