---
title: OptimizelyDecision for the Swift SDK
slug: feature-experimentation/docs/optimizelydecision-swift
docTags: 
createdAt: 2026-08-05T20:39:55.000Z
---

## Version

SDK v3.7 and higher

## Object model

The following shows the object model for the OptimizelyDecision object.

```swift
public struct OptimizelyDecision {
   public let variationKey: String?
   public let enabled: Bool
   public let variables: OptimizelyJSON
   public let ruleKey: String?     

   public let flagKey: String
   public let userContext: OptimizelyUserContext
   public let reasons: [String]
}
```

The following table describes the fields of the OptimizelyDecision object:

| Field            | Type                  | Description                                                                                                                                                                                                                                                                                                                                                                                                                          |
| ---------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| enabled          | Boolean               | Whether the flag is enabled or not. The SDK never returns nil for this field; it returns false when it fails to make a decision due to an error.                                                                                                                                                                                                                                                                                     |
| variables        | OptimizelyJSON        | Key-value pairs of all the flag variables. When there are no variables defined for the flag or SDK cannot get variables, this field returns an empty map. When SDK fails to get variables for a decision, the Reasons field includes the error messages regardless of the Include Reasons option.                                                                                                                                    |
| \`variationKey\` | String (nullable)     | When SDK fails to make a decision, this field returns nil and the Reasons field includes the log messages if the Include Reasons option is set.\ In legacy projects, there is an edge case: 1. no messages in the Reasons field and 2. nil value in the \`variationKey\` field means that a variation cannot be found (for example, because the flag is part of a rollout).                                                          |
| \`ruleKey\`      | String (nullable)     | The key of the flag rule that the user bucketed into for this decision.\ When a valid rule key is not available, returns a copy of the experiment key.\ Returns nil if neither a valid rule key nor experiment key is found.\ If the SDK buckets the user into a holdout, this field returns the holdout key. If the holdout excludes Targeted Deliveries and the user receives a delivery rule, this field returns that rule's key. |
| \`flagKey\`      | String                | The key of the flag for which the decision was made.                                                                                                                                                                                                                                                                                                                                                                                 |
| userContext      | OptimizelyUserContext | The user associated with the call to the Decide method.                                                                                                                                                                                                                                                                                                                                                                              |
| reasons          | Array                 | An array of relevant error and log messages, in chronological order. Optimizely returns messages in this field: \* when errors are detected while making decisions. \* when reasons for decisions are enabled in the decide options \* an empty array when there are no messages. See the following section for information.                                                                                                         |

## Reasons

The returned decision object includes error and log messages in the Reasons field.

## Error reasons

For the sake of consistency with other decision results, Optimizely returns a decision with nil fields instead of throwing errors or exceptions.

Critical errors are always included in the reasons field of the returned decision object regardless of the include reasons option you set on the Decide method.

| Error                | Possible reasons                                                                                               |
| -------------------- | -------------------------------------------------------------------------------------------------------------- |
| nil \`variationKey\` | - Optimizely SDK not configured properly yet<br />- User context is not valid<br />- No flag was found for key |
| empty \`variables\`  | - Invalid variables for OptimizelyJSON<br />- Variable value for key is invalid or wrong type                  |

## Log reasons

Optimizely collects log messages in the reasons field of the returned OptimizelyDecision object in the order in which they are logged.

The following types of log messages are only included in the reasons field if you set the include reasons option on the Decide method:

- Log messages related to bucketing users based on audiences or attributes.
- Log messages related to bucketing users based on traffic percentages.

## Examples

For an example of a returned decision object, see [Decide methods](docId\:m4P0yMbtf7fA3weCgyLGn).
