{"id":6664,"library":"harness-featureflags","title":"Harness Feature Flags Python SDK","description":"The `harness-featureflags` library provides the Python server SDK for Harness Feature Flags. It allows developers to integrate feature flag management into their Python applications, enabling dynamic control over features without redeploying code. The library is actively maintained, with frequent patch and minor releases, and is currently at version 1.7.5.","status":"active","version":"1.7.5","language":"en","source_language":"en","source_url":"https://github.com/harness/ff-python-server-sdk","tags":["feature flags","feature toggles","harness","sdk"],"install":[{"cmd":"pip install harness-featureflags","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"HTTP client for API communication.","package":"requests","optional":false},{"reason":"Provides retry mechanisms for network operations.","package":"tenacity","optional":false},{"reason":"Backports and enhancements for Python's typing module.","package":"typing-extensions","optional":false},{"reason":"Used for caching feature flag configurations locally.","package":"cachetools","optional":false},{"reason":"Helps in determining appropriate platform-specific directories for caching.","package":"platformdirs","optional":false},{"reason":"Used for JSON path parsing and querying.","package":"jsonpath-ng","optional":false}],"imports":[{"symbol":"CfClient","correct":"from harness_featureflags.client import CfClient"}],"quickstart":{"code":"import os\nimport time\nfrom harness_featureflags.client import CfClient\nfrom harness_featureflags.util import Target\n\n# Set your Harness Server SDK Key as an environment variable or replace directly\nAPI_KEY = os.environ.get('FF_API_KEY', 'YOUR_HAARNESS_SDK_KEY')\n# Set a target identifier for your user or application\nTARGET_IDENTIFIER = os.environ.get('FF_TARGET_IDENTIFIER', 'test-user-1')\n\nif not API_KEY or API_KEY == 'YOUR_HAARNESS_SDK_KEY':\n    print(\"Error: FF_API_KEY not set. Please set the environment variable or replace the placeholder.\")\n    exit(1)\n\ndef main():\n    client = CfClient(API_KEY)\n    \n    # Wait for the SDK to initialize\n    client.wait_for_initialization()\n\n    target = Target(\n        identifier=TARGET_IDENTIFIER,\n        name=TARGET_IDENTIFIER,\n        attributes={\n            \"email\": f\"{TARGET_IDENTIFIER}@example.com\",\n            \"country\": \"US\"\n        }\n    )\n\n    print(f\"Client initialized. Target: {target.identifier}\")\n\n    try:\n        while True:\n            # Replace 'harnessappdemodarkmode' with your actual feature flag identifier\n            is_dark_mode_enabled = client.bool_variation('harnessappdemodarkmode', target, False)\n            print(f\"Feature flag 'harnessappdemodarkmode' is {'enabled' if is_dark_mode_enabled else 'disabled'} for target {target.identifier}\")\n\n            string_flag_value = client.string_variation('another_string_flag', target, 'default_string')\n            print(f\"Feature flag 'another_string_flag' has value: {string_flag_value}\")\n\n            time.sleep(10)\n    except KeyboardInterrupt:\n        print(\"Stopping...\")\n    finally:\n        client.close()\n        print(\"SDK Closed.\")\n\nif __name__ == '__main__':\n    main()","lang":"python","description":"This quickstart code demonstrates how to initialize the Harness Feature Flags Python SDK, define a target, and evaluate boolean and string feature flags. It continuously checks flag variations and prints their status until interrupted. Remember to replace `FF_API_KEY` with your actual Server SDK Key and update flag identifiers as needed. The SDK handles connection, caching, and updates automatically."},"warnings":[{"fix":"Upgrade to `harness-featureflags` version 1.6.4 or later using `pip install --upgrade harness-featureflags`.","message":"Older versions of the SDK (prior to 1.6.4) had issues with rigidly pinned `tenacity` and `typing_extensions` dependencies, potentially causing conflicts with other libraries in your project. Ensure you are using version 1.6.4 or newer for improved dependency management.","severity":"gotcha","affected_versions":"<1.6.4"},{"fix":"Double-check your `API_KEY` against the one provided in the Harness Platform. Ensure your application's environment has outbound network access to the Harness configuration URL. Listen for the `Event.FAILED` event during SDK initialization for more detailed error information.","message":"SDK initialization failures are commonly caused by an invalid Server SDK Key or network connectivity issues to the Harness Feature Flags configuration service (https://config.ff.harness.io/api/1.0). Verify your key and network access.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Design your application with robust fallback logic using the default values in `variation` calls. For advanced control, you can configure streaming/polling behavior and HTTP client options (e.g., via `with_httpx_args` introduced in v1.7.0) to suit your environment's needs, such as disabling streaming for consistent session flags if updates are not required.","message":"By default, the SDK uses both streaming (for real-time updates) and polling (as a fallback). In case of connection issues, the SDK will gracefully degrade, either serving the most recently cached flag state or the default variation provided in the `variation` call if no cached value exists. Ensure your application logic accounts for these fallback scenarios to prevent unexpected behavior.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}