{"id":5355,"library":"oslo-context","title":"Oslo Context Library","description":"The `oslo.context` library is a core OpenStack component providing helpers to manage and maintain useful information about a request context. This context is typically populated within a WSGI pipeline and utilized by various modules, such as `oslo.log`, for consistent data access throughout a request's lifecycle. The library is currently at version 6.3.0 and follows the OpenStack release cadence.","status":"active","version":"6.3.0","language":"python","source_language":"en","source_url":"https://github.com/openstack/oslo.context","tags":["openstack","context","wsgi","thread-local","logging"],"install":[{"cmd":"pip install oslo.context","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The primary class for managing request-scoped context information.","symbol":"RequestContext","correct":"from oslo_context import context"},{"note":"Used to retrieve the current request context from the thread-local storage.","symbol":"get_current","correct":"from oslo_context import context"}],"quickstart":{"code":"from oslo_context import context\n\n# Create a basic RequestContext\nreq_context = context.RequestContext(\n    auth_token='fake_auth_token',\n    user_id='fake_user_id',\n    project_id='fake_project_id',\n    is_admin=False\n)\n\n# Store the context for the current thread\nreq_context.make_current()\n\n# Later in the application, retrieve the context\ncurrent_context = context.get_current()\n\nif current_context:\n    print(f\"Current User ID: {current_context.user_id}\")\n    print(f\"Is Admin: {current_context.is_admin}\")\n\n# Example of creating an admin context\nadmin_context = context.get_admin_context(show_deleted=True)\nadmin_context.make_current()\nprint(f\"Admin Context User ID: {context.get_current().user_id}\")\nprint(f\"Admin Context is Admin: {context.get_current().is_admin}\")\n\n# Clean up the context (important in testing or if not in a WSGI app)\ncontext.clear_current()","lang":"python","description":"This quickstart demonstrates how to create a `RequestContext` object, make it available globally within the current thread using `make_current()`, and retrieve it with `get_current()`. It also shows how to create a specialized admin context. For real-world applications, `RequestContext` is typically initialized by WSGI middleware and automatically managed."},"warnings":[{"fix":"Migrate from using `tenant` to `project_id` or `project_name` when initializing or accessing context information. Review your `RequestContext` instantiations and update arguments accordingly.","message":"The `tenant` argument of `RequestContext` has been removed in version 6.x.x (specifically, from the Zed series). It was deprecated for a long time.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Always use keyword arguments when initializing `RequestContext` to ensure clarity and forward compatibility (e.g., `RequestContext(user_id='...', project_id='...')` instead of `RequestContext('...', '...')`).","message":"Passing positional arguments to `RequestContext.__init__` has been deprecated since version 2.7.0. While it might still work, it can lead to unexpected behavior in future versions.","severity":"deprecated","affected_versions":">=2.7.0"},{"fix":"If your application explicitly uses `get_logging_values` and expects the full `auth_token`, be aware of this change. For non-logging purposes, access `context.auth_token` directly from your `RequestContext` instance.","message":"The `get_logging_values` function (intended for logging) no longer outputs the `auth_token` directly but replaces it with `***` to prevent sensitive data leakage. If you rely on the `auth_token` for other purposes, retrieve it directly from the `RequestContext` object.","severity":"gotcha","affected_versions":">=6.0.0"},{"fix":"For multi-threaded or asynchronous environments, ensure that the `RequestContext` is explicitly passed or re-established in the new thread/task context. Libraries like `oslo.privsep` and `oslo.middleware` handle this for specific use cases [8, 18].","message":"The request context is stored in thread-local storage. If your application uses worker pools or asynchronous patterns that change the execution thread, the context might not be propagated automatically, leading to `None` being returned by `context.get_current()`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'6.3.0':56 'access':44 'cadenc':62 'compon':11 'consist':42 'context':2,23,25,64 'core':9 'current':53 'data':43 'follow':58 'helper':13 'inform':19 'librari':3,6,51 'lifecycl':49 'local':68 'log':69 'maintain':17 'manag':15 'modul':37 'openstack':10,60,63 'oslo':1 'oslo.context':5 'oslo.log':40 'pipelin':32 'popul':28 'provid':12 'releas':61 'request':22,47 'thread':67 'thread-loc':66 'throughout':45 'typic':27 'use':18 'util':34 'various':36 'version':55 'within':29 'wsgi':31,65","created_at":"2026-04-14T01:30:17.041761+00:00","updated_at":"2026-04-16T17:49:33.566672+00:00","problems":[{"fix":"Migrate from using 'tenant' to 'project_id' or 'project_name' when initializing or accessing context information. For example, replace `RequestContext(tenant=...)` with `RequestContext(project_id=...)` or `RequestContext(project_name=...)`.","cause":"The 'tenant' argument of RequestContext was deprecated for a long time and has been completely removed in oslo.context version 6.x.x (Zed series) and later.","error":"TypeError: RequestContext.__init__() got an unexpected keyword argument 'tenant'"},{"fix":"For multi-threaded or asynchronous environments, ensure that the `RequestContext` is explicitly passed or re-established within the new thread/task context. This often involves making the context available to the new execution unit, for instance, by passing the context object as an argument.","cause":"The request context is stored in thread-local storage, meaning it is not automatically propagated across different threads or asynchronous tasks. When a new thread or task attempts to retrieve the current context without it being explicitly set for that execution flow, `None` is returned.","error":"oslo_context.context.get_current() returns None"},{"fix":"Ensure that the desired attribute is properly initialized when the `RequestContext` is created or updated. If the error occurs after calling `get_current()`, add a check to ensure a valid context object was returned before attempting to access its attributes, e.g., `context = oslo_context.context.get_current(); if context: print(context.attribute_name)`.","cause":"This error occurs when code attempts to access an attribute on a `RequestContext` instance that has not been defined or set on that particular object. This can also happen if `oslo_context.context.get_current()` returned `None` (because the context was not propagated) and a subsequent attribute access was attempted without a null check.","error":"AttributeError: 'RequestContext' object has no attribute 'some_attribute'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"6.5.0","cli_name":"","cli_version":null,"type":"library","homepage":"https://docs.openstack.org/oslo.context","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/oslo-context/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-30","next_check":"2026-07-28","install_tag":null}}