{"id":24652,"library":"starlette-request-id","title":"starlette-request-id","description":"A Starlette middleware that adds a unique request ID to each incoming request, typically used for logging and tracing. Current version 1.2.1 with a release cadence of occasional updates. Requires Python >=3.7.","status":"active","version":"1.2.1","language":"python","source_language":"en","source_url":"https://github.com/bigbag/starlette-request-id","tags":["starlette","request-id","middleware","logging","tracing"],"install":[{"cmd":"pip install starlette-request-id","lang":"bash","label":"Install from PyPI"}],"dependencies":[{"reason":"Core dependency for middleware integration","package":"starlette","optional":false}],"imports":[{"note":"In older versions, RequestIDMiddleware was in a submodule but now it's imported directly from the top-level package.","wrong":"from starlette_request_id.middleware import RequestIDMiddleware","symbol":"RequestIDMiddleware","correct":"from starlette_request_id import RequestIDMiddleware"}],"quickstart":{"code":"from starlette.applications import Starlette\nfrom starlette.routing import Route\nfrom starlette.requests import Request\nfrom starlette.responses import JSONResponse\nfrom starlette_request_id import RequestIDMiddleware\n\nasync def home(request: Request):\n    request_id = request.headers.get('X-Request-ID')\n    return JSONResponse({'request_id': request_id})\n\napp = Starlette(debug=True, routes=[Route('/', home)])\napp.add_middleware(RequestIDMiddleware)\n\nif __name__ == '__main__':\n    import uvicorn\n    uvicorn.run(app, host='0.0.0.0', port=8000)","lang":"python","description":"A minimal Starlette app that adds request ID middleware and returns the ID in the response."},"warnings":[{"fix":"To force generation of a new ID regardless, pass force_new_uuid=True to the middleware constructor: app.add_middleware(RequestIDMiddleware, force_new_uuid=True).","message":"The middleware automatically adds a request ID header, but it does not override an existing one from the client unless explicitly configured. By default, if the incoming request already has a 'X-Request-ID' header, that value is used instead of generating a new one. This can be surprising if you expect a fresh ID for every request.","severity":"gotcha","affected_versions":"all"},{"fix":"Access the request ID via request.headers.get('X-Request-ID') or the middleware's helper function.","message":"The attribute 'request.id' used in some older examples is deprecated. Use the 'X-Request-ID' header or the middleware's provided property instead.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Add the RequestIDMiddleware as the first middleware in your application.","message":"The middleware must be added before any other middleware that might read request headers, otherwise the request ID might not be available in those middlewares.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Run 'pip install starlette-request-id' in your active environment.","cause":"Package not installed or installed in wrong environment.","error":"ModuleNotFoundError: No module named 'starlette_request_id'"},{"fix":"Use 'from starlette_request_id import RequestIDMiddleware'.","cause":"Incorrect import path (likely an older version's submodule).","error":"ImportError: cannot import name 'RequestIDMiddleware' from 'starlette_request_id'"},{"fix":"Use 'request.headers.get('X-Request-ID')' instead.","cause":"Trying to access request.id which is deprecated or not available in this version.","error":"AttributeError: 'Request' object has no attribute 'id'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}