Quickstart
The Eppo Python SDK enables feature flags and experiments in your Python applications with only a few lines of code.
The SDK handles all the complexity of feature flag evaluation and experiment assignment locally in your application, with no network calls required after initial setup. This guide will walk you through installing the SDK and implementing your first feature flag, experiment, and contextual bandit.
Installation
First, install the SDK using pip:
pip install eppo-server-sdk
Feature Flags
Feature flags are a way to toggle features on and off without needing to deploy code.
Initialize the SDK
Create an SDK key if you don't already have one.
First, initialize the SDK using your SDK key:
import eppo_client
from eppo_client import ClientConfig
client_config = ClientConfig(api_key="<SDK-KEY>")
eppo_client.init(client_config)
The SDK key is different from the project API key. You can find your SDK key in the SDK Keys section of the Eppo interface.
Assign a variant
Once initialized, you can get an instance of the client and start making assignments:
client = eppo_client.get_instance()
user = get_current_user()
variation = client.get_string_assignment(
'show-new-feature',
user.id,
{ 'country': user.country },
'control'
)
if variation == "variant-a":
handle_variant_a()
elif variation == "variant-b":
handle_variant_b()
else:
handle_control()
Assignment Types
The SDK provides different assignment functions based on the type of value you need:
Function | Return Type |
---|---|
get_string_assignment() | String |
get_boolean_assignment() | Boolean |
get_json_assignment() | JSON object |
get_integer_assignment() | Integer |
get_numeric_assignment() | Float |
See more details about assignment functions in the Assignments page.
Experiments
While feature flags are useful, they do not send you any information about how your users are interacting with the feature. Experiments provide a way to collect data about these interactions using your preferred logging system.
To log events through the SDK, you need to implement the AssignmentLogger
class:
from eppo_client import AssignmentLogger
class MyLogger(AssignmentLogger):
def log_assignment(self, assignment):
print(f"Logging assignment: {assignment}") # Replace with your logging logic
client_config = ClientConfig(
api_key="<SDK-KEY>",
assignment_logger=MyLogger()
)
eppo_client.init(client_config)
In a production application, you would want to replace the print statement with an actual logging system. We have documentation on how to set up logging with multiple popular data warehouses and logging systems in the Assignments page.
Contextual Bandits
Contextual Multi-Armed Bandits are a way to dynamically optimize assignments based on user context. A bandit balances exploration of new actions with exploitation of known successful actions to maximize a specified metric.
Bandit Setup
Setting up a bandit requires implementing both an assignment logger and a bandit logger:
from eppo_client import AssignmentLogger
class MyLogger(AssignmentLogger):
def log_assignment(self, assignment):
print(f"Logging assignment: {assignment}")
def log_bandit_action(self, bandit_action):
print(f"Logging bandit action: {bandit_action}")
client_config = ClientConfig(
api_key="<SDK-KEY>",
assignment_logger=MyLogger()
)
eppo_client.init(client_config)
Query the bandit for actions
Instead of making simple assignments with a bandit, you query the bandit for actions:
import eppo_client
from eppo_client import ContextAttributes
client = eppo_client.get_instance()
bandit_result = client.get_bandit_action(
"shoe-bandit",
user.id,
ContextAttributes(
numeric_attributes={"age": user.age},
categorical_attributes={"country": user.country}
),
{
"nike": ContextAttributes(
numeric_attributes={"brand_affinity": 2.3},
categorical_attributes={"previously_purchased": True},
),
"adidas": ContextAttributes(
numeric_attributes={"brand_affinity": 0.2},
categorical_attributes={"previously_purchased": False},
),
},
"control"
)
if bandit_result.action:
show_shoe_ad(bandit_result.action)
else:
show_default_ad()
For full steps to create a bandit including UI steps, see the bandit quickstart.
Next Steps
Now that you've seen how to make assignments with the Eppo Python SDK, we recommend familiarizing yourself with: