Second Opinionfor Flagsmith

How to use

From install to deploy gate

Try the built-in fixtures first. They need no Flagsmith server and no database. Requires Python 3.10 or newer.

Quickstart

1. Install

bash
pip install flagsmith-second-opinion

Or with the optional extras (NumPy, SciPy and others):

bash
pip install "flagsmith-second-opinion[full]"

2. Run a built-in demo

bash
so analyze --demo hidden-harm

It finishes in a couple of seconds and exits with code 20. That is the correct result, not a crash.

output (excerpt)
checkout_button   environment: Development   24,000 identities
Flagsmith:        Winning, 99.7% chance to win, +13.3% lift
Second Opinion:   DON'T SHIP (Exit 20)

3. Generate an HTML report

bash
so analyze --demo hidden-harm --format html -o so-report.html

The file name so-report.html avoids overwriting the report.html page of these docs.

Demo scenarios

ScenarioPlanted problemVerdictExit
honest-winNone. A real liftSHIP0
hidden-harmMobile segment harmedDON'T SHIP20
guardrailRefund requests spikeDON'T SHIP20
noveltyDay 1 lift fadesDON'T SHIP20
srm70/30 split on a 50/50 flagDON'T SHIP20
dirty-eventsEvent name casing mismatchHOLD10
peeking-aaA/A test, no real effectHOLD10

CLI command cheat sheet

Analyze an experiment (all 7 layers)

bash
so analyze checkout_button --config second-opinion.yml
bash
so analyze --all

Gate a deploy

There is no separate so gate command. so analyze is the gate: read its exit code.

bash
so analyze checkout_button
echo "exit code: $?"

Choose an output format

text (default), json, md or html.

bash
so analyze --demo hidden-harm --format json -o verdict.json

Generate synthetic test fixtures

Writes CSV files into ./fixtures/. If that file exists, --demo reads it in place of its cached copy.

bash
so sim run hidden-harm
so sim all

Calibrate false positive rates

The default is 200 simulated A/A experiments. Pass 2,000 for the published figures.

bash
so calibrate --replicates 2000

Other commands

CommandWhat it does
so initWrites a starter second-opinion.yml (--force overwrites)
so inspectPrints the configured source, API URL and experiments. It does not test the connection
so report --demo hidden-harm -o so-report.htmlShortcut for HTML output
so branch-run --simulateSimulated A/B run between a main and a feature build

Exit codes

ExitVerdictMeaning
0SHIPNo veto or blocker, and safe to stop
10HOLDKeep the experiment running, or fix data quality
20DON'T SHIPAt least one veto or blocker
30INSUFFICIENT DATAFewer than two arms, or an arm below 50 identities or 5 conversions
2USAGE ERRORFor example, an unknown demo name or no feature given

Self-hosted Flagsmith integration

Known limitation in v0.1.4: live PostgreSQL runs do not detect segment harm or novelty decay.

The PostgreSQL reader does not load per-segment conversions or time windows. With the hidden-harm events loaded into PostgreSQL 16, the result was HOLD (exit 10) with “No subgroup harm detected”, while the CSV demo gives DON'T SHIP (exit 20). Do not rely on a live database run to catch these two traps.

Connect to the events table

Install the PostgreSQL driver, which neither install option above includes:

bash
pip install "psycopg[binary]"

Set the connection string as source in second-opinion.yml. Events are read from experimentation_sdkexperimentationevent.

second-opinion.yml
# second-opinion.yml
source: postgresql://so_reader:CHANGE_ME@localhost:5432/flagsmith

experiments:
  checkout_button:
    environment: Development   # must match environment_key in the events table
    primary:
      event: purchase
    guardrails:
      - event: refund_requested
        direction: down        # lower is better
        tolerance: 0.10
    segments:
      - platform
      - plan
    expected_split:
      control: 0.5
      treatment: 0.5

Keeping the URL in SO_DATABASE_URL

The v0.1.4 CLI does not read SO_DATABASE_URL by itself. To keep credentials out of the repository, write the variable into a temporary config before running:

bash
export SO_DATABASE_URL="postgresql://flagsmith:flagsmith@localhost:5432/flagsmith"

python - <<'PY'
import os, yaml
cfg = yaml.safe_load(open("second-opinion.yml"))
cfg["source"] = os.environ["SO_DATABASE_URL"]
yaml.safe_dump(cfg, open("so.local.yml", "w"))
PY

so analyze checkout_button --config so.local.yml

Flagsmith REST API

There are no --flagsmith-url or --api-key flags. The config file accepts an API URL and the name of the variable holding the key:

second-opinion.yml
flagsmith:
  api_url: http://localhost:8000
  api_key_env: FLAGSMITH_API_KEY

The package includes a read-only API client, but so analyze does not call it yet. The “Flagsmith” line in a verdict is recomputed locally from the event counts.

Read-only by design

Second Opinion's PostgreSQL reader issues SELECT queries only. It runs no migrations and writes nothing to your database. To have the database enforce that, connect with a role that can only read:

sql
CREATE ROLE so_reader LOGIN PASSWORD 'CHANGE_ME';
GRANT CONNECT ON DATABASE flagsmith TO so_reader;
GRANT USAGE ON SCHEMA public TO so_reader;
GRANT SELECT ON public.experimentation_sdkexperimentationevent TO so_reader;

CI/CD integration

Save this as .github/workflows/experiment-gate.yml. Commit second-opinion.yml without real credentials and add a repository secret named SO_DATABASE_URL.

.github/workflows/experiment-gate.yml
name: experiment-gate

on:
  pull_request:

permissions:
  contents: read

jobs:
  gate:
    runs-on: ubuntu-latest   # the runner must be able to reach your database
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install Second Opinion
        run: pip install "flagsmith-second-opinion[full]==0.1.1" "psycopg[binary]"

      - name: Add the database URL to the config
        env:
          SO_DATABASE_URL: ${{ secrets.SO_DATABASE_URL }}
        run: |
          python - <<'PY'
          import os, yaml
          cfg = yaml.safe_load(open("second-opinion.yml"))
          cfg["source"] = os.environ["SO_DATABASE_URL"]
          yaml.safe_dump(cfg, open("so.ci.yml", "w"))
          PY

      - name: Run the experiment gate
        run: |
          code=0
          so analyze checkout_button --config so.ci.yml --format md > verdict.md || code=$?
          cat verdict.md >> "$GITHUB_STEP_SUMMARY"
          case "$code" in
            0)  echo "SHIP" ;;
            10) echo "::warning::HOLD: not enough evidence yet" ;;
            20) echo "::error::DON'T SHIP"; exit 20 ;;
            *)  echo "::error::Gate could not evaluate (exit $code)"; exit "$code" ;;
          esac

To actually block merges, mark the gate job as a required status check in your branch protection rules. Exit 20 fails the job. Exit 10 passes with a warning. Errors and insufficient data also fail, so a broken gate never approves a merge.

The PostgreSQL limitation above applies in CI too.