Flagsmith Flag Engine

10.0.3 · active · verified Sun Apr 12

The `flagsmith-flag-engine` is the core evaluation engine for the Flagsmith API, enabling feature flag and remote configuration management. It processes environment, feature, segment, and identity data to determine flag states locally. The library is actively maintained, with version 10.0.3 currently available, and frequent releases that often include minor and major updates with breaking changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `flagsmith-flag-engine` directly to evaluate feature flags for a given identity. It simulates an `EnvironmentModel` with defined feature states and a segment rule, then creates an `IdentityModel` with traits. Finally, it uses `evaluate_identity_flags` to determine the active flags and segments for that identity based on the engine's logic. This is typically done within a server-side SDK which fetches the `EnvironmentModel` from the Flagsmith API.

import os
from flagsmith_engine.environments import EnvironmentModel, FeatureStateModel, SegmentModel
from flagsmith_engine.identities import IdentityModel, TraitModel
from flagsmith_engine.evaluator import evaluate_identity_flags
from flagsmith_engine.segments import SegmentCondition, SegmentRule, SEGMENT_CONDITION_OPERATORS

# Simulate an Environment structure (typically fetched from Flagsmith API)
environment_data = {
    "id": 1,
    "api_key": os.environ.get('FLAGSMITH_ENVIRONMENT_KEY', 'your_environment_key'),
    "project": {"id": 1, "name": "Test Project", "organisation": {"id": 1, "name": "Test Org"}},
    "feature_states": [
        {
            "id": 101,
            "feature": {"id": 1, "name": "my_test_feature", "type": "STANDARD"},
            "enabled": True,
            "feature_state_value": "default_value"
        },
        {
            "id": 102,
            "feature": {"id": 2, "name": "another_feature", "type": "STANDARD"},
            "enabled": False,
            "feature_state_value": "false_value"
        }
    ],
    "segments": [
        {
            "id": 201,
            "name": "Test Segment",
            "rules": [
                {
                    "type": "ALL",
                    "conditions": [
                        {
                            "operator": SEGMENT_CONDITION_OPERATORS["EQUAL"],
                            "property": "plan",
                            "value": "premium"
                        }
                    ]
                }
            ]
        }
    ]
}

environment = EnvironmentModel.parse_obj(environment_data)

# Create an identity
identity_traits = [TraitModel(trait_key="plan", trait_value="premium")]
identity = IdentityModel(identifier="test_user_123", environment_api_key=environment.api_key, traits=identity_traits)

# Evaluate flags for the identity
feature_states, identities_segments = evaluate_identity_flags(identity, environment, None)

print(f"Identity '{identity.identifier}' is in segments: {[s.name for s in identities_segments]}")
for fs in feature_states:
    print(f"Feature '{fs.feature.name}': Enabled={fs.enabled}, Value='{fs.feature_state_value}'")

# Example with a user not in the segment
identity_traits_basic = [TraitModel(trait_key="plan", trait_value="basic")]
identity_basic = IdentityModel(identifier="basic_user_456", environment_api_key=environment.api_key, traits=identity_traits_basic)
feature_states_basic, identities_segments_basic = evaluate_identity_flags(identity_basic, environment, None)

print(f"\nIdentity '{identity_basic.identifier}' is in segments: {[s.name for s in identities_segments_basic]}")
for fs in feature_states_basic:
    print(f"Feature '{fs.feature.name}': Enabled={fs.enabled}, Value='{fs.feature_state_value}'")

view raw JSON →