{"id":24684,"library":"taskiq-fastapi","title":"TaskIQ FastAPI Integration","description":"TaskIQ-FastAPI provides integration between FastAPI and TaskIQ, allowing you to run background tasks with access to FastAPI's context (e.g., dependency injection, request state). Current version is 0.5.0, compatible with Python >=3.10. Release cadence is irregular, driven by TaskIQ ecosystem updates.","status":"active","version":"0.5.0","language":"python","source_language":"en","source_url":"https://github.com/taskiq-python/taskiq-fastapi","tags":["taskiq","fastapi","background-tasks","async","integration"],"install":[{"cmd":"pip install taskiq-fastapi","lang":"bash","label":"Default install"}],"dependencies":[{"reason":"Core dependency; taskiq-fastapi is an integration layer on top of TaskIQ.","package":"taskiq","optional":false},{"reason":"Required to use the integration with FastAPI applications.","package":"fastapi","optional":false}],"imports":[{"note":"Entry point for integrating TaskIQ with FastAPI.","symbol":"InitFastAPI","correct":"from taskiq_fastapi import InitFastAPI"},{"note":"Class for managing TaskIQ lifecycle in FastAPI.","wrong":"from taskiq_fastapi import TaskiqFastAPI (if misspelled)","symbol":"TaskiqFastAPI","correct":"from taskiq_fastapi import TaskiqFastAPI"},{"note":"Middleware to expose result backend via request state.","symbol":"ResultBackendMiddleware","correct":"from taskiq_fastapi import ResultBackendMiddleware"}],"quickstart":{"code":"from fastapi import FastAPI\nfrom taskiq import TaskIQ\nfrom taskiq_fastapi import InitFastAPI, TaskiqFastAPI\n\napp = FastAPI()\n\n# Initialize TaskIQ broker (e.g., InMemoryBroker)\nfrom taskiq import InMemoryBroker\nbroker = InMemoryBroker()\n\n# Attach FastAPI integration\ntaskiq_app = TaskiqFastAPI(app, broker)\n\n# Register an initialization callback (optional, for context access)\n@taskiq_app.on_app_init\ndef on_startup():\n    InitFastAPI().init()\n\n@app.get(\"/\")\nasync def root():\n    return {\"message\": \"Hello World\"}\n\n# Task must be defined after broker is available\n@broker.task\ndef my_task(param: str):\n    print(f\"Processing {param}\")\n    return param","lang":"python","description":"Minimal setup integrating TaskIQ with FastAPI using InMemoryBroker. Note: In production, use a persistent broker like Redis or RabbitMQ."},"warnings":[{"fix":"Use `from taskiq_fastapi import InitFastAPI, TaskiqFastAPI` instead of `from taskiq_fastapi.broker import ...`.","message":"In version 0.5.0, the import path changed from `taskiq_fastapi.broker` to `taskiq_fastapi` directly. Old imports will break.","severity":"breaking","affected_versions":"0.5.0"},{"fix":"Ensure `InitFastAPI().init()` is called at startup (e.g., in a lifespan event or by decorating a function with `@taskiq_app.on_app_init`).","message":"The `InitFastAPI` must be called during the app lifecycle (usually in a startup event or via `on_app_init` decorator) to inject FastAPI dependencies into tasks. Not calling it results in `RuntimeError: No request context available` when accessing FastAPI dependencies.","severity":"gotcha","affected_versions":"all"},{"fix":"Define task functions after the `TaskiqFastAPI` instance is created, or ensure the broker is correctly wired before task definitions.","message":"Tasks defined before the broker is attached to `TaskiqFastAPI` may not have access to context. Define tasks after calling `taskiq_app = TaskiqFastAPI(app, broker)`.","severity":"gotcha","affected_versions":"all"},{"fix":"Remove `middleware` parameter from `TaskiqFastAPI` and add `ResultBackendMiddleware` to the app if needed.","message":"The `TaskiqFastAPI` class previously accepted a `middleware` argument; this is deprecated in favor of using `ResultBackendMiddleware` separately.","severity":"deprecated","affected_versions":">=0.4.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Add a startup event or use `@taskiq_app.on_app_init` to call `InitFastAPI().init()` before any task runs.","cause":"Trying to access FastAPI request or dependency injection in a task without initializing the context via `InitFastAPI().init()`.","error":"RuntimeError: No request context available"},{"fix":"Upgrade to the latest version (0.5.0) and use `from taskiq_fastapi import InitFastAPI`.","cause":"Using an older version where the import path was different (e.g., `from taskiq_fastapi.broker import InitFastAPI`).","error":"ImportError: cannot import name 'InitFastAPI' from 'taskiq_fastapi'"},{"fix":"Use `from taskiq_fastapi import TaskiqFastAPI` and ensure you have version 0.5.0 compatible code.","cause":"Misspelled class name or using an outdated version that used a different class name (e.g., `FastApiApp`).","error":"AttributeError: module 'taskiq_fastapi' has no attribute 'TaskiqFastAPI'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}