{"id":6021,"library":"optimizely-sdk","title":"Optimizely Python SDK","description":"The Optimizely Python SDK enables developers to integrate Optimizely Feature Experimentation, Optimizely Full Stack (legacy), and Optimizely Rollouts into their Python applications. It allows for server-side A/B testing, feature flagging, and personalization. The library is actively maintained with regular releases, currently at version 5.4.0.","status":"active","version":"5.4.0","language":"en","source_language":"en","source_url":"https://github.com/optimizely/python-sdk","tags":["A/B testing","feature flags","experimentation","optimizely","personalization"],"install":[{"cmd":"pip install optimizely-sdk","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used for making HTTP requests, primarily for fetching the Optimizely datafile from the CDN and sending impression/conversion events.","package":"requests","optional":false}],"imports":[{"symbol":"Optimizely","correct":"from optimizely import Optimizely"}],"quickstart":{"code":"import os\nfrom optimizely import Optimizely\n\n# The Optimizely datafile is a JSON string describing your experiments and features.\n# In a real application, you would typically fetch this from Optimizely's CDN\n# or use a dedicated datafile manager. For this example, we'll use an environment\n# variable or a minimal placeholder.\n# Replace 'YOUR_OPTIMIZELY_DATAFILE_JSON_HERE' with your actual datafile content.\n# A minimal valid datafile for testing might look like:\n# '{\"version\": \"4\", \"revision\": \"1\", \"projectId\": \"123\", \"experiments\": [], \"featureFlags\": []}'\ndatafile_json = os.environ.get(\n    'OPTIMIZELY_DATAFILE',\n    '{\"version\": \"4\", \"revision\": \"1\", \"projectId\": \"123\", \"experiments\": [], \"featureFlags\": []}'\n)\n\n# Initialize the Optimizely client\n# Consider using an event dispatcher and logger for production environments.\noptimizely_client = Optimizely(datafile_json=datafile_json)\n\n# Example: Check if a feature flag is enabled for a user\nuser_id = \"user_xyz\"\nuser_attributes = {\"browser_type\": \"chrome\", \"plan_type\": \"premium\"}\nfeature_key = \"example_feature\"\n\nif optimizely_client.is_feature_enabled(feature_key, user_id, user_attributes):\n    print(f\"Feature '{feature_key}' is ENABLED for user '{user_id}'.\")\nelse:\n    print(f\"Feature '{feature_key}' is DISABLED for user '{user_id}'.\")\n\n# Example: Decide on a flag and get its variables\nflag_key = \"new_ui_variant\"\ndecision = optimizely_client.decide(flag_key, user_id, user_attributes)\n\nprint(f\"\\nDecision for flag '{flag_key}':\")\nprint(f\"  Variation Key: {decision.variation_key}\")\nprint(f\"  Enabled: {decision.enabled}\")\nprint(f\"  Variables: {decision.variables}\")\nprint(f\"  Reasons: {decision.reasons}\")\n\n# Example: Track a conversion event\nevent_key = \"purchase_complete\"\nevent_tags = {\"revenue\": 49.99, \"currency\": \"USD\"}\noptimizely_client.track(event_key, user_id, user_attributes, event_tags)\nprint(f\"\\nTracked event '{event_key}' for user '{user_id}'.\")\n\n# It's important to close the client to ensure background threads are terminated cleanly.\noptimizely_client.close()\nprint(\"\\nOptimizely client closed.\")\n","lang":"python","description":"This quickstart demonstrates how to initialize the Optimizely client, check the status of a feature flag, make a decision on an experiment flag, and track a conversion event. It's crucial to provide a valid `datafile_json` and manage the client's lifecycle, including calling `close()` when it's no longer needed."},"warnings":[{"fix":"Review the Optimizely documentation on Advanced Audience Targeting and ODP integration. Ensure your `OptimizelyUserContext` attributes and audience definitions align with the new capabilities and requirements. Test existing experiments thoroughly.","message":"Version 5.0.0 introduced significant new features, notably the integration with Optimizely Data Platform (ODP) for Advanced Audience Targeting. While core API signatures might not have changed universally, this release impacts how audiences are defined, how user attributes are processed, and potentially requires a re-evaluation of your experimentation strategy and data integration if you leverage advanced targeting.","severity":"breaking","affected_versions":"5.0.0 and later"},{"fix":"If your application or its other dependencies require `pyOpenSSL` or `cryptography`, ensure they are explicitly installed in your environment.","message":"In version 5.0.1, the `pyOpenSSL` and `cryptography` dependencies were removed. This change reduces dependency bloat, but applications that implicitly relied on `optimizely-sdk` to install these for other unrelated purposes may encounter `ModuleNotFoundError`.","severity":"gotcha","affected_versions":"5.0.1 and later"},{"fix":"Implement a robust datafile management strategy. This typically involves using a `DatafileManager` (or custom logic) to fetch the datafile from the Optimizely CDN, cache it locally, and refresh it periodically. Ensure your application handles network failures gracefully when fetching the datafile.","message":"The SDK requires an Optimizely datafile as a JSON string for initialization. Managing the fetching, caching, and refreshing of this datafile is critical for performance and ensuring experiments are up-to-date. The SDK provides mechanisms but does not automatically handle all aspects of persistence or complex caching.","severity":"gotcha","affected_versions":"All versions"},{"fix":"After upgrading, review your experiment data and user assignment reports to confirm the new, correct behavior aligns with your expectations for data integrity and experiment outcomes.","message":"Version 5.4.0 includes fixes for impression event handling and global holdout implementation. These changes correct how events are dispatched and how users assigned to a global holdout are processed, which could subtly alter collected experiment data or user assignment behavior if your system was inadvertently relying on previous, potentially incorrect, logic.","severity":"gotcha","affected_versions":"5.4.0 and later"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}