The flipt-client-python library contains the Python source code for the Flipt client-side evaluation client.
pip install flipt-clientThe flipt-client-python library is a wrapper around the flipt-engine-ffi library.
All evaluation happens within the SDK, using the shared library built from the flipt-engine-ffi library.
Because the evaluation happens within the SDK, the SDKs can be used in environments where the Flipt server is not available or reachable after the initial data is fetched.
Upon instantiation, the flipt-client-python library will fetch the flag state from the Flipt server and store it in memory. This means that the first time you use the SDK, it will make a request to the Flipt server.
By default, the SDK will poll the Flipt server for new flag state at a regular interval. This interval can be configured using the update_interval option when constructing a client. The default interval is 120 seconds.
Flipt Cloud and Flipt v2 users can use the streaming fetch method to stream flag state changes from the Flipt server to the SDK.
When in streaming mode, the SDK will connect to the Flipt server and open a persistent connection that will remain open until the client is closed. The SDK will then receive flag state changes in real-time.
The SDK will automatically retry fetching (or initiating streaming) flag state if the client is unable to reach the Flipt server temporarily.
The SDK will retry up to 3 times with an exponential backoff interval between retries. The base delay is 1 second and the maximum delay is 30 seconds.
Retriable errors include:
429 Too Many Requests502 Bad Gateway503 Service Unavailable504 Gateway Timeout- Other potential transient network or DNS errors
This SDK currently supports the following OSes/architectures:
- Linux x86_64
- Linux arm64
- MacOS x86_64
- MacOS arm64
- Windows x86_64
This section is for users who are migrating from a previous (pre-1.0.0) version of the SDK.
FliptEvaluationClienthas been renamed toFliptClient.- The
namespaceargument is now part ofClientOptionsasnamespace(and optionallyenvironment). update_intervalandrequest_timeoutare nowtimedeltaobjects instead ofint.- All response models are now Pydantic models with explicit types and improved docstrings.
- Backwards compatibility with Pydantic v1 is now maintained.
- All errors now inherit from
FliptErrorand use specific subclasses likeValidationErrorandEvaluationError. - The client and models now follow Python naming conventions (snake_case for methods and fields).
- The minimum supported Python version is now 3.8.
from flipt_client import FliptClient
from flipt_client.models import ClientOptions, ClientTokenAuthentication
client = FliptClient(
opts=ClientOptions(
url="http://localhost:8080",
authentication=ClientTokenAuthentication(client_token="your-token"),
)
)
variant_result = client.evaluate_variant(flag_key="flag1", entity_id="entity", context={"fizz": "buzz"})
print(variant_result)
# Important: always close the client to release resources
client.close()The FliptClient constructor accepts a single opts argument:
opts: An instance of theClientOptionsclass. The structure is:environment: The environment (Flipt v2) to fetch flag state from. If not provided, the client will default to thedefaultenvironment.namespace: The namespace to fetch flag state from. If not provided, the client will default to thedefaultnamespace.url: The URL of the upstream Flipt instance. Defaults tohttp://localhost:8080.request_timeout: Timeout for requests. Defaults to no timeout.update_interval: Interval to fetch new flag state. Defaults to 120s.authentication: Authentication strategy. See Authentication.reference: The namespace or reference key. Defaults todefault.fetch_mode: Fetch mode (pollingorstreaming). Defaults to polling.error_strategy: Error strategy (failorfallback). Defaults to fail.snapshot: A base64 encoded snapshot of the engine state. Defaults toNone. See Snapshotting.tls_config: TLS configuration for connecting to Flipt servers with custom certificates. See TLS Configuration.
The FliptClient supports:
- No Authentication (default)
- Client Token Authentication
- JWT Authentication
The FliptClient supports TLS configuration for connecting to Flipt servers with custom certificates, self-signed certificates, or mutual TLS authentication.
The tls_config option accepts a TlsConfig object with the following options:
ca_cert_file: Path to custom CA certificate file (PEM format). Use this to trust self-signed certificates or custom certificate authorities.ca_cert_data: Raw CA certificate content (PEM format). Alternative toca_cert_filefor providing certificate data directly.insecure_skip_verify: Skip certificate verification (development only). Set toTrueto disable TLS certificate and hostname verification. Warning: Only use this for development/testing.insecure_skip_hostname_verify: Skip hostname verification while maintaining certificate validation (development only). Set toTrueto disable hostname verification but still validate the certificate chain. Warning: Only use this for development/testing.client_cert_file: Client certificate file for mutual TLS (PEM format). Required for mTLS authentication.client_key_file: Client key file for mutual TLS (PEM format). Required for mTLS authentication.client_cert_data: Raw client certificate content (PEM format). Alternative toclient_cert_file.client_key_data: Raw client key content (PEM format). Alternative toclient_key_file.
Note: Data fields (*_data) take precedence over file paths (*_file) when both are provided.
Self-signed certificate with custom CA:
from flipt_client import FliptClient
from flipt_client.models import ClientOptions, TlsConfig
# Using CA certificate file
tls_config = TlsConfig(
ca_cert_file="/path/to/ca.crt"
)
client = FliptClient(
opts=ClientOptions(
url="https://flipt.example.com:8443",
tls_config=tls_config
)
)Skip certificate verification (development only):
tls_config = TlsConfig(
insecure_skip_verify=True
)
client = FliptClient(
opts=ClientOptions(
url="https://localhost:8443",
tls_config=tls_config
)
)Self-signed certificate with hostname mismatch:
# For self-signed certificates with hostname mismatch
tls_config = TlsConfig(
ca_cert_file="/path/to/ca.crt",
insecure_skip_hostname_verify=True
)
client = FliptClient(
opts=ClientOptions(
url="https://localhost:8443",
tls_config=tls_config
)
)Mutual TLS authentication:
tls_config = TlsConfig(
ca_cert_file="/path/to/ca.crt",
client_cert_file="/path/to/client.crt",
client_key_file="/path/to/client.key"
)
client = FliptClient(
opts=ClientOptions(
url="https://flipt.example.com:8443",
tls_config=tls_config
)
)Using certificate data instead of files:
# Read certificate contents
with open("/path/to/ca.crt", "r") as f:
ca_cert_data = f.read()
tls_config = TlsConfig(
ca_cert_data=ca_cert_data
)
client = FliptClient(
opts=ClientOptions(
url="https://flipt.example.com:8443",
tls_config=tls_config
)
)All errors raised by the client derive from flipt_client.errors.FliptError:
ValidationError: Raised for invalid arguments or configuration.EvaluationError: Raised for evaluation failures (e.g., flag not found).
Example:
from flipt_client.errors import ValidationError, EvaluationError
try:
client.evaluate_variant(flag_key=None, entity_id="entity")
except ValidationError as e:
print(f"Validation error: {e}")
except EvaluationError as e:
print(f"Evaluation error: {e}")The client supports snapshotting of flag state as well as seeding the client with a snapshot for evaluation. This is helpful if you want to use the client in an environment where the Flipt server is not guaranteed to be available or reachable on startup.
To get the snapshot for the client, you can use the get_snapshot method. This returns a base64 encoded JSON string that represents the flag state for the client.
You can set the snapshot for the client using the snapshot option when constructing a client.
Note: You most likely will want to also set the error_strategy to fallback when using snapshots. This will ensure that you wont get an error if the Flipt server is not available or reachable even on the initial fetch.
You also may want to store the snapshot in a local file so that you can use it to seed the client on startup.
Important
If the Flipt server becomes reachable after the setting the snapshot, the client will replace the snapshot with the new flag state from the Flipt server.
Contributions are welcome! Please feel free to open an issue or submit a Pull Request.
This project is licensed under the MIT License.