SDK guide

Feature flags in Python with OpenFeature

How to add feature flags to a Python service: install the provider, evaluate flags in process without a network call, use them in FastAPI, Django or Flask, and test the code behind them.

With Railflag's Python provider a flag check is a function call in your own process, about 15 µs and no network request. It plugs into the OpenFeature SDK for Python (3.10+), so the code you write is vendor-neutral.

Install and connect

The provider is one package, and its only dependency is openfeature-sdk.

pip install railflag-openfeature

Create a server key for the environment under Environments → keys in the dashboard, keep it in an environment variable, and register the provider once at startup:

import os
from openfeature import api
from railflag import RailflagProvider

api.set_provider_and_wait(RailflagProvider(
    sdk_key=os.environ["RAILFLAG_SERVER_KEY"],
    base_url="https://relay.railflag.com",
))
client = api.get_client()

At startup the provider downloads the environment's flag config once. After that it listens for changes over Server-Sent Events and picks them up within a second. If the stream drops it polls every 30 seconds, and if Railflag cannot be reached it keeps serving the last config it had, so a flag check never fails because of the network.

Evaluate a flag

Every check takes the flag key, a default, and an evaluation context: the targeting key (usually a user id) plus any attributes your rules use.

from openfeature.evaluation_context import EvaluationContext

ctx = EvaluationContext("user-42", {"plan": "pro", "country": "GR"})

if client.get_boolean_value("new-checkout", False, ctx):
    render_new_checkout()

limit = client.get_integer_value("export-row-limit", 10_000, ctx)
theme = client.get_string_value("dashboard-theme", "classic", ctx)

get_boolean_value, get_string_value, get_integer_value, get_float_value and get_object_value work as in any OpenFeature provider. If the flag is unknown or has a different type, you get your default back together with an error code, never an exception in the middle of a request.

Using it in Django, FastAPI or Flask

Create the provider once per process, not once per request. In a web app that means application startup: a FastAPI lifespan handler, Django's AppConfig.ready(), or the module that builds your Flask app. Build the context per request, from the signed-in user:

from fastapi import Depends, FastAPI

app = FastAPI()

@app.get("/checkout")
def checkout(user=Depends(current_user)):
    ctx = EvaluationContext(str(user.id), {"plan": user.plan, "country": user.country})
    if client.get_boolean_value("new-checkout", False, ctx):
        return new_checkout(user)
    return old_checkout(user)

With a pre-fork server such as gunicorn, register the provider in each worker after the fork (for example in a post_fork hook) so every worker has its own stream and in-memory config.

React to flag changes

Most code just evaluates on every request and always sees current values. If you cache a derived value, subscribe to changes instead:

from openfeature.event import ProviderEvent

def on_change(details):
    print("changed:", details.flags_changed)

api.add_handler(ProviderEvent.PROVIDER_CONFIGURATION_CHANGED, on_change)

The same answer in every language

A user is put into a rollout bucket by hashing their targeting key with the flag, and the Python evaluator is a port of the TypeScript one the other SDKs use. The test suite replays thousands of generated evaluations against it, so a user who gets the new checkout in your Python service gets it in your Node, Go and Java services too. Known differences are small: regexes in matches rules run on Python's re, and a date-time without a time zone is read as UTC.

Testing code that uses flags

Because the flag client is an ordinary OpenFeature client, tests can swap the provider. OpenFeature's InMemoryProvider holds a dictionary of flags, so a test sets exactly the values it needs and never talks to Railflag. See how to test code behind feature flags.

Keys, privacy and experiments

  • Server keys can read every rule. Keep them on the server. Browsers use the JavaScript SDK with a client key.
  • Once a minute the provider sends counts of how often each flag returned each variation, for Release Watch. It never sends contexts or user ids, runs on a background thread, and can be turned off with send_exposures=False.
  • For experiments, client.track("purchase", ctx, TrackingEventDetails(value=49.9)) records conversions. Only events that a running experiment counts are sent, and user keys are hashed before storage.
  • Server-side checks are never metered, on any plan.

Frequently asked questions

How do I use feature flags in Python?

Install an OpenFeature provider such as railflag-openfeature, register it once at startup, then call client.get_boolean_value(flag_key, default, context) wherever you need to branch. The provider holds the rules in memory, so each check takes microseconds and makes no network call.

Does a flag check make a network request in Python?

No. The Railflag provider downloads the environment config once, keeps it in memory and updates it over a streaming connection. Each check is a local function call of about 15 microseconds, and the last config keeps serving if Railflag cannot be reached.

Which Python versions does the Railflag provider support?

Python 3.10 and newer. The only dependency is the OpenFeature SDK for Python.

Can I use the same flags from Python and from other languages?

Yes. The Node, browser, Go, Java and Python SDKs run the same evaluation logic and are tested against shared vectors, so a user gets the same variation whichever service evaluates the flag.

Feature flags for your Python services

Local evaluation, live updates within a second, unlimited server-side checks on every plan.

Start for free