{"id":7241,"library":"flask-request-id-header","title":"Flask Request ID Header","description":"Flask-Request-ID-Header is a Python Flask middleware designed to ensure that all incoming requests to a Flask application include an `X-Request-ID` header, containing at least one unique value. This is particularly useful for request tracing and correlation in distributed systems and logging. The current version is 0.1.1, and the project has not seen active development since its last release in March 2019, indicating a maintenance-only status.","status":"maintenance","version":"0.1.1","language":"en","source_language":"en","source_url":"https://github.com/antarctica/flask-request-id-header","tags":["flask","middleware","request-id","header","logging","tracing","correlation-id"],"install":[{"cmd":"pip install flask-request-id-header","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core web framework dependency for the middleware.","package":"Flask"}],"imports":[{"symbol":"RequestID","correct":"from flask_request_id_header.middleware import RequestID"}],"quickstart":{"code":"from flask import Flask, request\nfrom flask_request_id_header.middleware import RequestID\n\napp = Flask(__name__)\nRequestID(app)\n\n@app.route('/')\ndef hello_world():\n    request_id = request.headers.get('X-Request-ID', 'No-Request-ID-Found')\n    return f'Hello, world! Request ID: {request_id}'\n\nif __name__ == '__main__':\n    app.run(debug=True)","lang":"python","description":"Initializes a Flask app and applies the RequestID middleware. It then defines a simple route to demonstrate how to retrieve the `X-Request-ID` header from the request."},"warnings":[{"fix":"No direct fix needed for current functionality, but be aware of evolving HTTP header conventions.","message":"The 'X-' prefix for custom HTTP headers, such as 'X-Request-ID', is technically deprecated by W3C standards, which recommend formal header naming conventions. While still widely supported and a de-facto standard, this might be a consideration for future-proofing.","severity":"deprecated","affected_versions":"All versions"},{"fix":"Set `app.config['REQUEST_ID_UNIQUE_VALUE_PREFIX'] = 'YOUR_PREFIX-'` to prevent modification of headers starting with 'YOUR_PREFIX-'.","message":"The middleware automatically appends a new UUID (version 4) to the 'X-Request-ID' header if an existing value does not already contain at least one UUID v4. If you wish to preserve a custom non-UUID request ID provided by a client, you must configure `app.config['REQUEST_ID_UNIQUE_VALUE_PREFIX']` with a prefix that your custom IDs will use.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Manually configure a `logging.Filter` to inject `request.headers.get('X-Request-ID')` into log records.","message":"This library focuses on ensuring the 'X-Request-ID' header exists and is correctly formatted. It does not automatically integrate the request ID into Flask's default logging system. To include the request ID in your application logs, you will need to implement a custom logging filter or use a more comprehensive logging library.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure the package is installed: `pip install flask-request-id-header`. Verify the import statement matches the correct path.","cause":"The `flask-request-id-header` package is not installed in the current environment, or the import path `from flask_request_id_header.middleware import RequestID` is incorrect.","error":"ModuleNotFoundError: No module named 'flask_request_id_header.middleware'"},{"fix":"Access the header within a request context (e.g., inside a route function, a `before_request` handler, or using `app.test_request_context()`). Use `request.headers.get('X-Request-ID', 'default_value')` to safely retrieve the header without raising a `KeyError` if it might be missing in certain scenarios.","cause":"Attempting to access the `X-Request-ID` header from the Flask `request` object outside of an active request context (e.g., during application setup) or before the middleware has processed the request. While the middleware ensures the header, access without a proper request context will fail.","error":"KeyError: 'X-Request-ID' when accessing `request.headers['X-Request-ID']`"},{"fix":"Access the request ID directly from the request headers dictionary: `request.headers.get('X-Request-ID')`.","cause":"This specific library (flask-request-id-header) does not expose the request ID via `request.request_id` or `flask.g.request_id` by default. These patterns are common in *other* Flask request ID extensions.","error":"AttributeError: 'Request' object has no attribute 'request_id' or NameError: name 'g' is not defined when trying to access `g.request_id`"}]}