> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.getunleash.io/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.getunleash.io/_mcp/server.

# Python OpenFeature provider

> Set up the Unleash OpenFeature provider for Python to evaluate feature flags through the OpenFeature API.

The [Unleash OpenFeature Python provider](https://github.com/Unleash/unleash-openfeature-python-provider) lets Python applications evaluate Unleash feature flags through the [OpenFeature](https://openfeature.dev/) API.

The provider wraps the [Unleash Python SDK](/sdks/python) and uses the same backend SDK behavior for polling, local evaluation, caching, metrics, and shutdown.

## Requirements

* Python 3.10 or later

## Installation

Install the provider. The provider declares the OpenFeature Python SDK and the [Unleash Python SDK](/sdks/python) as dependencies, so you don't need to add them separately.

#### pip

```bash
pip install unleash-openfeature-python-provider
```

#### uv

```bash
uv add unleash-openfeature-python-provider
```

## Configuration

Create an `UnleashFlagProvider` with the same [options](/sdks/python#configuration-options) you would pass to the Unleash Python SDK client, then register it with OpenFeature. The provider builds and owns the Unleash client.

```python
from openfeature import api

from unleash_openfeature_python_provider import UnleashFlagProvider

provider = UnleashFlagProvider(
    url="<YOUR_UNLEASH_URL>/api",
    app_name="my-python-app",
    custom_headers={"Authorization": "<YOUR_API_TOKEN>"},
)

api.set_provider_and_wait(provider)
```

`set_provider_and_wait` starts the Unleash client and waits for startup to complete before the provider is registered for use. After startup, the SDK continues refreshing feature flag data in the background.

## Evaluate a flag

Build an OpenFeature client and evaluate flags through the OpenFeature API:

```python
from openfeature import api
from openfeature.evaluation_context import EvaluationContext

client = api.get_client()

context = EvaluationContext(targeting_key="user-123")

enabled = client.get_boolean_value("my-feature", False, context)
```

The provider supports all OpenFeature evaluation methods. Boolean evaluation uses the flag's enabled state. All other types resolve from the payload of the flag's [variant](/concepts/feature-flag-variants):

* String values accept `string` and `csv` payloads.
* Integer and float values accept `number` payloads.
* Object values accept `json` payloads.

If the flag doesn't exist, the flag is disabled, or the variant payload is missing, has a different type, or can't be parsed, the evaluation returns the default value. The detail methods, such as `get_string_details`, additionally report the evaluation reason and the assigned variant name.

## Context mapping

The provider maps the OpenFeature evaluation context to an [Unleash context](/concepts/unleash-context) before each evaluation.

The following OpenFeature fields map directly to top-level Unleash context fields:

* `currentTime`
* `userId`
* `sessionId`
* `remoteAddress`
* `environment`
* `appName`

If you set `targeting_key`, the provider maps it to `userId`. The `targeting_key` takes precedence over a `userId` field set on the evaluation context.

The provider adds all other scalar fields, such as strings, numbers, booleans, and date-time values, to the Unleash context [properties](/concepts/unleash-context#the-properties-field). It discards nested values, such as lists and dictionaries, and logs each discarded field at debug level using the standard `logging` module.

## Shutdown

Shut down OpenFeature when your application exits. This also shuts down the underlying Unleash client.

```python
api.shutdown()
```

## Example

To try the provider against your Unleash instance, clone the [provider repository](https://github.com/Unleash/unleash-openfeature-python-provider) and run the [boolean flag example](https://github.com/Unleash/unleash-openfeature-python-provider/blob/main/examples/boolean_flag.py) with your Unleash URL and API token:

```bash
uv run python examples/boolean_flag.py \
  --url '<YOUR_UNLEASH_URL>/api' \
  --api-key "$UNLEASH_API_KEY" \
  --flag-key my-feature \
  --targeting-key user-123
```