OpenFeature Provider (Python)
Overview
The Mixpanel OpenFeature provider allows you to use Mixpanel Feature Flags through the standardized OpenFeature API. This provider wraps the Mixpanel Python SDK’s feature flags, letting you use the OpenFeature Python SDK with Mixpanel as the backend.
For the core Feature Flags SDK guide, see Feature Flags (Python).
Prerequisites
- You are on an Enterprise subscription plan
- You have the Mixpanel Python SDK installed
- You have your Project Token from your Mixpanel Project Settings
Installation
pip install mixpanel-openfeature-provider openfeature-sdkUsage
The provider wraps either a local or remote flags provider from the Mixpanel SDK.
With Local Evaluation
from openfeature import api
from mixpanel_openfeature import MixpanelProvider
from mixpanel.flags.types import LocalFlagsConfig
config = LocalFlagsConfig(
enable_polling=True,
polling_interval_in_seconds=60
)
# Create provider with local evaluation (automatically starts polling)
provider = MixpanelProvider.from_local_config("YOUR_PROJECT_TOKEN", config)
api.set_provider(provider)
client = api.get_client()
show_new_ui = client.get_boolean_value("new-ui", False)With Remote Evaluation
from mixpanel.flags.types import RemoteFlagsConfig
config = RemoteFlagsConfig(
api_host="api.mixpanel.com"
)
provider = MixpanelProvider.from_remote_config("YOUR_PROJECT_TOKEN", config)
api.set_provider(provider)
client = api.get_client()
variant = client.get_string_value("checkout-flow", "control")With an Existing Flags Provider
If you already have a configured Mixpanel instance, you can wrap its flags provider directly:
from mixpanel import Mixpanel
from mixpanel_openfeature import MixpanelProvider
mp = Mixpanel("YOUR_PROJECT_TOKEN", local_flags_config={
"enable_polling": True,
"polling_interval_in_seconds": 60
})
mp.local_flags.start_polling_for_definitions()
provider = MixpanelProvider(mp.local_flags)
api.set_provider(provider)Supported Flag Types
| OpenFeature Method | Python Type |
|---|---|
get_boolean_value / get_boolean_details | bool |
get_string_value / get_string_details | str |
get_integer_value / get_integer_details | int |
get_float_value / get_float_details | float |
get_object_value / get_object_details | dict |
Context and Identity
The evaluation context passed to each flag evaluation is forwarded to the Mixpanel flags provider.
targeting_keyhas no special meaning in this provider. It is treated as a regular context property.- Identity should be managed through the Mixpanel SDK (e.g., setting
distinct_idin the context).
Error Handling
The provider returns the default value on all errors, with an error code indicating the cause:
| Error Code | Condition |
|---|---|
PROVIDER_NOT_READY | The provider has not been initialized |
FLAG_NOT_FOUND | The requested flag does not exist |
TYPE_MISMATCH | The flag value does not match the requested type |
Use get_details() methods instead of get_value() to inspect error codes:
details = client.get_boolean_details("my-flag", False)
if details.error_code:
print(f"Flag error: {details.error_code} - {details.error_message}")Lifecycle
shutdown()delegates to the underlying flags provider to clean up resources (e.g., stop polling).- The reason code for successful evaluations is
STATIC.
Was this page useful?