ABsmartly Python SDK
A/B Smartly - Python SDK
Compatibility
The ABsmartly Python SDK is compatible with Python 3. It provides both a blocking and an asynchronous interface.
Installation
Install the SDK using pip:
pip install absmartly
Dependencies
setuptools~=60.2.0
requests~=2.28.1
urllib3~=1.26.12
jsons~=1.6.3
Getting Started
Please follow the installation instructions before trying the following code.
Initialization
This example assumes an API Key, an Application, and an Environment have been created in the ABsmartly web console.
Recommended: Named Parameters
from sdk import ABsmartly, ContextConfig
def main():
sdk = ABsmartly.create(
endpoint="https://your-company.absmartly.io/v1",
api_key="YOUR-API-KEY",
application="website",
environment="production"
)
context_config = ContextConfig()
context_config.units = {"session_id": "5ebf06d8cb5d8137290c4abb64155584fbdb64d8"}
ctx = sdk.create_context(context_config)
ctx.wait_until_ready()
With Optional Parameters
from sdk import ABsmartly, ContextConfig
sdk = ABsmartly.create(
endpoint="https://your-company.absmartly.io/v1",
api_key="YOUR-API-KEY",
application="website",
environment="production",
timeout=5, # Connection timeout in seconds (default: 3)
retries=3 # Max retries on failure (default: 5)
)
Alternative: Manual Configuration
For use cases where you need to manually configure all components:
from sdk import (
ABsmartly,
ABsmartlyConfig,
Client,
ClientConfig,
ContextConfig,
DefaultHTTPClient,
DefaultHTTPClientConfig,
)
def main():
client_config = ClientConfig()
client_config.endpoint = "https://your-company.absmartly.io/v1"
client_config.api_key = "YOUR-API-KEY"
client_config.application = "website"
client_config.environment = "production"
default_client_config = DefaultHTTPClientConfig()
default_client_config.max_retries = 5
default_client_config.connection_timeout = 3
default_client = DefaultHTTPClient(default_client_config)
sdk_config = ABsmartlyConfig()
sdk_config.client = Client(client_config, default_client)
sdk = ABsmartly(sdk_config)
context_config = ContextConfig()
context_config.units = {"session_id": "5ebf06d8cb5d8137290c4abb64155584fbdb64d8"}
ctx = sdk.create_context(context_config)
ctx.wait_until_ready()
SDK Options
| Config | Type | Required? | Default | Description |
|---|---|---|---|---|
| endpoint | str |
✅ | None |
The URL to your API endpoint. Most commonly "https://your-company.absmartly.io/v1" |
| api_key | str |
✅ | None |
Your API key which can be found on the Web Console. |
| environment | str |
✅ | None |
The environment of the platform where the SDK is installed. Environments are created on the Web Console and should match the available environments in your infrastructure. |
| application | str |
✅ | None |
The name of the application where the SDK is installed. Applications are created on the Web Console and should match the applications where your experiments will be running. |
| timeout | int |
❌ | 3 |
Connection timeout in seconds before the SDK will stop trying to connect. |
| retries | int |
❌ | 5 |
The number of retries before the SDK stops trying to connect. |
| event_logger | ContextEventLogger |
❌ | None |
A callback handler which runs after SDK events. |
| context_data_provider | ContextDataProvider |
❌ | auto | Custom provider for context data (advanced usage, manual configuration only) |
| context_event_handler | ContextEventHandler |
❌ | auto | Custom handler for publishing events (advanced usage, manual configuration only) |
Creating a New Context
Synchronously
context_config = ContextConfig()
context_config.units = {"session_id": "5ebf06d8cb5d8137290c4abb64155584fbdb64d8"}
context_config.publish_delay = 10
context_config.refresh_interval = 5
ctx = sdk.create_context(context_config)
ctx.wait_until_ready()
if ctx:
print("Context ready")
Asynchronously
context_config = ContextConfig()
context_config.units = {"session_id": "5ebf06d8cb5d8137290c4abb64155584fbdb64d8"}
context_config.publish_delay = 10
context_config.refresh_interval = 5
ctx = sdk.create_context(context_config)
ctx.wait_until_ready_async()
With Pre-fetched Data
When doing full-stack experimentation with ABsmartly, we recommend creating a context only once on the server-side. Creating a context involves a round-trip to the ABsmartly event collector. We can avoid repeating the round-trip on the client-side by reusing the server-side context data.
context_config = ContextConfig()
context_config.units = {
"session_id": "5ebf06d8cb5d8137290c4abb64155584fbdb64d8",
"user_id": "12345"
}
ctx = sdk.create_context(context_config)
ctx.wait_until_ready()
context_data = ctx.get_data()
another_config = ContextConfig()
another_config.units = {"session_id": "another-user-session-id"}
another_ctx = sdk.create_context_with(another_config, context_data)
Refreshing the Context with Fresh Experiment Data
For long-running contexts, the context is usually created once when the application is first started.
However, any experiments being tracked in your production code, but started after the context was created, will not be triggered.
To mitigate this, we can use the refresh_interval parameter on the context config.
context_config = ContextConfig()
context_config.units = {"session_id": "5ebf06d8cb5d8137290c4abb64155584fbdb64d8"}
context_config.refresh_interval = 5 # Refresh every 5 seconds
ctx = sdk.create_context(context_config)
Alternatively, the refresh() method can be called manually.
The refresh() method pulls updated experiment data from the ABsmartly collector and will trigger recently started experiments when get_treatment() is called again.
context.refresh()
Setting Extra Units
You can add additional units to a context by calling the set_unit() or the set_units() method.
This method may be used for example, when a user logs in to your application, and you want to use the new unit type to the context.
Please note that you cannot override an already set unit type as that would be a change of identity, and will throw an exception. In this case, you must create a new context instead.
The set_unit() and set_units() methods can be called before the context is ready.
context.set_unit("db_user_id", "1000013")
context.set_units({
"db_user_id": "1000013"
})
Basic Usage
Selecting a Treatment
treatment = context.get_treatment("exp_test_experiment")
if treatment == 0:
# User is in control group (variant 0)
pass
else:
# User is in treatment group
pass
Treatment Variables
button_color = context.get_variable_value("button.color", "red")
Listing Variable Keys
get_variable_keys() maps each variable key to the name of one experiment that defines it, keeping the v0.2.4 shape. When several experiments define the same key, it returns the lowest-id experiment (the first one variable lookups visit). Use get_variable_keys_all() to get every experiment defining each key, in experiment id order, matching the JavaScript SDK's variableKeys().
keys = context.get_variable_keys()
# {"button.color": "exp_test_abc", "banner.size": "exp_test_ab"}
all_keys = context.get_variable_keys_all()
# {"button.color": ["exp_test_abc"], "banner.size": ["exp_test_ab", "exp_test_banner"]}
Note: v0.2.4 returned one experiment name per key, the last one in the payload. Since 0.3.0
get_variable_keys()returns the lowest-id experiment instead, andget_variable_keys_all()returns every experiment.
Peek at Treatment Variants
Although generally not recommended, it is sometimes necessary to peek at a treatment or variable without triggering an exposure.
The ABsmartly SDK provides a peek_treatment() method for that.
treatment = context.peek_treatment("exp_test_experiment")
if treatment == 0:
# User is in control group (variant 0)
pass
else:
# User is in treatment group
pass
Peeking at Variables
variable = context.peek_variable_value("my_variable", None)
Overriding Treatment Variants
During development, for example, it is useful to force a treatment for an experiment. This can be achieved with the set_override() and/or set_overrides() methods.
The set_override() and set_overrides() methods can be called before the context is ready.
context.set_override("exp_test_experiment", 1)
context.set_overrides({
"exp_test_experiment": 1,
"exp_another_experiment": 0
})
Advanced
Context Attributes
Attributes are used to pass meta-data about the user and/or the request. They can be used later in the Web Console to create segments or audiences.
The set_attribute() and set_attributes() methods can be called before the context is ready.
context.set_attribute("user_agent", request.headers.get("User-Agent"))
context.set_attributes({
"customer_age": "new_customer",
"account_type": "premium"
})
Audience filters can also use application and environment, the application and environment names configured in the SDK. They are always available for audience evaluation, but they are not real attributes: they are never added to published attributes (the collector adds them server-side), not even with include_system_attributes=True, which only publishes sdk_name and sdk_version, and get_attributes() does not return them. Names that are not configured or empty are left out. An attribute you set with the same name, at any time, takes precedence:
context.set_attribute("environment", "staging") # audiences now see "staging"
The SDK no longer exposes an automatic app_version to audiences. An app_version you set yourself with set_attribute("app_version", ...) is still evaluated and published like any other attribute.
Custom Assignments
Sometimes it may be necessary to override the automatic selection of a variant. For example, if you wish to have your variant chosen based on data from an API call. This can be accomplished using the set_custom_assignment() method.
context.set_custom_assignment("exp_test_not_eligible", 3)
If you are running multiple experiments and need to choose different custom assignments for each one, you can do so using the set_custom_assignments() method.
context.set_custom_assignments({
"exp_test_experiment": 1,
"exp_another_experiment": 2
})
Tracking Goals
Goals are created in the ABsmartly web console.
context.track("payment", {
"item_count": 1,
"total_amount": 1999.99
})
Publishing Pending Data
Sometimes it is necessary to ensure all events have been published to the ABsmartly collector, before proceeding.
You can explicitly call the publish() or publish_async() methods.
context.publish()
context.publish_async()
Finalizing
The close() and close_async() methods will ensure all events have been published to the ABsmartly collector, like publish(), and will also "seal" the context, throwing an error if any method that could generate an event is called. Overrides and custom assignments, including the bulk setters, remain allowed after finalization; treatment and variable reads still raise the finalized error.
context.close()
context.close_async()
Custom Event Logger
The ABsmartly SDK can be instantiated with an event logger used for all contexts.
In addition, an event logger can be specified when creating a particular context, in the ContextConfig.
from sdk import ABsmartly, ContextEventLogger, EventType
class CustomEventLogger(ContextEventLogger):
def handle_event(self, event_type: EventType, data):
if event_type == EventType.ERROR:
print(f"Error: {data}")
elif event_type == EventType.READY:
print("Context is ready")
elif event_type == EventType.EXPOSURE:
print(f"Exposed to experiment: {data.name}")
elif event_type == EventType.GOAL:
print(f"Goal tracked: {data.name}")
elif event_type == EventType.REFRESH:
print("Context refreshed")
elif event_type == EventType.PUBLISH:
print("Events published")
elif event_type == EventType.CLOSE:
print("Context closed")
sdk = ABsmartly.create(
endpoint="https://your-company.absmartly.io/v1",
api_key="YOUR-API-KEY",
application="website",
environment="production",
event_logger=CustomEventLogger()
)
# Or with advanced configuration
sdk_config.context_event_logger = CustomEventLogger()
The data parameter depends on the type of event.
Event Types
| Event | When | Data |
|---|---|---|
Error |
Context receives an error |
Exception object |
Ready |
Context turns ready |
ContextData used to initialize |
Refresh |
Context.refresh() method succeeds |
ContextData used to refresh |
Publish |
Context.publish() method succeeds |
PublishEvent sent to collector |
Exposure |
Context.get_treatment() method succeeds on first exposure |
Exposure enqueued for publishing |
Goal |
Context.track() method succeeds |
GoalAchievement enqueued for publishing |
Close |
Context.close() method succeeds the first time |
None |
Finalize |
Logged right after Close (cross-SDK name for the event) |
None |
Platform-Specific Examples
Using with Flask
from flask import Flask, session, render_template
from sdk import ABsmartly, ContextConfig
app = Flask(__name__)
sdk = ABsmartly.create(
endpoint="https://your-company.absmartly.io/v1",
api_key="YOUR-API-KEY",
application="website",
environment="production"
)
@app.route('/')
def index():
context_config = ContextConfig()
context_config.units = {
"session_id": session.get('session_id'),
"user_id": session.get('user_id')
}
ctx = sdk.create_context(context_config)
ctx.wait_until_ready()
treatment = ctx.get_treatment("exp_test_experiment")
if treatment == 0:
return render_template('control.html')
else:
return render_template('treatment.html')
Using with Django
# settings.py
from sdk import ABsmartly
ABSMARTLY_SDK = ABsmartly.create(
endpoint="https://your-company.absmartly.io/v1",
api_key="YOUR-API-KEY",
application="website",
environment="production"
)
# views.py
from django.conf import settings
from django.shortcuts import render
from sdk import ContextConfig
def my_view(request):
context_config = ContextConfig()
context_config.units = {
"session_id": request.session.session_key,
}
ctx = settings.ABSMARTLY_SDK.create_context(context_config)
ctx.wait_until_ready()
treatment = ctx.get_treatment("exp_test_experiment")
context = {'treatment': treatment}
return render(request, 'template.html', context)
Using with FastAPI
from fastapi import FastAPI, Request
from sdk import ABsmartly, ContextConfig
import uuid
app = FastAPI()
sdk = ABsmartly.create(
endpoint="https://your-company.absmartly.io/v1",
api_key="YOUR-API-KEY",
application="website",
environment="production"
)
@app.get("/")
async def root(request: Request):
context_config = ContextConfig()
context_config.units = {
"session_id": str(uuid.uuid4()),
}
ctx = sdk.create_context(context_config)
ctx.wait_until_ready_async().result()
treatment = ctx.get_treatment("exp_test_experiment")
return {"treatment": treatment}
About A/B Smartly
A/B Smartly is the leading provider of state-of-the-art, on-premises, full-stack experimentation platforms for engineering and product teams that want to confidently deploy features as fast as they can develop them. A/B Smartly's real-time analytics helps engineering and product teams ensure that new features will improve the customer experience without breaking or degrading performance and/or business metrics.
Have a look at our growing list of clients and SDKs:
Metadata
Release files for ABSmartly 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| absmartly-0.3.0.tar.gz | 87.4 kB | Without provenance | Details for absmartly-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| absmartly-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for absmartly-0.3.0-py3-none-any.whl |
Total release size: 143.7 kB
Release files / absmartly-0.3.0.tar.gz
| Download URL | absmartly-0.3.0.tar.gz |
|---|---|
| Size | 87.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
33b0e20f1bd7d4b658009007b5b7794f8f29d3964dfcadd283e9d0981048af25
|
|
BLAKE2b-256 checksum How to use checksums |
7c0e5e7ecb4f7d589ca12a3841dfe22990d786dcff66bd64741109b3096814bb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.2
|
Release files / absmartly-0.3.0-py3-none-any.whl
| Download URL | absmartly-0.3.0-py3-none-any.whl |
|---|---|
| Size | 56.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
16cbd51a4e4bfb1cb0e54c3cc61f8dded8bd42ca56230f5ea030efc154b99f88
|
|
BLAKE2b-256 checksum How to use checksums |
249ea981b1de3dc7e1061822321eceb89860c05fe3a18d99e21064a866b99885
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.2
|
Security for ABSmartly 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.