Ship the Installer, Not the Paste: Deploying Open WebUI Filter Functions from CI
LLM SecurityOpen WebUIPlatform EngineeringThe design of our Qwen3Guard guardrail for Open WebUI stands or falls with three measured findings — jailbreaks that only rate "Controversial", PII that the model does not flag at all, and a 0.6 B verdict in about two-thirds of a second on CPU. That part is written up elsewhere. This is the second half of the story: how the filter, its configuration, and its updates get deployed and re-deployed without anyone ever opening the admin panel — because a paste-and-pray install is how guardrails rot.
Functions live in the database, so the database is the deployment target
Open WebUI stores filter functions as Python source in its SQLite database and loads them per request. The officially supported write path is the admin HTTP API. For a GitOps deployment that means the repo holds one file — qwen3guard_filter.py — and an installer script that converges the instance onto it. Converges, not appends: the interesting part is the state machine.
The installer compares SHA-256 hashes of the live content field against the repo file. Four outcomes:
- missing →
POST /functions/create, then activate (and since v0.13 the first function of an instance auto-activates — check, don't assume); - drift →
POST /functions/id/{id}/updatewith the full form; - inactive →
POST /functions/id/{id}/toggle(body-less); - current → touch nothing, verify, exit 0.
Valves are always set explicitly via POST /id/{id}/valves/update, so instance-level environment luck cannot silently change policy: block on Unsafe, block on Controversial + Jailbreak category, fail closed when the guard is unreachable. Then a verification pass re-reads the function and asserts type, active flag, global flag and content hash. Run it twice; the second run must be a no-op. Run it after editing the filter; it must converge.
Getting an admin token without the GUI
Deployment needed an API key before there was a human admin session. Open WebUI stores API keys in the same SQLite database (api_key table) and gates their use behind a config flag that ships off. So the bootstrap is two writes inside the running pod — no restart, no downtime, WAL mode allows concurrent writers:
con = sqlite3.connect('/app/backend/data/webui.db', timeout=10)
uid = con.execute("SELECT id FROM user WHERE role='admin'").fetchone()[0]
con.execute("INSERT INTO api_key (id,user_id,key,data,expires_at,last_used_at,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?)",
(str(uuid.uuid4()), uid, 'sk-'+secrets.token_hex(32), None, None, None, int(time.time()), int(time.time())))
con.execute("INSERT INTO config (key,value) VALUES ('auth.enable_api_keys','true') ON CONFLICT(key) DO UPDATE SET value=excluded.value")
One catch worth knowing: minimal container images strip your usual tools — no shell utils, no obvious python on PATH. We found the interpreter by enumerating /proc/*/cmdline and picking the running server's own binary. The token then lives in a Kubernetes secret; every later step (installer, check, CI) reads it from there. No key material in git, ever.
Five behaviours that cost us an hour each
These are the API quirks we hit while converging, none of them documented:
- HTTP 401 means "not found" on function routes. An unknown function id returns 401 (sometimes 404), not a clean 404. If your installer treats 401 as auth failure it will chase a bad token forever while the real problem is a missing row.
Valvesmust be a nested class. The loader checkshasattr(function_module, "Valves")on the filter instance. A module-levelValvesclass parses fine, stores fine — and is silently ignored at runtime, while/valves/*endpoints answer 401. The first version of our filter had exactly this bug: configuration looked applied and never was./valves/updatetakes a flat dict. Not{"valves": {…}}. A wrapped payload half-works in some versions and returns an empty dict in others.- Trailing-newline lies. Shell command substitution strips trailing newlines; the database keeps them. A naive
sha256(repo) == sha256(live)comparison drifts forever. Normalize both sides explicitly. - API-key auth is a config switch, not a default. With
auth.enable_api_keysunset you get403: Use of API key is not enabled in the environment— phrased, convincingly, like a permission bug.
The test that makes it durable
Three artifacts close the loop: an L2 contract test (exists / type=filter / active / global / hash matches the repo file — exit code CI can gate on), a twelve-case matrix run against the real guard model in-cluster (safe, unsafe by category, jailbreak variants, PII redaction), and unit checks for parsing and redaction that run without any network. Together they turn "the filter is probably still on" into a question with a machine answer. Six months from now, when the instance gets upgraded and the function cache goes cold, the CI check will be the one to notice — not a student filing a complaint about unfiltered content.
The whole chain — model seed, token seed, installer, L2 check, matrix — is five scripts in the deployment repo and one command on top. If you run Open WebUI for more than one person, the admin panel is a nice toy and an installer is the product.