Skip to content

Python provider

Installation

pip install openfeature-provider-flagd

Configuration and Usage

The flagd provider can operate in two modes: RPC (evaluation takes place in flagd, via gRPC calls) or in-process (evaluation takes place in-process, with the provider getting a ruleset from a compliant sync-source).

Remote resolver (RPC)

This is the default mode of operation of the provider. In this mode, FlagdProvider communicates with flagd via the gRPC protocol. Flag evaluations take place remotely at the connected flagd instance.

Instantiate a new FlagdProvider instance and configure the OpenFeature SDK to use it:

from openfeature import api
from openfeature.contrib.provider.flagd import FlagdProvider

api.set_provider(FlagdProvider())

In-process resolver

This mode performs flag evaluations locally (in-process). Flag configurations for evaluation are obtained via gRPC protocol using sync protobuf schema service definition.

Consider the following example to create a FlagdProvider with in-process evaluations,

from openfeature import api
from openfeature.contrib.provider.flagd import FlagdProvider
from openfeature.contrib.provider.flagd.config import ResolverType

api.set_provider(
    FlagdProvider(
        resolver_type=ResolverType.IN_PROCESS,
    )
)

In the above example, in-process handlers attempt to connect to a sync service on address localhost:8013 to obtain flag definitions.

File mode

In-process resolvers can also work in an offline mode. To enable this mode, you should provide a valid flag configuration file with the option offlineFlagSourcePath.

from openfeature import api
from openfeature.contrib.provider.flagd import FlagdProvider
from openfeature.contrib.provider.flagd.config import ResolverType

api.set_provider(
    FlagdProvider(
        resolver_type=ResolverType.FILE,
        offline_flag_source_path="my-flag.json",
    )
)

Provider will attempt to detect file changes using polling. Polling happens at 5 second intervals and this is currently unconfigurable. This mode is useful for local development, tests and offline applications.

Configuration options

The default options can be defined in the FlagdProvider constructor.

Option name Environment variable name Type & Values Default Compatible resolver
resolver_type FLAGD_RESOLVER enum - rpc, in-process rpc
host FLAGD_HOST str localhost rpc & in-process
port FLAGD_SYNC_PORT (in-process), FLAGD_PORT (rpc or fallback) int 8013 (rpc), 8015 (in-process) rpc & in-process
tls FLAGD_TLS bool false rpc & in-process
cert_path FLAGD_SERVER_CERT_PATH String null rpc & in-process
deadline FLAGD_DEADLINE_MS int 500 rpc & in-process
stream_deadline_ms FLAGD_STREAM_DEADLINE_MS int 600000 rpc & in-process
keep_alive_time FLAGD_KEEP_ALIVE_TIME_MS int 0 rpc & in-process
selector FLAGD_SOURCE_SELECTOR str null in-process
cache_type FLAGD_CACHE enum - lru, disabled lru rpc
max_cache_size FLAGD_MAX_CACHE_SIZE int 1000 rpc
retry_backoff_ms FLAGD_RETRY_BACKOFF_MS int 1000 rpc
offline_flag_source_path FLAGD_OFFLINE_FLAG_SOURCE_PATH str null in-process
channel_credentials - grpc.ChannelCredentials null rpc & in-process

[!NOTE] The selector configuration is only used in in-process mode for filtering flag configurations. See Selector Handling for migration guidance.

[!NOTE] Some configurations are only applicable for RPC resolver.

Custom channel credentials

Pass custom grpc.ChannelCredentials to channel_credentials to control the TLS setup. The provider uses these credentials in both resolver modes. They take precedence over tls and cert_path, and the provider creates a secure channel even when tls is False.

To trust a private server certificate authority (CA), supply only the root certificates:

import grpc