Writing

GitLab feature flags with Python and Unleash

Feature flags let you toggle functionality at runtime without redeploying. Walks through implementing GitLab feature flags using Python and the Unleash SDK.

Feature flags

Feature flags are a common part of application development. They let you switch on or off a particular feature or code path at runtime — without deploying new code.

if flag_enabled:
    DO THIS
else:
    DO THIS

Traditionally, feature flags are hosted via config servers, databases, or tools like HashiCorp Consul and Vault. Storing configs alongside code is less preferred — it forces a deploy every time a config changes.

Feature flags in GitLab

GitLab provides a built-in option to host feature flags alongside your code repository. While there are more capable products for production-scale config management, GitLab’s feature flags are especially handy in CI/CD pipelines.

Creating a feature flag in GitLab

Navigate to: Repository → Deploy → Feature Flags → New Feature Flag

When creating the flag, make sure the strategy is set to All users (or the applicable user segment), and that the correct environments are enabled. Without those settings, the flag will not be accessible from your application or script.

Accessing feature flags via Python

Installation

pip install UnleashClient

Code

from UnleashClient import UnleashClient

try:
    client = UnleashClient(
        url="https://gitlab.com/api/v4/feature_flags/unleash/PROJECT_ID",
        app_name="demo",
        instance_id="INSTANCE_ID"
    )

    client.initialize_client()

    if client.is_enabled("test"):
        print("flag enabled")
    else:
        print("flag is disabled")
except Exception as e:
    print(e)

How it works

  1. Import UnleashClient from the library.
  2. Initialize the client with your GitLab project URL, app name, and instance ID (found under Feature Flags → Configure in GitLab).
  3. Call client.is_enabled("flag-name") to check the flag state.

With the flag enabled: $ python main.pyflag enabled With the flag disabled: $ python main.pyflag is disabled

Full demo code: https://gitlab.com/dwarak_nath/demo

Conclusion

Feature flags integrated into CI/CD give you a safe way to control rollouts and avoid code rollbacks. GitLab’s built-in flag support, combined with the Unleash SDK, makes this easy to adopt without adding external infrastructure.

References