OpenFeature Provider (Python)
Overview
This guide covers using Mixpanel’s Feature Flags through the OpenFeature standard with the Mixpanel Python OpenFeature provider. OpenFeature provides a vendor-agnostic API for feature flag evaluation, allowing you to switch between providers without changing your application code.
For the native Mixpanel SDK approach, see the Feature Flags (Python) guide.
Prerequisites
- Enterprise subscription plan with Feature Flags enabled
- Python 3.9 or higher
- Project Token from your Mixpanel Project Settings
Installation
pip install mixpanel-openfeature openfeature-sdkQuick Start
from mixpanel_openfeature import MixpanelProvider
from mixpanel.flags.types import LocalFlagsConfig
from openfeature import api
# 1. Create and register the provider with local evaluation
provider = MixpanelProvider.from_local_config(
"YOUR_PROJECT_TOKEN",
LocalFlagsConfig(token="YOUR_PROJECT_TOKEN"),
)
api.set_provider(provider)
# 2. Get a client and evaluate flags
client = api.get_client()
show_new_feature = client.get_boolean_value("new-feature-flag", False)
if show_new_feature:
print("New feature is enabled!")Initialization
The provider supports three initialization methods depending on your evaluation strategy.
Local Evaluation (Recommended)
Evaluates flags locally using cached flag definitions polled from Mixpanel. This minimizes latency since there is no network call at evaluation time.
Targeting by Mixpanel cohorts and sticky variants are not supported in Local Evaluation mode.
from mixpanel_openfeature import MixpanelProvider
from mixpanel.flags.types import LocalFlagsConfig
provider = MixpanelProvider.from_local_config(
"YOUR_PROJECT_TOKEN",
LocalFlagsConfig(token="YOUR_PROJECT_TOKEN"),
)Remote Evaluation
Evaluates flags by making a request to Mixpanel’s servers for each evaluation. Use this when you need real-time flag values or cohort-based targeting.
from mixpanel_openfeature import MixpanelProvider
from mixpanel.flags.types import RemoteFlagsConfig
provider = MixpanelProvider.from_remote_config(
"YOUR_PROJECT_TOKEN",
RemoteFlagsConfig(token="YOUR_PROJECT_TOKEN"),
)Using an Existing Mixpanel Instance
If your application already has a Mixpanel instance configured for tracking, you can wrap its flags provider:
from mixpanel import Mixpanel
from mixpanel.flags.types import LocalFlagsConfig
from mixpanel_openfeature import MixpanelProvider
mp = Mixpanel("YOUR_PROJECT_TOKEN", local_flags_config=LocalFlagsConfig(token="YOUR_PROJECT_TOKEN"))
mp.local_flags.start_polling_for_definitions()
provider = MixpanelProvider(mp.local_flags)Usage
Flag Types and Evaluation Methods
| Mixpanel Flag Type | Variant Values | OpenFeature Method |
|---|---|---|
| Feature Gate | True / False | get_boolean_value() |
| Experiment | boolean, string, number, or JSON object | get_boolean_value(), get_string_value(), get_integer_value(), get_float_value(), or get_object_value() |
| Dynamic Config | JSON object | get_object_value() |
client = api.get_client()
# Feature Gate
is_feature_on = client.get_boolean_value("new-checkout", False)
# Experiment with string variants
button_color = client.get_string_value("button-color-test", "blue")
# Experiment with numeric variants
max_items = client.get_integer_value("max-items", 10)
threshold = client.get_float_value("score-threshold", 0.5)
# Dynamic Config
feature_config = client.get_object_value("homepage-layout", {"layout": "default"})Evaluation Context
Pass context to provide user attributes for targeting:
from openfeature.evaluation_context import EvaluationContext
context = EvaluationContext(
targeting_key="user-123",
attributes={
"email": "user@example.com",
"plan": "premium",
"beta_tester": True,
},
)
value = client.get_boolean_value("premium-feature", False, context)Unlike some providers, targetingKey is not used as a special bucketing key. It is passed as another context property. Mixpanel’s server-side configuration determines which properties are used for targeting and bucketing.
Full Resolution Details
details = client.get_boolean_details("my-feature", False)
print(details.value) # The resolved value
print(details.variant) # The variant key from Mixpanel
print(details.reason) # Why this value was returned
print(details.error_code) # Error code if evaluation failedAccessing the Underlying Mixpanel Instance
When using from_local_config or from_remote_config, you can access the Mixpanel instance for tracking:
mp = provider.mixpanelShutdown
provider.shutdown()Error Handling
The provider uses OpenFeature’s standard error codes:
| Error Code | When |
|---|---|
PROVIDER_NOT_READY | Flags evaluated before local provider has finished loading definitions |
FLAG_NOT_FOUND | The requested flag does not exist in Mixpanel |
TYPE_MISMATCH | The flag value type does not match the requested type |
Troubleshooting
Flags Always Return Default Values
- Provider not ready (local evaluation): Flag definitions are polled asynchronously. Allow time for the initial fetch to complete.
- Invalid project token: Verify the token matches your Mixpanel project.
- Flag not configured: Verify the flag exists and is enabled in your Mixpanel project.
Type Mismatch Errors
- Verify the flag’s value type in Mixpanel matches your evaluation method (e.g., string
"true"requiresget_string_value(), notget_boolean_value()). - Use
get_object_value()for JSON objects. - Integer evaluation accepts whole-number
floatvalues. Float evaluation accepts any numeric type.
Was this page useful?