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
pip install flagsmith-second-opinionOr with the optional extras (NumPy, SciPy and others):
pip install "flagsmith-second-opinion[full]"2. Run a built-in demo
so analyze --demo hidden-harmIt finishes in a couple of seconds and exits with code 20. That is the correct result, not a crash.
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
so analyze --demo hidden-harm --format html -o so-report.htmlThe file name so-report.html avoids overwriting the report.html page of these docs.
Demo scenarios
| Scenario | Planted problem | Verdict | Exit |
|---|---|---|---|
honest-win | None. A real lift | SHIP | 0 |
hidden-harm | Mobile segment harmed | DON'T SHIP | 20 |
guardrail | Refund requests spike | DON'T SHIP | 20 |
novelty | Day 1 lift fades | DON'T SHIP | 20 |
srm | 70/30 split on a 50/50 flag | DON'T SHIP | 20 |
dirty-events | Event name casing mismatch | HOLD | 10 |
peeking-aa | A/A test, no real effect | HOLD | 10 |
CLI command cheat sheet
Analyze an experiment (all 7 layers)
so analyze checkout_button --config second-opinion.ymlso analyze --allGate a deploy
There is no separate so gate command. so analyze is the gate: read its exit code.
so analyze checkout_button
echo "exit code: $?"Choose an output format
text (default), json, md or html.
so analyze --demo hidden-harm --format json -o verdict.jsonGenerate synthetic test fixtures
Writes CSV files into ./fixtures/. If that file exists, --demo reads it in place of its cached copy.
so sim run hidden-harm
so sim allCalibrate false positive rates
The default is 200 simulated A/A experiments. Pass 2,000 for the published figures.
so calibrate --replicates 2000Other commands
| Command | What it does |
|---|---|
so init | Writes a starter second-opinion.yml (--force overwrites) |
so inspect | Prints the configured source, API URL and experiments. It does not test the connection |
so report --demo hidden-harm -o so-report.html | Shortcut for HTML output |
so branch-run --simulate | Simulated A/B run between a main and a feature build |
Exit codes
| Exit | Verdict | Meaning |
|---|---|---|
| 0 | SHIP | No veto or blocker, and safe to stop |
| 10 | HOLD | Keep the experiment running, or fix data quality |
| 20 | DON'T SHIP | At least one veto or blocker |
| 30 | INSUFFICIENT DATA | Fewer than two arms, or an arm below 50 identities or 5 conversions |
| 2 | USAGE ERROR | For 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:
pip install "psycopg[binary]"Set the connection string as source in second-opinion.yml. Events are read from
experimentation_sdkexperimentationevent.
# 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.5Keeping 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:
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.ymlFlagsmith 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:
flagsmith:
api_url: http://localhost:8000
api_key_env: FLAGSMITH_API_KEYThe 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:
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.
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" ;;
esacTo 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.