Python SDK
The Unleash Python SDK lets you evaluate feature flags in Python applications. It connects to Unleash or Unleash Edge to fetch flag configurations and evaluates them locally against an Unleash context.
You can use this SDK with Unleash Enterprise or Unleash Open Source.
For an overview of how Unleash SDKs work, including offline behavior, feature compatibility across SDKs, and default refresh and metrics intervals, refer to the SDK overview.
The SDK includes two clients: UnleashClient for synchronous applications and AsyncUnleashClient, an asyncio-native client for applications built on frameworks such as FastAPI, Starlette, or aiohttp. Both expose the same API for evaluating flags; they differ in how you initialize and stop them.
Requirements
- Python 3.10 or later
Installation
Install the SDK from PyPI:
Sync
Async
The async extra installs the SDK together with aiohttp, which AsyncUnleashClient uses to communicate with Unleash. Importing AsyncUnleashClient without aiohttp installed raises an ImportError.
Initialization
Create a single Unleash client instance and share it across your application. Running multiple instances can lead to significantly increased request volume and may count as additional SDK connections toward your usage limits.
Set url to the /api endpoint of your Unleash instance or your Unleash Edge instance. Set the API token to a backend API token.
Flag checks are regular synchronous methods on both clients, because evaluation happens locally against the SDK’s cached configuration and performs no I/O. Only the async client’s lifecycle methods are coroutines.
Sync
Async
You can also use the client as a context manager, which initializes the client on entry and shuts it down on exit:
Check if the SDK is ready
Once initialized but not yet synchronized with Unleash, all flags evaluate to false and variants resolve to the disabled variant, unless you have a bootstrapped configuration or use a fallback function.
Initialize the client before calling is_enabled or get_variant. Flag evaluation is only supported after initialization.
To know when flag configuration is available, subscribe to the READY event. The SDK emits it when it first loads flags from the Unleash server or the local cache. The is_initialized property only tells you that initialize_client() has completed, which can happen before any flags are loaded.
The event callback runs on a background thread:
Sync
Async
UnleashClient makes its first fetch during initialize_client(), before the call returns. If that fetch fails, the client keeps polling and emits READY once a fetch succeeds.
Because the callback runs on a thread, bridge it to the event loop with a thread-safe primitive such as the Event from aiologic:
Configuration options
url and app_name are positional arguments. All other arguments are keyword-only, and related settings are grouped into option objects:
Client settings are fixed at creation. To change a setting, create a new client.
If you are upgrading from an earlier version, see Migrating to v7.
Both client constructors support the following parameters. Parameters marked “Sync client only” apply only to UnleashClient.
URL of your Unleash or Unleash Edge API, for example https://<your-unleash-instance>/api. Must be an absolute http:// or https:// URL with a host, otherwise the constructor raises a ValueError.
Name of the application using the client. Included in registration, metrics, and request headers.
Your backend API token, sent as the Authorization header. Required for any client that connects to Unleash.
How the client identifies itself to Unleash. See IdentityOptions.
How the client sends its requests to Unleash, including custom headers, the timeout, and proxy settings. UnleashClient uses a RequestsTransport by default, AsyncUnleashClient an AiohttpTransport. See transport and custom HTTP transport.
How the client receives flag updates: PollingOptions or StreamingOptions. AsyncUnleashClient supports polling only.
How the client reports usage metrics to Unleash. See MetricsOptions.
Dictionary of strategy names to strategy objects. See custom strategies.
Custom cache implementation that extends UnleashClient.cache.BaseCache. See custom cache. When unset, the SDK uses an on-disk cache.
Location of the on-disk cache. When unset, a platform-specific user cache directory is used. Ignored when you pass cache.
Numerical log level used when checking a feature flag fails.
Custom APScheduler instance. Use this to customize job stores or executors. When unset, the SDK creates its own scheduler. Sync client only.
Name of the APScheduler executor to use with a custom scheduler. Required with scheduler. Sync client only.
How the SDK handles multiple client instances: BLOCK fails construction, WARN logs a warning, and SILENTLY_ALLOW does nothing.
Function that receives SDK events, including impression events. Called on a dedicated background thread. If you run under a WSGI server or in multiple processes, see framework integration.
IdentityOptions
Unique identifier for this client instance.
Identifier of an integration built on top of this SDK, such as an OpenFeature provider. Leave unset for plain SDK usage.
Version of the integration named by sdk_flavor.
Transport
RequestsTransport (for UnleashClient) and AiohttpTransport (for AsyncUnleashClient) take the same arguments:
Your own session, for example with a proxy, TLS settings, or hooks. The client never closes a session you pass in. See custom HTTP transport.
Additional headers for all API requests. Pass the API token as api_key instead of as a header. If you also set api_key, the SDK ignores an Authorization header and logs a warning. Without api_key, an Authorization header raises a ValueError when the client is created.
Timeout, in seconds, for requests to the Unleash server.
The SDK retries a failed flag fetch up to 3 times, on a 500, 502, or 504 response or when no response arrives, with a short randomized backoff between attempts. Registration and metrics requests are not retried. The number of retries can’t be changed.
PollingOptions
The default mode. The SDK fetches flag configuration from Unleash on an interval.
How often, in seconds, the SDK polls for updated flag configuration.
Maximum random jitter, in seconds, added to each refresh.
Restricts flag fetches to a single project. Deprecated; use a project-scoped API token instead.
MetricsOptions
How often, in seconds, the SDK sends usage metrics to Unleash.
Maximum random delay, in seconds, added to each metrics send.
Disables sending usage metrics to Unleash.
Disables client registration with Unleash.
Check flags
Use is_enabled to evaluate a flag. Evaluation happens locally against the SDK’s cached configuration.
Check variants
Use get_variant to evaluate a flag with variants. The method returns a dictionary:
The SDK can return two forms of the disabled variant. In both cases, name is disabled and enabled is False:
Flag disabled or not found
Flag enabled, no variant resolved
The flag is enabled but no variant applies, for example when the flag has no variants configured:
Use feature_enabled when you need the flag’s state alongside the variant result, without a separate is_enabled call.
The enabled: False field marks the result as the SDK’s default disabled variant. This distinguishes it from a real variant that happens to be named disabled, which the SDK returns with enabled: True.
Fallback function
You can specify a fallback function for cases where the client doesn’t recognize the flag by using the fallback_function keyword argument:
The fallback function must accept the feature name and context as positional arguments, in that order.
The client evaluates the fallback function if the feature flag is not found or if an exception occurs during evaluation. If the fallback function itself raises an exception, is_enabled returns False and logs a warning.
The fallback function runs as part of flag evaluation, so keep it fast: its runtime adds directly to every is_enabled call that invokes it.
Evaluate all flags
Use evaluate_all_toggles to evaluate every flag the SDK knows for one context in a single call, for example to pass all flag states to a frontend. It returns a dictionary keyed by flag name:
enabled is the flag’s result for this context, and variant.enabled is whether a variant was assigned. Unlike is_enabled and get_variant, evaluate_all_toggles records no usage metrics and emits no impression events, so evaluating every flag doesn’t report them all as in use. Before the client has any flag configuration, it returns an empty dictionary.
To list the flags without evaluating them, use feature_definitions, which returns each flag’s project, type, and whether it is enabled in the environment.
Unleash context
The Unleash context holds properties such as user ID, session ID, and custom fields that drive flag evaluation, including strategy targeting, constraints, and stickiness.
Both is_enabled and get_variant accept a context dictionary as the second parameter:
The SDK enriches the context automatically before evaluation:
appNameis set fromapp_name, andenvironmentfrom the API token. Values passed in the context take precedence.currentTimeis set to the current UTC time when you don’t provide it, so date-based constraints work without extra setup.- Any top-level key that isn’t a standard context field is moved into
properties. Passing{"custom-property": "some-value"}at the top level is equivalent to nesting it underproperties.
Context values can be any type that has a __str__ implementation. Explicitly supported types are:
- Numerics
- Strings
- Dates
- UUIDs
Gradual rollout strategies require you to pass either a userId or a sessionId for stickiness to work correctly.
Stop the client
Call destroy() to shut down the SDK and flush any pending metrics to Unleash. If you use the client as a context manager, this happens automatically on exit.
A destroyed client cannot be reinitialized. Create a new instance if you need to reconnect.
Custom strategies
The SDK supports all built-in activation strategies. You can also define custom activation strategies if the built-in ones don’t cover your needs.
A custom strategy is a class that implements an apply method:
Once you’ve defined your strategy, register it when you initialize the client. The key must exactly match the strategy name in Unleash:
Events
To subscribe to SDK events, provide an event_callback function when you initialize the client. The callback receives a single event object.
The SDK invokes your callback on a dedicated background thread. Flag evaluation does not wait for the callback, so a slow callback cannot block your application. Two consequences of this design:
- The callback cannot read thread-local state from the calling thread, such as Flask’s
gobject, the current Django request, orcontextvars. - If the callback cannot keep up, the SDK drops events and logs a warning instead of blocking.
Set event_callback when you construct the client; reassigning it afterwards has no effect.
Both clients deliver events this way. With AsyncUnleashClient, the callback is still a regular function, not a coroutine, and it runs on the background thread, outside the event loop.
The SDK does not include a built-in event bus. The example below uses Blinker to send signals.
Impression data
When impression data is enabled for a flag, the SDK emits an impression event each time that flag is evaluated. Use impression events to track feature exposures in analytics tools.
Impression events arrive through the same event_callback with the event types FEATURE_FLAG and VARIANT. Each event includes the feature_name, the enabled result, and, for variant checks, the variant name:
Impact metrics
Impact metrics are lightweight, application-level time-series metrics stored and visualized directly inside Unleash. Use them to connect application data, such as request counts, error rates, or latency, to your feature flags and release plans.
Recording impact metrics with the SDK is currently available on Unleash Cloud only. If you self-host Unleash, connect external metrics from your Prometheus or VictoriaMetrics instance instead.
The SDK automatically attaches appName and environment labels to all impact metrics. The environment label comes from your API token.
Define and record metrics through the client’s impact_metrics property:
Metric types
Counter
Gauge
Histogram
Use counters for cumulative values that only increase, such as the total number of requests or errors.
Use gauges for values that can go up or down, such as current memory usage or active user count.
Histograms measure distributions such as latency or payload size.
Impact metrics are batched and sent on the same interval as standard SDK metrics, controlled by MetricsOptions(interval=...).
Bootstrap
By default, the SDK fetches your feature flags from the Unleash API at startup. To make your application more resilient, for example during network outages, you can bootstrap the client with a local or remote flag configuration:
- Create a
FileCache, or your ownBaseCacheimplementation. - Pre-seed it with feature flags using
bootstrap_from_dict,bootstrap_from_file, orbootstrap_from_url. - Pass the cache to the client constructor.
A bootstrapped client can evaluate flags before its first fetch from Unleash completes. The two clients load the bootstrapped configuration at different points:
UnleashClientloads it when you construct the client.AsyncUnleashClientloads it when you callinitialize_client().
The examples below use the sync client. Pass the cache to AsyncUnleashClient the same way.
Dictionary
File
URL
The headers argument is optional; use it when the bootstrap URL requires authentication. You can also set request_timeout, in seconds, for the bootstrap request.
Local caching and offline behavior
The SDK persists the latest flag configuration to an on-disk cache between restarts, using fcache. Set cache_directory to control where the cache is stored.
On startup, the SDK loads cached state first, then fetches fresh configuration from Unleash. If Unleash is temporarily unreachable, the SDK continues serving cached or bootstrapped values and keeps polling on PollingOptions(interval=...).
Custom cache
If you need a different storage backend, for example Redis, memory-only, or a custom database, provide your own cache implementation that extends BaseCache.
Below is an example custom cache using fcache under the hood:
Pass your cache instance to either client with the cache argument:
Custom HTTP transport
By default, UnleashClient sends its requests with requests, on a new session for every request, and AsyncUnleashClient with one aiohttp session. To configure the session, for example for a proxy or TLS settings, pass it to RequestsTransport or AiohttpTransport:
Sync
Async
Use a requests.Session for a proxy, a private certificate authority, a client certificate for mutual TLS, or hooks. A session also keeps connections open between polls.
The client never closes a session you pass in. Close it yourself after client.destroy().
Create the aiohttp.ClientSession inside a coroutine, because aiohttp needs a running event loop, and close it after the client:
AiohttpTransport sets its own timeout on every request and ignores any timeout on the session. To change it, pass timeout to the transport (default: 30 seconds).
The client never closes a transport or session that is passed in:
- If a session is passed, as in the example, close the session after the client.
- If
AiohttpTransport()is passed without a session, it opens a session of its own. Callawait transport.aclose()after closing the client.
Don’t mount a retrying adapter on the session: the SDK already retries failed flag fetches, and logs a warning if it finds one.
You can also use another HTTP library, such as httpx, by passing any object with an execute(request) -> Response method. For examples and more guidance, see custom HTTP transport in the SDK’s README.
Framework integration
UnleashClient runs background threads to poll for feature flags and send metrics, and both clients deliver events on a background thread. Some runtime environments, like WSGI servers and Celery workers, restrict threads or spawn new processes, and need extra setup to make sure the SDK works correctly.
WSGI
When using WSGI servers, for example with Flask or Django apps, be aware that:
- Many WSGI setups disable threading by default, so set
enable-threadsin your WSGI config. Without it,UnleashClientcannot poll for updates in the background, and neither client can deliver events to your callback. - When running under uWSGI with multiple processes using the
--processesoption, you may need to enable thelazy-appsoption. This ensures each process gets a fresh SDK instance.
See The Art of Graceful Reloading for more details.
Celery
When using the SDK in Celery tasks, initialize it inside the worker_process_init event. Otherwise, the worker may run but won’t poll for feature flag updates or receive event updates.
Multi-process setups
When each new process creates its own client, every process runs an independent poll cycle. With many short-lived processes, this wastes system resources and counts against your Unleash usage.
To avoid this, set up a leader and follower pattern: one process keeps the on-disk cache fresh, and the other processes call initialize_client(fetch_toggles=False). These clients then read flag configuration from the cache instead of polling the Unleash API. They reload the cache every PollingOptions(interval=...) seconds, 15 by default.
This mode is available with UnleashClient. AsyncUnleashClient accepts the fetch_toggles argument for compatibility and always polls.
Unit testing
To test code that depends on feature flags without a running Unleash instance, bootstrap a cache with the flag state you want and turn off all network activity:
With fetch_toggles=False, the client loads flags from the cache only, so tests are deterministic and make no network requests. This approach uses UnleashClient, because fetch_toggles=False is specific to the sync client.
With either client, you can also pass a fake transport that answers the SDK’s requests itself. See tests without a server in the SDK’s README.
Migrating to v7
Version 7 requires Python 3.10 or later and changes how you configure the client:
- The API token is passed as
api_keyinstead of anAuthorizationheader incustom_headers. - All arguments after
urlandapp_nameare keyword-only, and most settings moved into the option objects described in configuration options. - HTTP settings moved to the transport:
custom_headersandrequest_timeoutbecomeRequestsTransport(headers=..., timeout=...), orAiohttpTransport(...)forAsyncUnleashClient.custom_optionshas been removed; configure arequests.Sessioninstead and passRequestsTransport(session). See custom HTTP transport. request_retrieshas been removed. A failed flag fetch is retried up to 3 times, with a short backoff.- The
environmentargument has been removed. The environment comes from your API token. - The constructor raises a
ValueErrorifurlis not an absolutehttp://orhttps://URL with a host. AsyncUnleashClientraises aValueErrorformode=StreamingOptions(). In v6, it accepted streaming settings and polled.- Client settings are fixed at creation. The
unleash_*settings properties,strategy_mapping, andconnector_modehave been removed.
The environment field of the Unleash context and the environment label on impact metrics now come from your API token. In v6, the context field was default unless you set the environment argument. If a strategy or constraint uses the environment context field, for example environment IN [default], update it to match your token’s environment, or pass the value in the context:
For the complete mapping from v6 arguments to v7, read the v7 migration guide.
Migrating to v6
If you use custom strategies or access the features property on the Unleash client, read the complete v6 migration guide before upgrading.
Troubleshooting
All flags evaluate to false
If is_enabled always returns False:
- Confirm the flag exists and is enabled in the target environment.
- Verify
urlpoints to your Unleash API base URL, for examplehttps://<your-unleash-instance>/api. - Verify
api_keyis a valid backend API token. - Verify the SDK completed its initial sync by checking that the
READYevent fired.is_initializedbeingTrueonly means thatinitialize_client()has completed. - Verify your Unleash context fields match your rollout strategy and constraints.
Flags never update in uWSGI or Celery workers
If the SDK initializes but flag changes never arrive:
- Under uWSGI, enable the
enable-threadsoption; the SDK needs threads to poll in the background. With multiple processes, also considerlazy-apps. - In Celery workers, initialize the client inside the
worker_process_initsignal.
See Framework integration for details.