Skip to main content

flagsmith-openfeature-provider-python

The Flagsmith provider allows you to connect to your Flagsmith instance through the OpenFeature SDK

Python SDK usage

Install dependencies

First, you'll need to install the OpenFeature SDK and the Flagsmith Provider.

pip install openfeature-sdk openfeature-provider-flagsmith

Using the Flagsmith Provider with the OpenFeature SDK

To create a Flagsmith provider you will need to provide a number of arguments. These are shown and described below. See the Flagsmith docs for further information on the configuration options available for the Flagsmith python client.

from flagsmith import Flagsmith
from openfeature_flagsmith.provider import FlagsmithProvider

provider = FlagsmithProvider(
    # Provide an instance of the Flagsmith python client.
    # Required: True
    client=Flagsmith(...),
    
    # By enabling the use_flagsmith_defaults setting, you can instruct the OpenFeature SDK to use
    # the default logic included in the Flagsmith client as per the docs here: 
    # https://docs.flagsmith.com/clients/server-side#managing-default-flags. This will override the 
    # default provided at evaluation time in the OpenFeature SDK in most cases (excluding those where 
    # an unexpected exception happens in the Flagsmith client itself).
    # Required: False
    # Default: False
    use_flagsmith_defaults=False,
    
    # By default, when evaluating the boolean value of a feature in the OpenFeature SDK, the Flagsmith 
    # OpenFeature Provider will use the 'Enabled' state of the feature as defined in Flagsmith. This 
    # behaviour can be changed to use the 'value' field defined in the Flagsmith feature instead by 
    # enabling the use_boolean_config_value setting. 
    # Note: this relies on the value being defined as a Boolean in Flagsmith. If the value is not a 
    # Boolean, an error will occur and the default value provided as part of the evaluation will be 
    # returned instead.  
    # Required: False
    # Default: False
    use_boolean_config_value=False,
    
    # By default, the Flagsmith OpenFeature Provider will raise an exception (triggering the 
    # OpenFeature SDK to return the provided default value) if the flag is disabled. This behaviour
    # can be configured by enabling this flag so that the Flagsmith OpenFeature provider ignores
    # the enabled state of a flag when returning a value.
    # Required: False
    # Default: False
    return_value_for_disabled_flags=False,
)

The provider can then be used with the OpenFeature client as per the documentation.

Tracking and experimentation

The provider supports the OpenFeature tracking API (an experimental OpenFeature capability), which lets you record custom events and flag exposures for experimentation.

Tracking requires events to be enabled on the Flagsmith client (flagsmith ≥5.5). The provider acts as a thin delegate — all buffering and flushing is managed by the client.

from flagsmith import Flagsmith
from openfeature import api
from openfeature_flagsmith import FlagsmithProvider

client = Flagsmith(
    environment_key="your-environment-key",
    enable_events=True,
)

provider = FlagsmithProvider(client=client)
api.set_provider(provider)
of_client = api.get_client()

If events are not enabled on the Flagsmith client, all tracking calls are silently dropped.

Recording exposures

An exposure marks an identity as having experienced an experiment variant. Exposures are never recorded automatically: evaluating a flag does not expose anyone. There are three ways to record them, from most to least recommended.

1. The exposure hook (recommended). Attach FlagsmithExposureHook to the evaluations that are your experiment — attaching the hook is the experiment declaration:

from openfeature.evaluation_context import EvaluationContext
from openfeature.flag_evaluation import FlagEvaluationOptions
from openfeature_flagsmith import FlagsmithExposureHook

hook = FlagsmithExposureHook(provider)

details = of_client.get_string_details(
    "my_experiment_flag",
    "control",
    EvaluationContext(targeting_key="user-123"),
    FlagEvaluationOptions(hooks=[hook]),
)

The hook records an exposure only when the flag resolved with a variant and reason SPLIT — a multivariate percentage-split assignment (enabled, identified, not offline). With flagsmith ≥6.2 resolution reasons come from the Flagsmith engine (e.g. SPLIT; weight=30; the engine's DEFAULT maps to STATIC); on older SDKs or APIs the provider infers them. Repeated evaluations are safe: duplicate exposures are deduplicated downstream.

2. Explicit track(). Use the reserved feature_flag.exposure event name when you need to record an exposure decoupled from evaluation:

from openfeature.track import TrackingEventDetails
from openfeature_flagsmith import EXPOSURE_TRACKING_EVENT

# With an explicit variant: sent as rendered.
of_client.track(
    EXPOSURE_TRACKING_EVENT,
    evaluation_context=EvaluationContext(targeting_key="user-123"),
    tracking_event_details=TrackingEventDetails(
        attributes={"flag_key": "my_experiment_flag", "variant": "treatment"}
    ),
)

# Without a variant: the provider resolves the flag for the targeting key and
# records the exposure only if the flag exists, is enabled and has a variant.
of_client.track(
    EXPOSURE_TRACKING_EVENT,
    evaluation_context=EvaluationContext(targeting_key="user-123"),
    tracking_event_details=TrackingEventDetails(
        attributes={"flag_key": "my_experiment_flag"}
    ),
)

3. The native Flagsmith client. client.get_experiment_flag(...) / client.track_exposure_event(...) work as documented in the Flagsmith docs and share the same event pipeline.

Custom events

Any other event name is forwarded as a plain Flagsmith event. TrackingEventDetails.value must be numeric and is sent as the event value; attributes become event metadata; context traits are attached to the event.

of_client.track(
    "purchase",
    evaluation_context=EvaluationContext(
        targeting_key="user-123",
        attributes={"plan": "premium"},
    ),
    tracking_event_details=TrackingEventDetails(
        value=99.77,
        attributes={"currency": "USD"},
    ),
)

Caveats

  • Anonymous contexts: exposures require a targeting_key; without one they are skipped (logged at info).
  • Reserved names: event names starting with $ are reserved for Flagsmith system events and are dropped with a warning — use EXPOSURE_TRACKING_EVENT to record exposures.
  • Transient identities (Python provider only, remote evaluation only): set the context attribute "transient": True to evaluate an identity without persisting it. The variant-less exposure path honors it too.

Evaluation Context

The evaluation context supports traits in two ways:

  1. Flat top-level attributes
  2. A nested traits object

The two forms are merged and sent to Flagsmith, with the traits object taking precedence if keys conflict.

context = EvaluationContext( # Traits are: {"abc":"def", "foo": "bar2"}
    targeting_key="user",
    attributes={
        "foo": "bar", 
        "abc": "def", 
        "traits": {"foo": "bar2"}
    },
)

Release files for openfeature-provider-flagsmith 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openfeature-provider-flagsmith 1.0.0
File Size Uploaded
openfeature_provider_flagsmith-1.0.0.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openfeature-provider-flagsmith 1.0.0
File Interpreter ABI Platform
openfeature_provider_flagsmith-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.7 kB

Release files / openfeature_provider_flagsmith-1.0.0.tar.gz

Download URL openfeature_provider_flagsmith-1.0.0.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
21b3ef02e6c0716a81500e39b1f489028765e13ccf76df191fc2d2315ae2bbee
BLAKE2b-256 checksum
How to use checksums
f2c273618a78380b3996cecb71827ee58dcaab699f54b428b40ed1a037fe1fbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 7, 2026.

Transparency log

Release files / openfeature_provider_flagsmith-1.0.0-py3-none-any.whl

Download URL openfeature_provider_flagsmith-1.0.0-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9cc510ec288ae38a1ae8b7d0cad38ea46a802c8b5a20e7df197d5c8dfc1a01bd
BLAKE2b-256 checksum
How to use checksums
747b1ded625a9c1bacc0564378fe309c8af19282474e4e35d3f1fa71db99a2c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 7, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.2.0

2 release files

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page