Unleash SDK for Python
Unleash is a private, secure, and scalable feature management platform built to reduce the risk of releasing new features and accelerate software development. This server-side Python SDK is designed to help you integrate with Unleash and evaluate feature flags inside your application.
You can use this client with Unleash Enterprise or Unleash Open Source.
Migrating to v7
The client's settings are now grouped into option objects, and every argument after url and app_name is keyword-only. Read the v7 migration guide before upgrading.
Migrating to v6
If you use custom strategies or access the features property on the Unleash Client, read the complete migration guide before upgrading to v6.
Getting Started
Install the Unleash Client in your project
pip install UnleashClient
Initialization
You must initialize the SDK before you use it. Note that until the SDK has synchronized with the API, all features will evaluate to false unless
you have a bootstrapped configuration or you use fallbacks.
from UnleashClient import UnleashClient
client = UnleashClient(
url="https:<YOUR-API-URL>",
app_name="my-python-app",
api_key="<API token>")
client.initialize_client()
Check features
Once the SDK is initialized, you can evaluate toggles using the is_enabled or get_variant methods.
enabled = client.is_enabled("my_toggle")
print(enabled)
> True
variant = client.get_variant("variant_toggle")
print(variant)
> {
> "name": "variant1",
> "payload": {
> "type": "string",
> "value": "val1"
> },
> "enabled": True
> }
Shutdown
If your program no longer needs the SDK, you can call destroy(), which shuts down the SDK and flushes any pending metrics to Unleash.
client.destroy()
Usage
Context
Both the is_enabled and get_variant functions support Unleash contexts as the second parameter.
app_context = {
"userId": "test@email.com",
"sessionId": "55845",
"properties": {
"custom-property": "some-value"
}
}
client.is_enabled("user_id_toggle", app_context)
client.get_variant("variant_toggle", app_context)
The context values can be any type that has a __str__ implementation. Types that are explicitly supported are:
- Numerics
- Strings
- Dates
- UUIDs
Note: Gradual rollout strategies require you to pass either a userId or a sessionId for stickiness to work correctly.
Fallback function
You can specify a fallback function for cases where the client doesn't recognize the toggle by using the fallback_function keyword argument:
def custom_fallback(feature_name: str, context: dict) -> bool:
return True
client.is_enabled("my_toggle", fallback_function=custom_fallback)
The fallback function must accept the feature name and context as positional arguments in that order.
The client will evaluate the fallback function if the feature flag is not found or an exception occurs when calling the is_enabled() method.
Configuration options
url and app_name are required. Everything else is a keyword argument, and related settings are grouped into option objects:
from UnleashClient import (
MetricsOptions, PollingOptions, RequestsTransport, UnleashClient,
)
client = UnleashClient(
url="https://YOUR-API-URL",
app_name="my-python-app",
api_key="<API token>",
transport=RequestsTransport(timeout=10),
mode=PollingOptions(interval=30),
metrics=MetricsOptions(disabled=True),
)
Settings can't be changed after the client is created.
| Parameter |
Description |
Default |
| url |
URL of your Unleash server, e.g. https://app.unleash-hosted.com/demo/api/. Required. |
β |
| app_name |
Name of the application using the client. Required. |
β |
| api_key |
Client API token, sent as the Authorization header. |
None |
| identity |
How the client identifies itself. See IdentityOptions. |
IdentityOptions() |
| mode |
How the client receives flag updates: PollingOptions or StreamingOptions. |
PollingOptions() |
| metrics |
How the client reports to the server. See MetricsOptions. |
MetricsOptions() |
| verbose_log_level |
Log level for messages about unknown flags and failed impression events. |
30 |
| custom_strategies |
Dict of {name: strategy} for custom activation strategies. See custom strategies. |
None |
| cache |
Custom cache implementation (must extend BaseCache). See custom cache. |
FileCache |
| cache_directory |
Directory for the default FileCache. When unset, a platform-specific user cache directory is used. Ignored when cache is given. |
None |
| scheduler |
Custom APScheduler instance. When unset, the client creates a BackgroundScheduler. Sync client only. |
None |
| scheduler_executor |
Name of the executor to use on your custom scheduler. Required with scheduler. Sync client only. |
None |
| multiple_instance_mode |
How to handle multiple client instances (BLOCK, WARN, SILENTLY_ALLOW). |
WARN |
| event_callback |
Function that receives impression and lifecycle events. See events and impression data. |
None |
| transport |
How requests are sent, including headers, timeout, proxy and TLS settings. See custom HTTP transport. |
RequestsTransport or AiohttpTransport |
IdentityOptions
| Parameter |
Description |
Default |
| instance_id |
Unique identifier for this client instance. |
"unleash-python-sdk" |
| sdk_flavor |
Identifier of an integration built on this SDK (e.g. an OpenFeature provider). |
None |
| sdk_flavor_version |
Version of the integration named by sdk_flavor. |
None |
PollingOptions
The default: the client fetches feature flags from the server on an interval.
| Parameter |
Description |
Default |
| interval |
How often to fetch feature flags (seconds). |
15 |
| jitter |
Maximum random delay added to each refresh (seconds). |
None |
| project_name |
Only fetch the feature flags of this project. |
None |
StreamingOptions
Experimental: the client keeps a streaming connection open and receives flag updates as they happen. Pass mode=StreamingOptions(). It has no settings yet. AsyncUnleashClient doesn't support streaming.
MetricsOptions
| Parameter |
Description |
Default |
| interval |
How often to send metrics to Unleash (seconds). |
60 |
| jitter |
Maximum random delay added to each metrics send (seconds). |
None |
| disabled |
Disable sending usage metrics. |
False |
| disable_registration |
Disable client registration. |
False |
Bootstrap
By default, the Python SDK fetches your feature flags from the Unleash API at startup. If you want to make your SDK more resilient (e.g., during network outages), you can bootstrap the client with a local or remote toggle config.
How it works:
Use a FileCache (or your own BaseCache implementation).
Pre-seed it with feature flags using bootstrap_from_dict, bootstrap_from_file, or bootstrap_from_url.
Pass your cache to the UnleashClient on startup.
The default FileCache has built-in methods for bootstrapping from a dictionary, file, or URL.
Bootstrap from dict
from UnleashClient.cache import FileCache
from UnleashClient import UnleashClient
# Create and seed the cache
cache = FileCache("MY_CACHE")
cache.bootstrap_from_dict({
"version": 2,
"features": [
{
"name": "my_toggle",
"enabled": True,
"strategies": [{"name": "default"}],
}
]
})
client = UnleashClient(
url="https://YOUR-API-URL",
app_name="my-python-app",
cache=cache
)
Bootstrap from file
from pathlib import Path
from UnleashClient.cache import FileCache
cache = FileCache("MY_CACHE")
cache.bootstrap_from_file(Path("/path/to/your_bootstrap.json"))
client = UnleashClient(
url="https://YOUR-API-URL",
app_name="my-python-app",
cache=cache
)
Bootstrap from URL
from UnleashClient.cache import FileCache
cache = FileCache("MY_CACHE")
cache.bootstrap_from_url("https://your-server/bootstrap.json")
client = UnleashClient(
url="https://YOUR-API-URL",
app_name="my-python-app",
cache=cache
)
Custom strategies
The Python SDK lets you define custom activation strategies if the built-in ones donβt cover your needs. This gives you more fine grained control over how your features evaluate.
A custom strategy is just a class that implements an apply method.
class ActiveForEmailStrategy:
def apply(self, parameters: dict, context: dict = None) -> bool:
# Decide if the feature is active for this context
return context.get("email") in parameters
Once youβve defined your strategy, register it when you initialize the client. The key must match the strategy name in Unleash exactly.
## You should have a custom strategy defined in Unleash called 'EmailStrategy'
my_custom_strategies = {
"EmailStrategy": ActiveForEmailStrategy()
}
client = UnleashClient(
url="https://YOUR-API-URL",
app_name="my-python-app",
api_key="<API token>",
custom_strategies = my_custom_strategies
)
Events and impression data
The Python SDK lets you tap into its behavior through impression data and lifecycle events.
Note: The SDK does not include a built-in event bus β youβll need to provide your own. The example below shows how to use Blinker to send signals.
Impression events
To use impression data:
- Enable impression data on your feature flags in the Unleash UI.
- Provide an event_callback function when you initialize the client.
Your callback must accept a single UnleashEvent. You can log it, store it, or send it to another system.
from blinker import signal
from UnleashClient import UnleashClient
from UnleashClient.events import UnleashEvent
send_data = signal('send-data')
@send_data.connect
def receive_data(sender, **kw):
print("Caught signal from %r, data %r" % (sender, kw))
def example_callback(event: UnleashEvent):
send_data.send('anonymous', data=event)
client = UnleashClient(
url="https://YOUR-API-URL",
app_name="my-python-app",
api_key="<API token>",
event_callback=example_callback
)
client.initialize_client()
client.is_enabled("testFlag")
Impression callbacks run in-process β keep them fast to avoid blocking your app.
Lifecycle events
The same event_callback also delivers lifecycle events:
- FETCHED: triggered when a new version of feature flags is pulled from the Unleash server. (Does not trigger on 304 Not Modified). The FETCHED event includes a features property containing all the feature flags returned by that fetch.
- READY: triggered once when the SDK first loads feature flags from the Unleash server or a local backup.
from blinker import signal
from UnleashClient import UnleashClient
from UnleashClient.events import UnleashEvent, UnleashEventType
send_data = signal('send-data')
@send_data.connect
def receive_data(sender, **kw):
if kw["data"].event_type == UnleashEventType.READY:
print("SDK is ready: toggles loaded from Unleash or backup")
elif kw["data"].event_type == UnleashEventType.FETCHED:
# Only FETCHED events have a 'features' property
print("Fetched new feature flags:", kw["data"].features)
def example_callback(event: UnleashEvent):
send_data.send('anonymous', data=event)
client = UnleashClient(
url="https://YOUR-API-URL",
app_name="my-python-app",
api_key="<API token>",
event_callback=example_callback
)
client.initialize_client()
client.is_enabled("testFlag")
Impact metrics