{"id":23711,"library":"fastapi-injector","title":"FastAPI Injector","description":"Integrates python-injector with FastAPI for dependency injection. Current version: 0.9.0. Supports Python 3.10+. Releases are infrequent; last release 2023.","status":"active","version":"0.9.0","language":"python","source_language":"en","source_url":"https://github.com/klen/python-fastapi-injector","tags":["fastapi","dependency-injection","injector"],"install":[{"cmd":"pip install fastapi-injector","lang":"bash","label":"Standard install"}],"dependencies":[{"reason":"Required framework","package":"fastapi","optional":false},{"reason":"Core DI library","package":"injector","optional":false}],"imports":[{"note":"Injector is from injector package, not fastapi-injector","wrong":"from fastapi_injector import Injector","symbol":"FastAPIInjector","correct":"from fastapi_injector import FastAPIInjector"},{"note":"The Injected type alias is provided by fastapi-injector, not injector","wrong":"from injector import Injected","symbol":"Injected","correct":"from fastapi_injector import Injected"}],"quickstart":{"code":"from fastapi import FastAPI, APIRouter\nfrom fastapi_injector import FastAPIInjector, Injected, request_injector\nfrom injector import Module, singleton, inject\n\nclass MyModule(Module):\n    def configure(self, binder):\n        binder.bind(str, to='Hello, World!', scope=singleton)\n\napp = FastAPI()\ninjector = FastAPIInjector(app, modules=[MyModule()])\n\n@router.get('/')\nasync def root(greeting: str = Injected(str)) -> dict:\n    return {'message': greeting}\n\napp.include_router(router)\n\nif __name__ == '__main__':\n    import uvicorn\n    uvicorn.run(app)","lang":"python","description":"Basic FastAPI app with injector module binding and route injection."},"warnings":[{"fix":"Use `from fastapi_injector import request_injector` and then `request_injector.get(SomeClass)` only within a request handler after calling `injector.attach_injector(app)`.","message":"`request_injector` is a property on the app instance, not a standalone import. It must be accessed via `app.state.injector` or `request_injector` only inside a request context when using `AttachInjector` middleware.","severity":"gotcha","affected_versions":"0.9.0"},{"fix":"Instead of `param: SomeClass = Injected(SomeClass)`, define a dependency function using `Depends` that gets the value from the injector's container.","message":"The `Injected` type hint from fastapi_injector is deprecated in favor of using `Depends` with a provider from injector. The direct use of `Injected` may be removed in future versions.","severity":"deprecated","affected_versions":"0.9.0"},{"fix":"After creating `injector = FastAPIInjector(app, modules=[...])`, call `injector.attach_injector()` before the first request.","message":"In version 0.9.0, the `FastAPIInjector` class no longer automatically attaches the injector to the app's state. You must explicitly call `attach_injector()` method on the `FastAPIInjector` instance after creation.","severity":"breaking","affected_versions":"0.9.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Ensure you have version 0.9.0 and call `injector.attach_injector(app)` (the method exists on the instance). If you have an older version, upgrade.","cause":"Using an older version of fastapi-injector where attach_injector didn't exist, or using wrong method name.","error":"AttributeError: 'FastAPIInjector' object has no attribute 'attach_injector'"},{"fix":"Make sure to call `injector.attach_injector()` on your `FastAPIInjector` instance after creation and before handling requests.","cause":"The injector was not attached to the app's middleware. The request_injector context is missing.","error":"InjectorNotFoundException: No injector found for this request"},{"fix":"Use `Injected` as a type annotation for a parameter with a default: `param: SomeClass = Injected(SomeClass)`.","cause":"Misusing `Injected` as a decorator or calling it as a function instead of using it as a type hint with default value.","error":"TypeError: 'Injected' object is not callable"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}