Skip to main content

flagnetic

Python SDK for Flagnetic, self-hosted feature flags and experiments.

pip install flagnetic

It needs Python 3.11 or later and installs no dependency: the package is pure Python, its regular expression engine included, so it installs the same on Alpine and on free-threaded builds. Supported: the Python releases still supported upstream (a release reaching its end of life is dropped in a minor version) and, with the extra, openfeature-sdk 0.9 and later. The package speaks the SDK API /sdk/v1 and works with every Flagnetic server from 0.1.0 on (compatibility); docs/sdks/python.md is how it is deployed and tested.

Because that engine is pure Python, the matches operator treats a context value longer than 64 KiB of UTF-8 as a non-match, not an error: the cost of a match is the input times the pattern, and a caller chose the input.

Quick start

import flagnetic

client = flagnetic.Client("<server SDK key>", "https://flags.example.com")
client.ready(timeout=5)  # fresh, last_good or built_in; never raises

context = flagnetic.Context(units={"user": "u-1001"}, attributes={"plan": "pro"})
if client.get_bool(context, "new-checkout", False):
    ...

client.close()

The getters never block and never raise: they read the ruleset the client holds and serve the default you pass when they cannot serve the flag. One background thread fetches the ruleset with If-None-Match, holds the ruleset stream (a kill switch reaches the client within two seconds), polls every 30 seconds while the stream is down, and backs off with jitter. A stream is stable once it has delivered a heartbeat: one the server closes before that (a draining replica, a proxy that ends the stream early) is a failure like any other and logs stream_dropped with the cause closed_early, while the close of a stable stream waits 0 to 2 seconds. store="/var/lib/app/flagnetic.json" keeps the last good ruleset across restarts. The file is headed by a digest of the SDK key, so one written under another key (another environment's, say) is discarded with store_failed.

A flag value nesting arrays and objects deeper than 32 levels, which the server refuses to store, fails that flag alone: its getters serve your default with inconsistent_ruleset, condition_invalid names the variation once per flag and ruleset version, and every other flag of the ruleset serves.

Experiments and events

The same thread reports an evaluation summary every minute (counts per flag, variation and reason, never a unit ID or a value) and sends exposures and aliases every two seconds, retrying with the same event IDs so the server drops a resend. An evaluation alone is not an exposure: call exposure where the unit actually meets the behaviour, or turn on automatic exposure for the flag.

price = client.get_string(context, "pricing", "Built-in price")
client.exposure(context, "pricing")  # reports the assignment just served
client.alias("anonymous", "d-8f1c", "user", "u-1001")  # after sign-up
client.flush(timeout=5)  # close() flushes too

Pre-forking servers and asyncio

Under gunicorn or uWSGI, build the client before the workers fork, or in each worker: a forked worker restarts the thread by itself, refetches and reports its own events. No hook is needed.

The getters never block, so they are safe in a coroutine. Start-up and shutdown wait on a worker thread:

stage = await client.aready(timeout=5)
await client.aflush()
await client.aclose()

Tests and local overrides

flagnetic.testing builds offline clients that make no request and start no thread:

from flagnetic import testing

client = testing.from_values({"new-checkout": True})  # or from_document(...), from_file(path)
client.set("new-checkout", False)  # change listeners fire
client.reload()  # back to what it was built from
client.recorder.exposures()  # what it would have reported

Client(..., allow_overrides=True) enables client.local_overrides.set(key, value), served with reason local_override; without the option set raises LocalOverridesNotAllowed, and a protected environment refuses and drops every local override.

OpenFeature

pip install 'flagnetic[openfeature]'

The extra installs openfeature-sdk 0.9 or later and the provider in flagnetic.openfeature. It takes the client's arguments and options and builds the client itself:

from openfeature import api
from openfeature.evaluation_context import EvaluationContext
from openfeature.track import TrackingEventDetails
from flagnetic.openfeature import Provider

provider = Provider("<server SDK key>", "https://flags.example.com", unit_type="user")
api.set_provider_and_wait(provider)  # api.set_provider before openfeature-sdk 0.10
client = api.get_client()

context = EvaluationContext("u-1001", {"plan": "pro", "units": {"account": "a-17"}})
price = client.get_string_value("pricing", "Built-in price", context)
client.track("exposure", context, TrackingEventDetails().add("flag", "pricing"))
client.track(
    "alias",
    tracking_event_details=TrackingEventDetails()
    .add("fromUnitType", "anonymous")
    .add("fromUnitId", "d-8f1c")
    .add("toUnitType", "user")
    .add("toUnitId", "u-1001"),
)

track("exposure") records the exposure of the provider's most recent evaluation of that flag for the same units, with the context the evaluation had when a before hook added attributes to it, as the client's exposure does.

The targeting key is the unit ID of unit_type; the units attribute adds further units by unit type; every other attribute is an attribute of the context. Reasons map as in every Flagnetic provider:

Flagnetic OpenFeature
rule, prerequisite_failed, override, targeting_match TARGETING_MATCH
rollout, experiment SPLIT
fallthrough DEFAULT
off DISABLED
local_override STATIC
error ERROR
fallback ERROR, with FLAG_NOT_FOUND, TYPE_MISMATCH or GENERAL

The flag metadata carries the Flagnetic reason as flagnetic_reason, with stage, ruleset_version, variation, overridden and, for an experiment, its experiment_* fields. The provider emits PROVIDER_CONFIGURATION_CHANGED with the changed flag keys, PROVIDER_STALE while the client serves its last good ruleset, PROVIDER_READY once it is fresh again and PROVIDER_ERROR when the SDK key is revoked. Initialization waits up to ready_timeout seconds (default 10) for the first fetch and never fails. provider.client is the wrapped client, for local overrides, flush and the recorder of an offline provider (Provider("", "", offline=document)).

What 1.0 promises

From 1.0 the public API below changes only additively until 2.0: a name is added, never renamed, removed or given another meaning, and a removal is announced first in a minor release and made in a major one. The public API is:

  • Client: flagnetic.Client with its keyword options, the getters get_bool, get_string, get_number, get_int, get_json and get_detail, ready, stage, status, on_change, on_stage, exposure, alias, flush, close, the aready, aflush and aclose forms, recorder and local_overrides.
  • Values: flagnetic.Context, Detail, Experiment, Stage, Status, Document (the accepted forms of an offline ruleset) and, of what recorder returns, Exposure, Alias, SummaryCount and Recorder.
  • Local overrides: LocalOverrides and its methods set, remove, clear and list.
  • Errors: FlagneticError and the two it is the base of, LocalOverridesNotAllowed and LocalOverrideRefused. A bad option is a ValueError and an override value of the wrong type a TypeError; getters never raise.
  • Logging: Logger, the hook's type, and the event names in flagnetic.events, which are the strings operators alert on.
  • Tests: the functions of flagnetic.testing and the TestClient they return.
  • OpenFeature: flagnetic.openfeature.Provider, its arguments and the flag metadata it fills.
  • flagnetic.__version__.

Everything else is internal and may change in any release: every module and name starting with an underscore, the modules behind them, and what is not named above. Import the public names from flagnetic, flagnetic.events, flagnetic.testing and flagnetic.openfeature.

Development

uv sync --extra openfeature
uv run ruff check . && uv run ruff format --check . && uv run mypy
uv run pytest
uv run pytest e2e  # the smoke against two real replicas (below)

The smoke needs the end-to-end harness of e2e/ running: start flagnetic-e2e up with FLAGNETIC_E2E_SEED naming a file, then run uv run pytest e2e with the same FLAGNETIC_E2E_SEED and, unless flagnetic-e2e is on the PATH, FLAGNETIC_E2E_CMD naming it. It ends by revoking the seed's server key, so bring the harness up again before running it a second time.

The tests run the shared conformance suite in conformance/ against the evaluator, play the protocol scenarios and degradation cases against a mock server through the public API, and measure that evaluating one flag of a 1,000-flag ruleset stays under a millisecond at the 99th percentile.

Metadata

Release files for flagnetic 0.2.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 flagnetic 0.2.0
File Size Uploaded Provenance Actions
flagnetic-0.2.0.tar.gz 205.7 kB With provenance Details for flagnetic-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flagnetic 0.2.0
File Interpreter ABI Platform Actions
flagnetic-0.2.0-py3-none-any.whl Python 3 none any Details for flagnetic-0.2.0-py3-none-any.whl

Total release size: 351.0 kB

Release files / flagnetic-0.2.0.tar.gz

Download URL flagnetic-0.2.0.tar.gz
Size 205.7 kB
Tags Source
SHA-256 checksum
How to use checksums
92b51355aade72a4265c9280cc39442c0871554904553ea2d6b9fd2ff0ef410a
BLAKE2b-256 checksum
How to use checksums
defb410091fa2063253d9f5e49d695a6328f9770b08acd15f1e68b6b30dc0ef9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 22, 2026.

Transparency log

Release files / flagnetic-0.2.0-py3-none-any.whl

Download URL flagnetic-0.2.0-py3-none-any.whl
Size 145.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
faaff544d7904c34485c9405aafee6fa5b706e2d93b09ad3eefea613ed095127
BLAKE2b-256 checksum
How to use checksums
52a3031a8b4aac1a4a79d1c0b063d2fbc5b71cf49bbc7edbe9173475287ef80e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 22, 2026.

Transparency log

Security for flagnetic 0.2.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for flagnetic-0.2.0.tar.gz

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 Nov 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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