Amplitude Experiment Python SDK
The `amplitude-experiment` library is the official Amplitude Experiment Python SDK for server-side instrumentation, enabling feature flagging and A/B testing. It supports both remote and local evaluation of feature flags. The current version is 1.10.1 and the library maintains an active development cycle with frequent updates and bug fixes.
Common errors
-
NameError: name 'Experiment' is not defined
cause The `Experiment` class (or other Amplitude Experiment symbols) was not correctly imported.fixEnsure you have the correct import statement: `from amplitude_experiment import Experiment, User, RemoteEvaluationConfig, RemoteEvaluationClient`. -
KeyError: 'YOUR-FLAG-KEY' or AttributeError: 'NoneType' object has no attribute 'value'
cause Attempting to access a feature flag variant that does not exist in the fetched variants, or the fetch operation failed to return a variant for the specified key. This could be due to an incorrect flag key, the flag not being enabled for the deployment, or the user not qualifying for any variants.fixAlways check if the variant exists before accessing its properties using `variants.get('YOUR-FLAG-KEY')` or by checking `if variant:` before proceeding. Verify the flag key in your Amplitude Experiment dashboard and ensure the flag is enabled for the correct deployment and user. -
Failed to connect to host 'api.amplitude.com'
cause Network issues preventing the SDK from reaching Amplitude's servers, or an incorrect `server_url` or `server_zone` configuration.fixVerify your network connectivity. If using an EU data center, ensure `server_zone='EU'` is correctly configured. For custom endpoints, check the `server_url` in your `RemoteEvaluationConfig`.
Warnings
- breaking Major version upgrades (e.g., v1.x.x to v2.x.x) typically introduce breaking changes in public interfaces, behaviors, or semantics. Always consult the official upgrade guidelines before updating to a new major version.
- gotcha Inconsistent bucketing or 'variant jumping' can occur if the `device_id` or `user_id` used for fetching variants differs from the IDs used to track subsequent exposure events in Amplitude Analytics. This often points to an identity mismatch in implementation.
- gotcha If you are using Amplitude's EU data center, you must explicitly configure the `server_zone` option during client initialization to `'EU'`, otherwise, data will be routed to the US data center by default.
- gotcha Do not assign a variant a name or value of 'off'. This value is explicitly reserved by Amplitude Experiment for users in the 'OFF/FALLBACK' bucket, which is used when Experiment cannot assign a user to a treatment group.
Install
-
pip install amplitude-experiment
Imports
- Experiment
from amplitude_experiment.experiment import Experiment
from amplitude_experiment import Experiment
- RemoteEvaluationClient
from amplitude_experiment import RemoteEvaluationClient
- LocalEvaluationClient
from amplitude_experiment import LocalEvaluationClient
- User
from amplitude_experiment import User
- RemoteEvaluationConfig
from amplitude_experiment import RemoteEvaluationConfig
- LocalEvaluationConfig
from amplitude_experiment import LocalEvaluationConfig
Quickstart
import os
from amplitude_experiment import Experiment, User
# Get your deployment's API key from environment variables
API_KEY = os.environ.get('AMPLITUDE_EXPERIMENT_API_KEY', 'YOUR_DEPLOYMENT_KEY')
# Initialize the experiment remote evaluation client
# It's recommended to initialize the client once on application startup.
experiment = Experiment.initialize_remote(API_KEY)
# Define a user for whom variants should be fetched
user = User(
device_id="abcdefg",
user_id="user@company.com",
user_properties={'premium': True, 'country': 'USA'}
)
async def get_variants_and_evaluate():
try:
# Fetch variants for the user asynchronously
variants = await experiment.fetch_v2(user)
# Access a flag's variant
feature_flag_key = 'your-feature-flag-key'
variant = variants.get(feature_flag_key)
if variant:
print(f"Variant for '{feature_flag_key}': {variant.value}")
if variant.value == 'on':
# Implement 'on' behavior
print("Feature is ON for this user.")
elif variant.value == 'control':
# Implement 'control' behavior
print("User is in control group.")
else:
# Implement other variant behavior
print(f"User is in variant: {variant.value}")
else:
print(f"Flag '{feature_flag_key}' not found or variant is None.")
except Exception as e:
print(f"An error occurred: {e}")
# In a real application, you would call get_variants_and_evaluate()
# within an async context, e.g., using asyncio.run()
# import asyncio
# asyncio.run(get_variants_and_evaluate())