Skip to main content
The Winning Variant Python SDK is currently in Public Preview. If you notice any issues with the SDK, please report issues to your Winning Variant account team.
The Winning Variant Python SDK makes it easier to implement split tests directly in your python applicatios, AI/ML workloads, or Streamlit apps. It uses an existing Winning Variant Snowflake Native App installation, accessed via a session object provided to the client.

Installation

General Installation

Install the SDK by running the following: pip install winningvariant

Import into Snowflake UDFs and procedures using Artifact Registry

The Snowflake Artifact Registry allows you to directly use Python packages from the Python Package Index (PyPI) within Snowpark Python user-defined functions (UDFs) and stored procedures. Follow the instructions in the linked article to use the winningvariant package.

Use in Snowflake Notebooks / Worksheets

Within a notebook or worksheet, select Packages and add the winningvariant package. Python Sdk In Snowflake

Initialization

To initialize the SDK, import the object and inintialize it using a Snowflake Snowpark session object:

Options

The following intializing parameters are available:
The user defined in the session must have one of the following application roles:
  • admin
  • editor
  • scientist
Caching Caching can be enabled/disabled after client initialization by calling wv.enable_cache() or wv.disable_cache(), respectively.

Assignment Object

The SDK includes an Assignment object that is used to identify the ID of a variant that a subject is assigned within an experiment. Read more here. The Assignment object includes a reference to the subject_id, experiment_id, and the variant it’s assigned to.

Assignment Comparison

To test if an assignment is for a given variant, you can call is_variant("<Variant>") or do a string comparison:

Assignment Management

The SDK provides various ways to get/create experiment assignments based on your needs.

Get Assignment

This read-only method gets an existing assignment for a subject ID inside of an experiment. If none exists, it will not be created.
Returns an Assignment object if one exists, otherwise None.
Example

Get or Create Assignment

Gets an assignment if one exists, otherwise it creates a new one according to the experiment definition.
Returns an Assignment object upon success, None if something went wrong.
Example

Check if a subject has a specific assignment with an experiment

If you have a subject for which you want to do a quick check to see if they have a particular assignment or not, you can use the shorthand check_variant function. A good use case for this would be in 2-variant A/B tests or feature flag scenarios where you want to quickly check if a subject is in the treatment group.
Returns a boolean indicating if the subject has the given assignment in the experiment.
Example

Decorators

The SDK provides a number of function decorators to provide optionality in how you implement split tests in our codebase. Each decorator requires the following:
  1. An explicit subject_id OR subject_arg that specifes the argument passed to the function that will include the subject ID.
  2. An explicit experiment_id OR experiment_arg that specifies the argument passed to the function that will include the experiment ID.

@<client>.assignment

This decorator provides the assignment to the wrapped function. If no assignment exist for the subject, one is created. Supports syncronous and asyncronous functions.
Example

@<client>.if_assignment

This decorator executes the wrapped function only if the subject has the given assignment within the experiment. In addition to the general required parameter combinations defined above, this decorator has a few additional parameters:
Example

@<client>.unless_assignment

This decorator executes the wrapped function only if the subject DOES NOT HAVE the given assignment within the experiment. In addition to the general required parameter combinations defined above, this decorator has a few additional parameters:
Example