{"id":4536,"library":"flagsmith","title":"Flagsmith Python SDK","description":"The Flagsmith Python SDK enables you to integrate feature flags and remote configuration into your Python applications. It allows for managing features across multiple projects, environments, and organizations, facilitating controlled rollouts and A/B testing. The current version is 5.1.1, and it typically sees regular releases with bug fixes, features, and dependency updates.","status":"active","version":"5.1.1","language":"en","source_language":"en","source_url":"https://github.com/Flagsmith/flagsmith-python-client","tags":["feature flags","remote config","flagsmith","feature toggle","ab testing"],"install":[{"cmd":"pip install flagsmith","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used by the underlying Flagsmith engine. Compatibility issues may arise with Pydantic V1 in older SDK versions.","package":"pydantic","optional":false}],"imports":[{"symbol":"Flagsmith","correct":"from flagsmith import Flagsmith"}],"quickstart":{"code":"import os\nfrom flagsmith import Flagsmith\n\n# Replace 'YOUR_ENVIRONMENT_KEY' with your actual Flagsmith Environment Key\n# It's recommended to use environment variables for keys in production.\n# Server-side Environment Keys begin with 'ser.'\nenvironment_key = os.environ.get('FLAGSMITH_ENVIRONMENT_KEY', 'YOUR_ENVIRONMENT_KEY')\n\n# Initialize Flagsmith client\n# enable_local_evaluation=True is recommended for production for performance\n# and resilience, as it polls the API in a separate thread.\nflagsmith = Flagsmith(\n    environment_key=environment_key,\n    enable_local_evaluation=True\n)\n\n# Wait for initial flags to be loaded if using local evaluation\n# In a real application, you might want to handle this asynchronously\n# or with a proper startup sequence.\nflagsmith.update_environment()\n\n# Get all flags for the environment\nenvironment_flags = flagsmith.get_environment_flags()\n\n# Check if a feature is enabled\nif environment_flags.is_feature_enabled('my_feature'):\n    print(\"My feature is enabled!\")\n\n# Get a feature value\nfeature_value = environment_flags.get_feature_value('another_feature')\nprint(f\"Value of 'another_feature': {feature_value}\")\n\n# Get flags for an identity\n# Traits will automatically be persisted against the identity in Remote Evaluation mode.\n# In Local Evaluation mode, only traits provided at runtime are used.\nidentity_flags = flagsmith.get_identity_flags(identifier='test_user_123', traits={'plan': 'premium'})\nif identity_flags.is_feature_enabled('premium_feature'):\n    print(\"Premium feature is enabled for test_user_123!\")\n\n# Clean up / close the Flagsmith client (important for local evaluation threads)\nflagsmith.close()","lang":"python","description":"Initializes the Flagsmith client, retrieves environment flags, checks feature status, gets feature values, and demonstrates how to get flags for a specific identity with traits. It highlights the use of local evaluation for performance and secret management via environment variables."},"warnings":[{"fix":"Upgrade Pydantic to V2, or for temporary compatibility, change pydantic imports to `pydantic.v1` and use tools like `bump-pydantic` to assist migration.","message":"Flagsmith Python SDK version 3.5.0 introduced a dependency on Pydantic V2. Projects still using Pydantic V1 will encounter compatibility issues.","severity":"breaking","affected_versions":">=3.5.0, <4.0.0"},{"fix":"Refer to the official Flagsmith Python SDK `CHANGELOG.md` on GitHub or the Flagsmith documentation for detailed migration guides relevant to your upgrade path.","message":"Major versions 4.0.0 and 5.0.0 included breaking changes. The changelog on GitHub should be consulted for specific migration steps. These often involve updates to the SDK's internal engine or API interactions.","severity":"breaking","affected_versions":">=4.0.0, >=5.0.0"},{"fix":"Always generate and use a Server-side Environment Key for your Python applications. Ensure it's stored securely, preferably via environment variables, and never exposed publicly.","message":"Server-side SDKs require a Server-side Environment Key, which starts with `ser.`. Using a Client-side key will lead to authentication failures or incorrect behavior. These keys should be treated as secret.","severity":"gotcha","affected_versions":"All"},{"fix":"Call `flagsmith.close()` when your application is shutting down to properly terminate background threads. During startup, allow sufficient time for the initial environment document to be fetched before making flag evaluations, or implement retry mechanisms.","message":"When using `enable_local_evaluation=True`, the SDK fetches the environment document in a separate thread. Ensure proper initialization and shutdown procedures to avoid resource leaks or outdated flag evaluations. The `close()` method should be called when the application exits.","severity":"gotcha","affected_versions":"All"},{"fix":"For performance-critical applications, especially in production, enable local evaluation (`enable_local_evaluation=True`) to minimize network latency. Understand the implications for real-time flag updates versus polling frequency.","message":"Remote Evaluation mode makes an API call for every `get_environment_flags()` or `get_identity_flags()` request, which can introduce latency. Local Evaluation mode can improve performance but evaluates flags based on a locally cached environment document.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}