On this page

Experiment Python SDK

Official documentation for Amplitude Experiment's server-side Python SDK implementation.

This documentation has separate sections for remote and local evaluation.

Remote evaluation

Implements fetching variants for a user using remote evaluation.

Install

Python version compatibility

The Python Server SDK works with Python 3.6+.

Install the Python Server SDK with pip.

bash
pip install amplitude-experiment

Quick start

  1. Initialize the experiment client
  2. Fetch variants for the user
  3. Access a flag's variant
python
from amplitude_experiment import Experiment, RemoteEvaluationConfig, RemoteEvaluationClient, User

# (1) Initialize the experiment client
experiment = Experiment.initialize_remote('<DEPLOYMENT_KEY>')

# (2) Fetch variants for a user
user = User(
    device_id="abcdefg",
    user_id="user@company.com",
    user_properties={
        'premium': True
    }
)
variants = experiment.fetch_v2(user)

# (3) Access a flag's variant
variant = variants['YOUR-FLAG-KEY']
if variant:
    if variant.value == 'on':
        # Flag is on
    else:
        # Flag is off

Initialize

Initialize the SDK client in your server on startup. The deployment key argument you pass to the api_key parameter must live within the same project that you are sending analytics events to.

python
Experiment.initialize_remote(api_key, config = None) : RemoteEvaluationClient

Timeout and retry configuration

Configure the timeout and retry options to best fit your performance requirements.

python
experiment = Experiment.initialize_remote('<DEPLOYMENT_KEY>', Config())

Configuration

You can configure the SDK client on initialization.

EU data center

If you're using Amplitude's EU data center, configure the server_zone option on initialization.

Fetch

Fetches variants for a user and returns the results. The function remote evaluates the user for flags associated with the deployment used to initialize the SDK client.

python
fetch_v2(user: User, fetch_options: FetchOptions = None) : Variants

FetchOptions

python
user = User(
    device_id="abcdefg",
    user_id="user@company.com",
    user_properties={
        'premium': True
    }
)
variants = experiment.fetch_v2(user)

After fetching variants for a user, you may to access the variant for a specific flag.

python
variant = variants['YOUR-FLAG-KEY']
if variant:
    if variant.value == 'on':
        # Flag is on
    else:
        # Flag is off

Fetch async

The fetch method is synchronous. To fetch asynchronously, you can use fetch_async method

python
fetch_async_v2(user: User, callback)
python
def fetch_callback(user, variants):
  variant = variants['YOUR-FLAG-KEY']
  if variant:
    if variant.value == 'on':
      # Flag is on
    else:
      # Flag is off

experiment.fetch_async_v2(user, fetch_callback)

Local evaluation

Implements evaluating variants for a user using local evaluation. If you plan to use local evaluation, you should understand the tradeoffs.

Install

Install the Python Server SDK's local evaluation.

Operating system and architecture support

The local evaluation package supports the following OS' and architectures (OS/ARCH):

Supported

  • darwin/amd64
  • darwin/arm64
  • linux/amd64
  • linux/arm64

The local evaluation package doesn't support Alpine Linux.

If you need another OS/Arch supported, submit an issue on github or email experiment@amplitude.com.

Install the Python Server SDK with pip.

bash
pip install amplitude-experiment

Quick start

  1. Initialize the local evaluation client.
  2. Start the local evaluation client.
  3. Evaluate a user.
python
# (1) Initialize the local evaluation client with a server deployment key.
experiment = Experiment.initialize_local("DEPLOYMENT_KEY", LocalEvaluationConfig(
  # (Recommended) Enable local evaluation cohort targeting.
  cohort_sync_config=CohortSyncConfig(api_key="API_KEY", secret_key="SECRET_KEY")
))

# (2) Start the local evaluation client.
experiment.start()

# (3) Evaluate a user.
user = User(
    device_id="abcdefg",
    user_id="user@company.com",
    user_properties={
        'premium': True
    }
)
variants = experiment.evaluate_v2(user)

Initialize

Initializes a local evaluation client.

Server deployment key

You must initialize the local evaluation client with a server deployment key in to get access to local evaluation flag configs.

python
Experiment.initialize_local(api_key, config = None) : LocalEvaluationClient

Flag polling interval

Use the flag_config_polling_interval_millis configuration to determine the time flag configs take to update after modification (default 30s).

Configuration

You can configure the SDK client on initialization.

EU data center

If you're using Amplitude's EU data center, configure the server_zone option on initialization.

LocalEvaluationConfig

AssignmentConfig

ExposureConfig

CohortSyncConfig

Start

Start the local evaluation client, pre-fetching local evaluation mode flag configs for evaluation and starting the flag config poller at the configured interval.

python
start()

You should await the result of start() to ensure that flag configs are ready to be used before calling evaluate_v2()

python
experiment.start()

Evaluate

Executes the evaluation logic using the flags pre-fetched on start(). You must give evaluate a user object argument. You can optionally pass an array of flag keys if you require only a specific subset of flag variants.

Automatic assignment tracking

Set assignment_config to automatically track an assignment event to Amplitude when evaluate_v2() is called.

Exposure tracking

Set exposure_config to enable exposure tracking. Then, set tracks_exposure to True in EvaluateOptions when calling evaluate_v2().

python
evaluate_v2(self, user: User, flag_keys: List[str], options: EvaluateOptions) : Dict[str, Variant]
python
# The user to evaluate
user = User(user_id='test_user')

# Evaluate all flag variants
all_variants = experiment.evaluate_v2(user)

# Evaluate a specific subset of flag variants
specific_variants = experiment.evaluate_v2(user, ["<FLAG_KEY_1>", "<FLAG_KEY_2>"])

# Access a variant
variant = all_variants["<FLAG_KEY>"]
if variant.value == 'on':
    # Flag is on
else:
    # Flag is off

EvaluateOptions

Local evaluation cohort targeting

Since version 1.4.0, the local evaluation SDK client supports downloading cohorts for local evaluation targeting. You must configure the cohort_sync_config option with the analytics api_key and secret_key on initialization to enable this support.

python
experiment = Experiment.initialize_local("DEPLOYMENT_KEY", LocalEvaluationConfig(
  # (Recommended) Enable local evaluation cohort targeting.
  cohort_sync_config=CohortSyncConfig(api_key="API_KEY", secret_key="SECRET_KEY")
))

Custom logging

Pass a custom logging.Logger instance to control logging behavior.

Custom logger

Pass a custom logging.Logger instance to RemoteEvaluationConfig or LocalEvaluationConfig:

python
import logging
from amplitude_experiment import Experiment, RemoteEvaluationConfig, LocalEvaluationConfig

# Create a custom logger
custom_logger = logging.getLogger('MyAppLogger')
custom_logger.setLevel(logging.WARN)
handler = logging.FileHandler('experiment.log')
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
custom_logger.addHandler(handler)

# Remote evaluation with custom logger
remote_config = RemoteEvaluationConfig(
    logger=custom_logger
)
experiment = Experiment.initialize_remote('DEPLOYMENT_KEY', remote_config)

# Local evaluation with custom logger
local_config = LocalEvaluationConfig(
    logger=custom_logger
)
experiment = Experiment.initialize_local('DEPLOYMENT_KEY', local_config)

Debug flag with default logger

Without a custom logger, the debug flag controls the default logger's level:

python
# Without custom logger, debug=False uses WARNING level
config = RemoteEvaluationConfig(
    debug=False
)
# Default logger level is WARNING

# Without custom logger, debug=True uses DEBUG level
config = RemoteEvaluationConfig(
    debug=True
)
# Default logger level is DEBUG

With a custom logger, the SDK ignores the debug flag and your logger maintains its configured level:

python
import logging
from amplitude_experiment import Experiment, RemoteEvaluationConfig

custom_logger = logging.getLogger('MyAppLogger')
custom_logger.setLevel(logging.WARN)

# Custom logger maintains its WARN level regardless of debug flag
config = RemoteEvaluationConfig(
    logger=custom_logger,
    debug=True
)
# Logger level stays WARN (debug flag is ignored)

Access Amplitude cookies

If you're using the Amplitude Analytics SDK on the client-side, the Python server SDK provides an AmplitudeCookie class with convenience functions for parsing and interacting with the Amplitude identity cookie. This is useful for ensuring that the Device ID on the server matches the Device ID set on the client, especially if the client hasn't yet generated a Device ID.

python
import uuid
from amplitude_experiment import AmplitudeCookie

# Get the cookie name for the Amplitude API key
# For Browser SDK 2.0 cookies, use new_format=True:
# amp_cookie_name = AmplitudeCookie.cookie_name('amplitude-api-key', new_format=True)
amp_cookie_name = AmplitudeCookie.cookie_name('amplitude-api-key')
device_id = None

# Try to get device ID from existing cookie
if request.cookies.get(amp_cookie_name):
  device_id = AmplitudeCookie.parse(request.cookies.get(amp_cookie_name)).device_id
  # For Browser SDK 2.0: AmplitudeCookie.parse(request.cookies.get(amp_cookie_name), new_format=True).device_id

# If no device ID found, generate a new one and set the cookie
if device_id is None:
  device_id = str(uuid.uuid4())
  amp_cookie_value = AmplitudeCookie.generate(device_id)
  # For Browser SDK 2.0: AmplitudeCookie.generate(device_id, new_format=True)
  response.set_cookie(amp_cookie_name, amp_cookie_value,
    domain='.your-domain.com',  # this should be the same domain used by the Amplitude JS SDK
    httponly=False,
    secure=False
  )

Was this helpful?