Registry / web-framework / oslo-context

oslo-context

JSON →
library6.5.0pypypi✓ verified 21d ago

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.

pip install oslo.context
INSTALL
IMPORT
SIG · OSLO-CONTEXT
O
oslo-context
web-frameworkpythonv6.5.0
Install
1.9s avg
Import
49ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v6.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.052s · 19.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.9s · import 0.046s · 20MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RequestContext
from oslo_context import context
The primary class for managing request-scoped context information.
get_current
from oslo_context import context
Used to retrieve the current request context from the thread-local storage.

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.

from oslo_context import context # Create a basic RequestContext req_context = context.RequestContext( auth_token='fake_auth_token', user_id='fake_user_id', project_id='fake_project_id', is_admin=False ) # Store the context for the current thread req_context.make_current() # Later in the application, retrieve the context current_context = context.get_current() if current_context: print(f"Current User ID: {current_context.user_id}") print(f"Is Admin: {current_context.is_admin}") # Example of creating an admin context admin_context = context.get_admin_context(show_deleted=True) admin_context.make_current() print(f"Admin Context User ID: {context.get_current().user_id}") print(f"Admin Context is Admin: {context.get_current().is_admin}") # Clean up the context (important in testing or if not in a WSGI app) context.clear_current()
Debug
Known issues
breakingThe `tenant` argument of `RequestContext` has been removed in version 6.x.x (specifically, from the Zed series). It was deprecated for a long time.
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.
affects: >=6.0.0
deprecatedPassing 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.
fix
Always use keyword arguments when initializing `RequestContext` to ensure clarity and forward compatibility (e.g., `RequestContext(user_id='...', project_id='...')` instead of `RequestContext('...', '...')`).
affects: >=2.7.0
gotchaThe `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.
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.
affects: >=6.0.0
gotchaThe 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()`.
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].
affects: All versions
Errors
Common errors & fixes
TypeError: RequestContext.__init__() got an unexpected keyword argument 'tenant'
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.
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=...)`.
oslo_context.context.get_current() returns None
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.
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.
AttributeError: 'RequestContext' object has no attribute 'some_attribute'
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.
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)`.
Upgrade
Version history
6.5.0latest on PyPI · released Jul 10, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources