Official documentation for Amplitude Experiment's server-side Python SDK implementation.
This documentation has separate sections for remote and local evaluation.
Implements fetching variants for a user via remote evaluation.
The Python Server SDK works with Python 3.6+.
Install the Python Server SDK with pip.
1pip install amplitude-experiment
1from amplitude_experiment import Experiment, RemoteEvaluationConfig, RemoteEvaluationClient, User 2 3# (1) Initialize the experiment client 4experiment = Experiment.initialize_remote('<DEPLOYMENT_KEY>') 5 6# (2) Fetch variants for a user 7user = User( 8 device_id="abcdefg", 9 user_id="user@company.com",10 user_properties={11 'premium': True12 }13)14variants = experiment.fetch_v2(user)15 16# (3) Access a flag's variant17variant = variants['YOUR-FLAG-KEY']18if variant:19 if variant.value == 'on':20 # Flag is on21 else:22 # Flag is off
The SDK client should be initialized in your server on startup. The deployment key argument passed into the api_key
parameter must live within the same project that you are sending analytics events to.
1Experiment.initialize_remote(api_key, config = None) : RemoteEvaluationClient
Parameter | Requirement | Description |
---|---|---|
api_key |
required | The deployment key which authorizes fetch requests and determines which flags should be evaluated for the user. |
config |
optional | The client configuration used to customize SDK client behavior. |
Configure the timeout and retry options to best fit your performance requirements.
1experiment = Experiment.initialize_remote('<DEPLOYMENT_KEY>', Config())
You can configure the SDK client on initialization.
If you're using Amplitude's EU data center, configure the server_zone
option on initialization.
Name |
Description | Default Value |
---|---|---|
debug |
Enable additional debug logging. | false |
server_zone |
The Amplitude data center to use. Either ServerZone.US or ServerZone.EU |
ServerZone.US |
server_url |
The host to fetch variants from. | https://api.lab.amplitude.com |
fetch_timeout_millis |
The timeout for fetching variants in milliseconds. This timeout only applies to the initial request, not subsequent retries | 10000 |
fetch_retries |
The number of retries to attempt if a request to fetch variants fails. | 0 |
fetch_retry_backoff_min_millis |
The minimum (initial) backoff after a request to fetch variants fails. This delay is scaled by the fetchRetryBackoffScalar |
500 |
fetch_retry_backoff_max_millis |
The maximum backoff between retries. If the scaled backoff becomes greater than the max, the max is used for all subsequent requests | 10000 |
fetch_retry_backoff_scalar |
Scales the minimum backoff exponentially. | 1.5 |
fetch_retry_timeout_millis |
The request timeout for retrying variant fetches. | 10000 |
Fetches variants for a user and returns the results. This function remote evaluates the user for flags associated with the deployment used to initialize the SDK client.
1fetch_v2(user: User) : Variants
Parameter | Requirement | Description |
---|---|---|
user |
required | The user to remote fetch variants for. |
1user = User(2 device_id="abcdefg",3 user_id="user@company.com",4 user_properties={5 'premium': True6 }7)8variants = experiment.fetch_v2(user)
After fetching variants for a user, you may to access the variant for a specific flag.
1variant = variants['YOUR-FLAG-KEY']2if variant:3 if variant.value == 'on':4 # Flag is on5 else:6 # Flag is off
The fetch method is synchronous. To fetch asynchronously, you can use fetch_async
method
1fetch_async_v2(user: User, callback)
Parameter | Requirement | Description |
---|---|---|
user |
required | The user to remote fetch variants for. |
callback |
optional | The callback to handle the variants. |
1def fetch_callback(user, variants):2 variant = variants['YOUR-FLAG-KEY']3 if variant:4 if variant.value == 'on':5 # Flag is on6 else:7 # Flag is off8 9experiment.fetch_async_v2(user, fetch_callback)
Implements evaluating variants for a user via local evaluation. If you plan on using local evaluation, you should understand the tradeoffs.
Install the Python Server SDK's local evaluation.
The local evaluation package currently only supports the following OS' and architectures (OS/ARCH
):
Supported
Alpine linux is not supported.
If you need another OS/Arch supported, submit an issue on github or email experiment@amplitude.com.
Install the Python Server SDK with pip.
1pip install amplitude-experiment
1# (1) Initialize the local evaluation client with a server deployment key. 2experiment = Experiment.initialize_local("DEPLOYMENT_KEY", LocalEvaluationConfig( 3 # (Recommended) Enable local evaluation cohort targeting. 4 cohort_sync_config=CohortSyncConfig(api_key="API_KEY", secret_key="SECRET_KEY") 5)) 6 7# (2) Start the local evaluation client. 8experiment.start() 9 10# (3) Evaluate a user.11user = User(12 device_id="abcdefg",13 user_id="user@company.com",14 user_properties={15 'premium': True16 }17)18variants = experiment.evaluate_v2(user)
Initializes a local evaluation client.
You must initialize the local evaluation client with a server deployment key in to get access to local evaluation flag configs.
1Experiment.initialize_local(api_key, config = None) : LocalEvaluationClient
Parameter | Requirement | Description |
---|---|---|
apiKey |
required | The server deployment key which authorizes fetch requests and determines which flags should be evaluated for the user. |
config |
optional | The client configuration used to customize SDK client behavior. |
Use the flag_config_polling_interval_millis
configuration to determine the time flag configs take to update once modified (default 30s).
You can configure the SDK client on initialization.
If you're using Amplitude's EU data center, configure the server_zone
option on initialization.
LocalEvaluationConfig
Name |
Description | Default Value |
---|---|---|
debug |
Set to true to enable debug logging. |
false |
server_zone |
The Amplitude data center to use. Either ServerZone.US or ServerZone.EU |
ServerZone.US |
server_url |
The host to fetch flag configurations from. | https://api.lab.amplitude.com |
flag_config_polling_interval_millis |
The interval to poll for updated flag configs after calling start() |
30000 |
flag_config_poller_request_timeout_millis |
The timeout for the request made by the flag config poller | 10000 |
assignment_config |
Configuration for automatically tracking assignment events after an evaluation. | None |
cohort_sync_config |
Configuration to enable cohort downloading for local evaluation cohort targeting. | None |
AssignmentConfig
Name |
Description | Default Value |
---|---|---|
api_key |
The analytics API key and NOT the experiment deployment key | required |
cache_capacity |
The maximum number of assignments stored in the assignment cache | 65536 |
Analytics SDK Options | Options to configure the underlying Amplitude Analytics SDK used to track assignment events |
CohortSyncConfig
Name |
Description | Default Value |
---|---|---|
api_key |
The analytics API key and NOT the experiment deployment key | required |
secret_key |
The analytics secret key | required |
max_cohort_size |
The maximum size of cohort that the SDK will download. Cohorts larger than this size will not be downloaded. | 2147483647 |
cohort_polling_interval_millis |
The interval, in milliseconds, to poll Amplitude for cohort updates (60000 minimum). | 60000 |
cohort_server_url |
The cohort server endpoint from which to fetch cohort data. For hitting the EU data center, set server_zone to ServerZone.EU . Setting this value will override server_zone defaults. |
https://cohort-v2.lab.amplitude.com |
Start the local evaluation client, pre-fetching local evaluation mode flag configs for evaluation and starting the flag config poller at the configured interval.
1start()
You should await the result of start()
to ensure that flag configs are ready to be used before calling evaluate_v2()
1experiment.start()
Executes the evaluation logic using the flags pre-fetched on start()
. Evaluate must be given a user object argument and can optionally be passed an array of flag keys if only a specific subset of required flag variants are required.
Set assignment_config
to automatically track an assignment event to Amplitude when evaluate_v2()
is called.
1evaluate_v2(self, user: User, flag_keys: List[str]) : Dict[str, Variant]
Parameter | Requirement | Description |
---|---|---|
user |
required | The user to evaluate. |
flag_keys |
optional | Specific flags or experiments to evaluate. If nil, or empty, all flags and experiments are evaluated. |
1# The user to evaluate 2user = User(user_id='test_user') 3 4# Evaluate all flag variants 5all_variants = experiment.evaluate_v2(user) 6 7# Evaluate a specific subset of flag variants 8specific_variants = experiment.evaluate_v2(user, ["<FLAG_KEY_1>", "<FLAG_KEY_2>"]) 9 10# Access a variant11variant = all_variants["<FLAG_KEY>"]12if variant.value == 'on':13 # Flag is on14else:15 # Flag is off
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.
1experiment = Experiment.initialize_local("DEPLOYMENT_KEY", LocalEvaluationConfig(2 # (Recommended) Enable local evaluation cohort targeting.3 cohort_sync_config=CohortSyncConfig(api_key="API_KEY", secret_key="SECRET_KEY")4))
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.
1import uuid 2from amplitude_experiment import AmplitudeCookie 3 4# grab amp device id if present 5amp_cookie_name = AmplitudeCookie.cookie_name('amplitude-api-key') 6device_id = nil 7if request.cookies.get(amp_cookie_name): 8 device_id = AmplitudeCookie.parse(request.cookies.get(amp_cookie_name)).device_id 9 10if device_id is None:11 # deviceId doesn't exist, set the Amplitude Cookie12 device_id = str(uuid.uuid4())13 amp_cookie_value = AmplitudeCookie.generate(device_id)14 resp.set_cookie(amp_cookie_name, {15 "value": amp_cookie_value,16 "domain": ".your-domain.com", # this should be the same domain used by the Amplitude JS SDK17 "httponly": False,18 "secure": False19 })
Thanks for your feedback!
June 4th, 2024
Need help? Contact Support
Visit Amplitude.com
Have a look at the Amplitude Blog
Learn more at Amplitude Academy
© 2024 Amplitude, Inc. All rights reserved. Amplitude is a registered trademark of Amplitude, Inc.