GrowthBook provider for OpenFeature
Project description
GrowthBook OpenFeature Provider for Python
A Python implementation of an OpenFeature provider for GrowthBook, enabling standardized feature flag evaluation in Python applications.
Features
- Full implementation of the OpenFeature provider interface
- Support for all flag types (boolean, string, integer, float, object)
- Proper context mapping between OpenFeature and GrowthBook
- Asynchronous and synchronous initialization options
- Comprehensive error handling
- Resource cleanup utilities
Installation
pip install growthbook-openfeature-provider
Quick Start
import asyncio
from openfeature.api import OpenFeatureAPI
from openfeature.evaluation_context import EvaluationContext
from growthbook_openfeature_provider import GrowthBookProvider, GrowthBookProviderOptions
# Create and initialize the provider
provider = GrowthBookProvider(GrowthBookProviderOptions(
api_host="https://cdn.growthbook.io",
client_key="sdk-abc123" # Replace with your actual SDK key
))
# Initialize the provider
async def setup():
await provider.initialize()
# Register with OpenFeature
OpenFeatureAPI.set_provider(provider)
# Get a client
client = OpenFeatureAPI.get_client("my-app")
# Create an evaluation context with targeting information
context = EvaluationContext(
targeting_key="user-123",
attributes={
"country": "US",
"email": "user@example.com",
"premium": True
}
)
# Evaluate a flag
value = client.get_boolean_value("my-flag", False, context)
print(f"Flag value: {value}")
# Clean up resources when done
await provider.close()
# Run the async function
asyncio.run(setup())
Synchronous Usage
If you prefer synchronous initialization:
from openfeature.api import OpenFeatureAPI
from growthbook_openfeature_provider import GrowthBookProvider, GrowthBookProviderOptions
# Create provider
provider = GrowthBookProvider(GrowthBookProviderOptions(
api_host="https://cdn.growthbook.io",
client_key="sdk-abc123"
))
# Initialize synchronously
provider.initialize_sync()
# Register with OpenFeature
OpenFeatureAPI.set_provider(provider)
Configuration Options
The GrowthBookProviderOptions class accepts the following parameters:
| Parameter | Type | Description | Default |
|---|---|---|---|
api_host |
str |
URL of the GrowthBook API | Required |
client_key |
str |
API key for authentication | Required |
decryption_key |
str |
Key for encrypted features | "" |
cache_ttl |
int |
Cache duration in seconds | 60 |
enabled |
bool |
Whether GrowthBook is enabled | True |
qa_mode |
bool |
Enable QA mode for testing | False |
on_experiment_viewed |
Callable |
Callback when experiments are viewed | None |
sticky_bucket_service |
AbstractStickyBucketService |
Service for consistent experiment assignments | None |
Evaluation Context
The provider maps OpenFeature evaluation context to GrowthBook user context:
- The
targeting_keyis mapped to theidattribute - All other attributes are passed through directly
Example:
context = EvaluationContext(
targeting_key="user-123",
attributes={
"country": "US",
"deviceId": "device-456",
"premium": True
}
)
This creates a GrowthBook context with:
{
"id": "user-123",
"country": "US",
"deviceId": "device-456",
"premium": true
}
Flag Evaluation
The provider supports all OpenFeature flag types:
# Boolean flags
boolean_value = client.get_boolean_value("my-boolean-flag", False, context)
# String flags
string_value = client.get_string_value("my-string-flag", "default", context)
# Integer flags
int_value = client.get_integer_value("my-number-flag", 0, context)
# Float flags
float_value = client.get_float_value("my-float-flag", 0.0, context)
# Object flags
object_value = client.get_object_value("my-object-flag", {"default": True}, context)
For detailed evaluation results:
details = client.get_boolean_details("my-flag", False, context)
print(f"Value: {details.value}")
print(f"Reason: {details.reason}")
print(f"Variant: {details.variant}")
Error Handling
The provider handles various error conditions:
- Uninitialized provider: Returns
PROVIDER_NOT_READYerror - Missing targeting key: Returns
TARGETING_KEY_MISSINGerror - Type conversion errors: Returns
TYPE_MISMATCHerror - General exceptions: Returns
GENERALerror with message
Resource Cleanup
Always clean up resources when done:
# Async cleanup
await provider.close()
# Sync cleanup
import asyncio
asyncio.run(provider.close())
Examples
See the examples directory for more usage examples.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgements
- OpenFeature - For the feature flag standard
- GrowthBook - For the feature flagging and experimentation platform
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file growthbook_openfeature_provider-0.0.5.tar.gz.
File metadata
- Download URL: growthbook_openfeature_provider-0.0.5.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
920bb2de01c020d8bc6a64f942f7a7fde7140fd1fd1d33864ac794e8f58d43ed
|
|
| MD5 |
c9756535d5205750f320956b052d9e87
|
|
| BLAKE2b-256 |
b1fde83f19d73e44f42f989ad9887ce5ae0c704b49be18ca3be7e3cedbf0001f
|
File details
Details for the file growthbook_openfeature_provider-0.0.5-py3-none-any.whl.
File metadata
- Download URL: growthbook_openfeature_provider-0.0.5-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae597df86e733536f7fe13a45b55786bd332efc4e0325fd2ba59fd9e2dd94b67
|
|
| MD5 |
ca5c57bb8db40930363f43c92d11d47a
|
|
| BLAKE2b-256 |
56c128580230ee03adf8811933787c53a605ee53efa36cd8054b29745da24140
|