> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mixpanel.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenFeature Provider (Python)

## Overview

This guide covers using Mixpanel's [Feature Flags](/docs/featureflags) through the [OpenFeature](https://openfeature.dev/) 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)](/docs/tracking-methods/sdks/python/python-flags) guide.

## Prerequisites

* Enterprise subscription plan with Feature Flags enabled
* Python 3.9 or higher
* Project Token from your [Mixpanel Project Settings](/docs/orgs-and-projects/managing-projects#find-your-project-tokens)

## Installation

```bash theme={"system"}
pip install mixpanel-openfeature openfeature-sdk
```

## Quick Start

```python theme={"system"}
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.

<Warning>
  Targeting by Mixpanel cohorts and sticky variants are not supported in Local Evaluation mode.
</Warning>

```python theme={"system"}
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.

```python theme={"system"}
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:

```python theme={"system"}
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()`                                                                                             |

```python theme={"system"}
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:

```python theme={"system"}
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)
```

<Note>
  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.
</Note>

### Full Resolution Details

```python theme={"system"}
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 failed
```

### Accessing the Underlying Mixpanel Instance

When using `from_local_config` or `from_remote_config`, you can access the Mixpanel instance for tracking:

```python theme={"system"}
mp = provider.mixpanel
```

### Shutdown

```python theme={"system"}
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

1. **Provider not ready (local evaluation):** Flag definitions are polled asynchronously. Allow time for the initial fetch to complete.
2. **Invalid project token:** Verify the token matches your Mixpanel project.
3. **Flag not configured:** Verify the flag exists and is enabled in your Mixpanel project.

### Type Mismatch Errors

1. Verify the flag's value type in Mixpanel matches your evaluation method (e.g., string `"true"` requires `get_string_value()`, not `get_boolean_value()`).
2. Use `get_object_value()` for JSON objects.
3. Integer evaluation accepts whole-number `float` values. Float evaluation accepts any numeric type.
