{"id":27734,"library":"fastapi-injectable","title":"FastAPI Injectable","description":"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.","status":"active","version":"1.4.7","language":"python","source_language":"en","source_url":"https://github.com/JasperSui/fastapi-injectable","tags":["fastapi","dependency-injection","celery","background-tasks","cli"],"install":[{"cmd":"pip install fastapi-injectable","lang":"bash","label":"Install from PyPI"}],"dependencies":[],"imports":[{"note":"The public API is at package level, not under util.","wrong":"from fastapi_injectable.util import get_injected_obj","symbol":"get_injected_obj","correct":"from fastapi_injectable import get_injected_obj"},{"note":"Use the top-level import for async variant.","wrong":"from fastapi_injectable.util import async_get_injected_obj","symbol":"async_get_injected_obj","correct":"from fastapi_injectable import async_get_injected_obj"},{"note":"Injectable class is exposed at package level.","wrong":"from fastapi_injectable.decorator import Injectable","symbol":"Injectable","correct":"from fastapi_injectable import Injectable"}],"quickstart":{"code":"from fastapi import Depends\nfrom fastapi_injectable import get_injected_obj\nfrom typing import Annotated\n\nasync def get_db() -> str:\n    return \"db_session\"\n\nDepDB = Annotated[str, Depends(get_db)]\n\n# Use in a non-FastAPI context (e.g., Celery task)\ndef worker(dep: DepDB) -> None:\n    print(dep)\n\n# Inject dependencies outside FastAPI\nobj = get_injected_obj(worker)\nprint(obj)\n","lang":"python","description":"Inject FastAPI-style dependencies anywhere, even outside an async context."},"warnings":[{"fix":"Replace get_injected_obj with async_get_injected_obj when in an async context.","message":"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.","severity":"gotcha","affected_versions":"<1.4.2"},{"fix":"Use from fastapi_injectable import get_injected_obj.","message":"Direct import from fastapi_injectable.util (e.g., from fastapi_injectable.util import get_injected_obj) is considered internal and may change without notice.","severity":"deprecated","affected_versions":"all"},{"fix":"Let fastapi-injectable handle stack management; avoid overriding internal AsyncExitStack.","message":"In v1.4.5, the internal async exit stack management changed. If you manually managed exit stacks, your code may break.","severity":"breaking","affected_versions":">=1.4.5"},{"fix":"Use the override parameter of get_injected_obj or ensure app.dependency_overrides is applied before calling.","message":"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+).","severity":"gotcha","affected_versions":">=1.4.0"}],"env_vars":null,"last_verified":"2026-05-09T00:00:00.000Z","next_check":"2026-08-07T00:00:00.000Z","problems":[{"fix":"Use async_get_injected_obj() instead: from fastapi_injectable import async_get_injected_obj; result = await async_get_injected_obj(func)","cause":"Calling get_injected_obj() inside an already running async context.","error":"RuntimeError: You cannot use AsyncToSync in the same thread as an async event loop"},{"fix":"Use: from fastapi_injectable import get_injected_obj","cause":"Using the internal module path instead of the public API.","error":"ImportError: cannot import name 'get_injected_obj' from 'fastapi_injectable.util'"},{"fix":"Use Annotated[type, Depends(dependency)] syntax, e.g., DepDB = Annotated[str, Depends(get_db)]","cause":"Passing Depends() as a parameter without annotating with Annotated.","error":"TypeError: 'Depends' object is not callable"},{"fix":"Ensure the app parameter is not None when using overrides, or omit it.","cause":"Passing a FastAPI app that is None to get_injected_obj.","error":"AttributeError: 'NoneType' object has no attribute 'dependency_overrides'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}