---
title: Forced decision methods for the Python SDK
slug: feature-experimentation/docs/forced-decision-methods-python
docTags: 
createdAt: 2026-08-05T20:39:55.000Z
---

These methods help test and debug various flows of your client applications by forcing users into a specific variation.

The Optimizely Feature Experimentation Python SDK checks forced decisions before making any decisions. If a matching item is found for the requested flag, the Python SDK returns the forced decision immediately ([audience conditions](docId\:UDBvr5a8WHNkyOqzPhOr9) and traffic allocations are ignored) before making normal decisions.

The Python SDK checks for forced decisions at the start of each decision process. If a matching forced decision is found, it returns the decision immediately.

- **Flag-to-Decision** – The SDK checks at the start of any decide call for the given flag.
- **Experiment-Rule-to-Decision** – The SDK checks at the start of the decision for the given experiment rule of the flag key.
- **Delivery-Rule-to-Decision** – The SDK checks at the start of the decision for the given delivery rule of the flag key.

:::hint{type="danger"}
**Warning**
You must associate your variations to a [flag rule](docId:-yq2-thNRv7-SgwCXpG90) before calling any forced decision methods.
:::

On forced decisions, the SDK fires impression events and notifications just like other normal decisions (unless disabled by the [DISABLE\\\_DECISION\\\_EVENT](docId\:i0YJZkiaylg7J6QKk3GR_) option).

:::hint{type="info"}
**Note**
These forced decisions are not persistent and are cleared when the [OptimizelyUserContext](docId\:UGnzh86pPS0MtU91FSSlQ) is re-initialized.
:::

For information about each method, click on the method name.

- [set\\\_forced\\\_decision](docId\:EPigw-Nc5Q8TavnThh1hO)
- [get\\\_forced\\\_decision](docId\:EPigw-Nc5Q8TavnThh1hO)
- [remove\\\_forced\\\_decision](docId\:EPigw-Nc5Q8TavnThh1hO)
- [remove\\\_all\\\_forced\\\_decisions](docId\:EPigw-Nc5Q8TavnThh1hO)

### OptimizelyDecisionContext

```python
class OptimizelyDecisionContext(object):
  def __init__(self, flag_key, rule_key):
```

### OptimizelyForcedDecision

```python
class OptimizelyForcedDecision(object):
  def __init__(self, variation_key):
```

## Set forced decision method - set\_forced\_decision()

### Version

4.0.0

### Description

Sets a forced decision (`variation_key`) for a given `OptimizelyDecisionContext`.

### Parameters

This table lists the required and optional parameters for the Python SDK.

| Parameter                  | Type  | Description                                                                                                                                      |
| -------------------------- | ----- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| **context** \*required\*   | Class | An instance of \`OptimizelyDecisionContext\` with the required \`flag\_key\` and optional \`rule\_key\` for the forced decision you want to set. |
| **decision**\ \*required\* | Class | An instance of \`OptimizelyForcedDecision\` with the required \`variation\_key\` for the forced decision you want to set.                        |

### Returns

A boolean value that indicates if setting the forced decision (`variation_key`) was completed.

### Example

See the full Python SDK example [here](docId\:EPigw-Nc5Q8TavnThh1hO).

## Get forced decision method - get\_forced\_decision()

### Version

4.0.0

### Description

Returns the forced decision (`variation_key`) for a given `OptimizelyDecisionContext`. Returns null if there is no matching item.

### Parameters

This table lists the required and optional parameters for the Python SDK.

| Parameter                | Type  | Description                                                                                                                                      |
| ------------------------ | ----- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| **context** \*required\* | Class | An instance of \`OptimizelyDecisionContext\` with the required \`flag\_key\` and optional \`rule\_key\` for the forced decision you want to get. |

### Returns

A forced decision `OptimizelyForcedDecision` instance for the context or None if there is no matching item.

### Example

See the full Python SDK example [here](docId\:EPigw-Nc5Q8TavnThh1hO).

## Remove forced decision method - remove\_forced\_decision()

### Version

4.0.0

### Description

Removes the forced decision (`variation_key`) for a given `OptimizelyDecisionContext`.

### Parameters

This table lists the required and optional parameters for the Python SDK.

| Parameters               | Type  | Description                                                                                                                                         |
| ------------------------ | ----- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\* | Class | An instance of \`OptimizelyDecisionContext\` with the required \`flag\_key\` and optional \`rule\_key\` for the forced decision you want to remove. |

### Returns

A success/failure boolean status if the forced decision (`variation_key`) was removed.

### Example

See the full Python SDK example [here](docId\:EPigw-Nc5Q8TavnThh1hO).

## Remove all forced decisions method - remove\_all\_forced\_decisions()

### Version

4.0.0

### Description

Removes all forced decisions (`variation_key`) for the user context.

### Parameters

This table lists the required and optional parameters for the Python SDK.

| Parameters | Type | Description |
| ---------- | ---- | ----------- |
| None       | N/A  | N/A         |

### Returns

A success/failure boolean status.

### Example

See the full Python SDK example [here](docId\:EPigw-Nc5Q8TavnThh1hO).

## Full code example

```python
from optimizely import optimizely, optimizely_user_context

optimizely_client = optimizely.Optimizely(sdk_key="<YOUR_SDK_KEY>")

user = optimizely_client.create_user_context("test_user", attributes)

flag_context = optimizely_user_context.OptimizelyUserContext.OptimizelyDecisionContext("flag-1",None)
flag_and_ab_test_context = optimizely_user_context.OptimizelyUserContext.OptimizelyDecisionContext("flag-1","ab-test-1")
flag_and_delivery_rule_context = optimizely_user_context.OptimizelyUserContext.OptimizelyDecisionContext("flag-1","delivery-1")
variation_a_forced_decision = optimizely_user_context.OptimizelyUserContext.OptimizelyForcedDecision("variation-a")
variation_b_forced_decision = optimizely_user_context.OptimizelyUserContext.OptimizelyForcedDecision("variation-b")
variation_on_forced_decision = optimizely_user_context.OptimizelyUserContext.OptimizelyForcedDecision("on")

# set a forced decision for a flag
success = user.set_forced_decision(flag_context, variation_a_forced_decision)
decision = user.decide("flag-1")

# set a forced decision for an ab-test rule
success = user.set_forced_decision(flag_and_ab_test_context, variation_b_forced_decision)
decision = user.decide("flag-1")

# set a forced variation for a delivery rule
success = user.set_forced_decision(flag_and_delivery_rule_context, variation_on_forced_decision)
decision = user.decide("flag-1")

# get forced variations
forced_decision = user.get_forced_decision(flag_context)
print(f"[ForcedDecision] variation_key = {forced_decision}")

# remove forced variations
success = user.remove_forced_decision(flag_and_ab_test_context)
success = user.remove_all_forced_decision()
```

## See also

[OptimizelyUserContext](docId\:UGnzh86pPS0MtU91FSSlQ)

## Source files

The source files are available on [GitHub](https://github.com/optimizely/python-sdk).
