---
title: OptimizelyUserContext for the Python SDK
slug: feature-experimentation/docs/optimizelyusercontext-python
docTags: 
createdAt: 2026-08-05T20:39:56.000Z
---

The OptimizelyUserContext object lets you make flag decisions and track events for a user context you have already created using the [Create User Context](docId\:IQktksuBns7WFb6Xv_OVE) method.

Also, if you have configured [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8), you can evaluate if your user would qualify for a [real-time audience from Optimizely Data Platform (ODP)](docId:7-qDl01uxXDPl5_jFVF1E).

## OptimizelyUserContext minimum SDK version

`OptimizelyUserContext` is supported on SDK v3.8.0 and higher.

### Forced decision methods minimum SDK version

The `set_forced_decision()`, `get_forced_decision()`, `remove_forced_decision()` and `remove_all_forced_decision()` methods are supported on 4.0.0 and higher.

### Real-Time Audiences for Feature Experimentation minimum SDK version

The `fetch_qualified_segments()` and `is_qualified_for()` methods are supported on version 5.0.0 and higher.

## `OptimizelyUserContext` definition

The following code sample demonstrates the object definition for `OptimizelyUserContext`:

```python
class OptimizelyUserContext(object):
    
  # set an attribute for the user
  def set_attribute(self, attribute_key, attribute_value):
    
  # get attributes for the user
  def get_user_attributes(self):

  # make a decision about which flag variation the user buckets into for the flag key 
  def decide(self, key, options=None):

  # make decisions about which flag variations the user buckets into for flag keys 
  def decide_for_keys(self, keys, options=None):

  # make decisions about which flag variations the user buckets into for all flags 
  def decide_all(self, options=None):

  # track user event
  def track_event(self, event_key, event_tags=None):
      
  # OptimizelyDecisionContext
  class OptimizelyDecisionContext(object):
    def __init__(self, flag_key, rule_key):
      
  # OptimizelyForcedDecision
  class OptimizelyForcedDecision(object):
    def __init__(self, variation_key):

  # Sets the forced decision (variation_key) for a given decision context
  def set_forced_decision(self, OptimizelyDecisionContext, OptimizelyForcedDecision):

  # Returns the forced decision for a given decision context
  def get_forced_decision(self, OptimizelyDecisionContext):

  # Removes the forced decision for a given decision context
  def remove_forced_decision(self, OptimizelyDecisionContext):

  # Removes all forced decisions bound to this user context
  def remove_all_forced_decisions(self):
  
  # The following methods require Real-Time Audiences for Feature Experimentation. 
  # See note following this code sample.

  # Returns the saved results of **fetch_qualified_segments()**. 
  # Can be None if not properly updated with fetch_qualified_segments().  
  def get_qualified_segments(self):
   
  # Overwrite the qualified segments array. 
  # This lets you bypass the remote fetching process from ODP 
  # or for utilizing your own fetching service.  
  def set_qualified_segments(self, segments):
      
  # Fetch all qualified audience segments for the user context.
  # If no callback is provided, this method fetches the qualified segments
  # and return a boolean signifying success.
  #
  # If a callback is provided, the method fetches segments in a separate thread, 
  # invoke the provided callback when results are available, and return the thread handle.
  def fetch_qualified_segments(callback=None, options=None):
 
  # Check if the user is qualified for the given segment. 
  def is_qualified_for(self, segment):
```

:::hint{type="info"}
**Note**
You must first configure  [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8) to be able to call the `get_qualified_segments()`, `set_qualified_segments()`, `fetch_qualified_segments()`, and `is_qualified_for()` methods.
:::

## Properties

The following table shows attributes for the `OptimizelyUserContext` object:

| Attribute                 | Type   | Comment                                                                                                                                                                                                                                                      |
| ------------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| \`user\_id\`              | String | The ID of the user                                                                                                                                                                                                                                           |
| \`attributes\` (Optional) | Dict   | A dictionary of custom key-value pairs specifying attributes for the user that are used for \[audience targeting]\(https\://support.optimizely.com/hc/en-us/articles/38683417490957). You can pass the dictionary with the user ID when you create the user. |

## Methods

The following table shows the methods for the `OptimizelyUserContext` object:

| Method                              | Comment                                                                                                                                                                                                                                                                                                                                                                     |
| ----------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| \`set\_attribute\`                  | Pass a custom user attribute as a key-value pair to the user context.                                                                                                                                                                                                                                                                                                       |
| \`decide\`                          | Returns a decision result for a flag key for a user. The method returns the decision result in an OptimizelyDecision object, which contains all data required to deliver the flag rule. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-python#decide)                                                           |
| \`decide\_for\_keys\`               | Returns a dictionary of flag decisions for specified flag keys. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-python#decide-for-keys)                                                                                                                                                                          |
| \`decide\_all\`                     | Returns decisions for all active (unarchived) flags for a user. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-python#decide-all)                                                                                                                                                                               |
| \`track\_event\`                    | Tracks a conversion event for a user (an action a user takes) and logs an error message if the specified event key does not match any existing events. See \[Track Event]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/track-event-python)                                                                                                         |
| \`set\_forced\_decision\`           | Forces a user into a specific variation. See \[Set Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-python#set-forced-decision-method---set\_forced\_decision)                                                                                                                                                |
| \`get\_forced\_decision\`           | Returns what variation the user was forced into. See \[Get Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-python#get-forced-decision-method---get\_forced\_decision)                                                                                                                                        |
| \`remove\_forced\_decision\`        | Removes a user from a specific forced variation. See \[Remove Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-python#remove-forced-decision-method---remove\_forced\_decision)                                                                                                                               |
| \`remove\_all\_forced\_decisions\`  | Removes a user from all forced variations. See \[Remove All Forced Decisions]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-python#remove-all-forced-decisions-method---remove\_all\_forced\_decisions)                                                                                                                     |
| \`fetch\_qualified\_segments\` \*\* | Fetch all ODP real-time audiences that the user context qualified for. Has a synchronous and asynchronous implementation. See \[Real-Time Audiences for Feature Experimentation segment qualification methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-python). |
| \`is\_qualified\_for\` \*\*         | Checks if the user context qualifies for a given ODP real-time audience. See \[Real-Time Audiences for Feature Experimentation segment qualification methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-python).                                                  |

\*\* Requires [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8).

## See also

[Create User Context](docId\:IQktksuBns7WFb6Xv_OVE)

## Source files

The language and platform source files containing the implementation for Python are available on [GitHub](https://github.com/optimizely/python-sdk/blob/master/optimizely/optimizely.py).
