FastAPI Troubleshooting¶
Common issues and fixes for Wireup's FastAPI integration.
Injection is not set up correctly¶
Symptom:
- You get an error saying injection is not set up correctly when hitting an endpoint.
Fix:
- Make sure
wireup.integration.fastapi.setup(container, app)is called. - Make sure routes use
Injected[...]or@injectwhere expected.
container = wireup.create_async_container(
injectables=[services, wireup.integration.fastapi]
)
app = FastAPI()
wireup.integration.fastapi.setup(container, app)
Tests fail but app works¶
Symptom:
- Injection or startup behavior fails in tests.
Fix:
Use TestClient as a context manager so FastAPI lifespan runs.
def test_endpoint(app: FastAPI):
with TestClient(app) as client:
res = client.get("/")
assert res.status_code == 200
get_request_container() is unavailable in middleware/helpers¶
Symptom:
get_request_container()raisesWireupErrorin middleware or request-time decorators/helpers.
Fix:
- Call setup with
middleware_mode=True. - Make sure Wireup middleware is outermost (middleware ordering matters).
wireup.integration.fastapi.setup(container, app, middleware_mode=True)
get_request_container() is unavailable in WebSocket handlers¶
Symptom:
get_request_container()raises inside WebSocket routes.
Why:
- Middleware-backed request containers in FastAPI are HTTP-only.
Fix:
- Prefer
Injected[...]in websocket handlers/services. - Use
get_app_container(app)outside request scope if needed.
Middleware added after setup(...)¶
Symptom:
- Request-container behavior in middleware is inconsistent.
Fix:
- Add middleware before calling
setup(...). - If middleware needs request-scoped DI, also enable
middleware_mode=True.