Format run webhook plus polling: one idempotent finalize in SQLite
A webhook and a poll can both see the same terminal run. Insert the run id with insert or ignore, and do the work only when a row was added.

Run both a webhook and a poll for every Format run, and send both to the same function that finalizes by run id. A SQLite insert or ignore on the id makes the second arrival a no-op, so the work happens once.
Webhooks deliver the terminal event at least once and can arrive late or twice, and polling can catch a run the webhook never delivered.
Which path carries what
From the Run webhooks page (read 2026-10-03).
| Path | What you get | Gap |
|---|---|---|
| Webhook format.run.terminal | Envelope with request_id equal to the run id, status, outcome and payload | payload is null when over 1 MiB |
| Poll GET /v1/format-runs/{run_id} | The receipt | Only as often as you ask, within the read budget |
| GET /v1/format-runs/{run_id}/result | The output | 409 run_not_completed before the run ends |
The finalizer
The function takes a run id and an optional payload. When the webhook payload is null, it fetches the result from the API instead.
import sqlite3
db = sqlite3.connect("done.db")
db.execute("create table if not exists done(id text primary key)")
def finalize(run_id, payload=None, fetch=lambda i: {"fetched": i}):
cur = db.execute("insert or ignore into done values(?)", (run_id,))
db.commit()
if cur.rowcount == 0:
return "duplicate"
data = payload if payload is not None else fetch(run_id)
print("deliver", run_id, data)
return "done"
print(finalize("run_1", {"ok": True}))
print(finalize("run_1"))Failure ordering
If the delivery step can fail, insert the id after it succeeds, or delete the row on failure, since a row that is written first would mark a failed delivery as finished. Verify the signature before this function runs, as in the Flask receiver, and see dedupe on request_id for ordering.
Sources
Related posts
More in Formats
- Fruits Drama Format: talking produce for a Thanksgiving push
Call Sume's fruits-drama Format for about 5 second vertical clips where a fruit character speaks your line, queued as five runs for the produce aisle.
- Green-screen Format reaction clip, revised with previous_run_id
Call Sume's green-screen Format for a talking-head overlay ad, then fix one thing with previous_run_id instead of paying for a full re-run.
- Magazine cover and model portrait Formats for beauty sellers
Two Sume Formats make 9:16 beauty images from a packshot: a magazine-cover campaign and a model product portrait. When each helps and what to verify.
- Mobile-app UGC Format: holiday app ads vs App Store previews
Use Sume's mobile-app UGC Format for creator-style app ads this holiday season, and check Apple's 15 to 30 second App Preview limits before reusing a clip.
Written by Sume