:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(workflows): name the source template for webhook/manual/pixel triggers (#73703)
posthog[bot]b292786aeccc
diff --git a/products/workflows/backend/api/hog_flow.py b/products/workflows/backend/api/hog_flow.pyindex e18fabd6a2a2..7f019acb0adf 100644--- a/products/workflows/backend/api/hog_flow.py+++ b/products/workflows/backend/api/hog_flow.py@@ -5,7 +5,7 @@ import hashlibimport dataclassesfrom copy import deepcopyfrom datetime import timedelta-from typing import Any, Optional, cast+from typing import Any, NamedTuple, Optional, castfrom django.conf import settingsfrom django.core.cache import cache@@ -462,6 +462,28 @@ _FIXED_TEMPLATE_IDS = {}+class _TriggerSourceTemplate(NamedTuple):+ template_id: str+ event: str+ distinct_id: str+++# Non-event trigger types are each backed by one fixed built-in "source" template. These live outside+# the destination template catalog (cdp-function-templates-list is type=destination), so callers told to+# "discover the template like a function node" never find them and loop on a bare "Template not found".+# Name the exact literal instead, along with the event/distinct_id inputs that template needs: the+# request reaching it differs per trigger type, and a webhook-shaped mapping on the other two saves+# fine and only fails once triggered (manual 400s every run; a pixel silently drops the hit and still+# returns its 200 GIF), which just moves the authoring loop one step later.+_FIXED_TRIGGER_TEMPLATES = {+ "webhook": _TriggerSourceTemplate("template-source-webhook", "{request.body.event}", "{request.body.distinct_id}"),+ "manual": _TriggerSourceTemplate("template-source-webhook", "$workflow_triggered", "{request.body.user_id}"),+ "tracking_pixel": _TriggerSourceTemplate(+ "template-source-webhook-pixel", "{request.query.ph_event}", "{request.query.ph_distinct_id}"+ ),+}++def _looks_like_uuid(value: str) -> bool:try:uuid_mod.UUID(value)@@ -600,6 +622,19 @@ def _apply_email_template_content(config: dict, team: Team, strict: bool, contexdef _describe_unknown_template(action: dict, template_id: str) -> str:+ if action.get("type") == "trigger":+ trigger_type = (action.get("config") or {}).get("type", "")+ source = _FIXED_TRIGGER_TEMPLATES.get(trigger_type)+ if source:+ return (+ f"Template not found. A '{trigger_type}' trigger uses the built-in source template "+ f"'{source.template_id}' - set config.template_id to that exact literal. It is NOT in the "+ "destination template catalog, so don't look it up there. This trigger type also needs "+ f"config.inputs.event = {{value: '{source.event}'}} and "+ f"config.inputs.distinct_id = {{value: '{source.distinct_id}'}} - these values are specific "+ f"to a '{trigger_type}' trigger, so don't copy another type's."+ )+fixed_id = _FIXED_TEMPLATE_IDS.get(action.get("type", ""))if fixed_id and _looks_like_uuid(template_id):return (diff --git a/products/workflows/backend/api/test/test_hog_flow.py b/products/workflows/backend/api/test/test_hog_flow.pyindex a378146d7f74..4ecab461ff4c 100644--- a/products/workflows/backend/api/test/test_hog_flow.py+++ b/products/workflows/backend/api/test/test_hog_flow.py@@ -404,6 +404,67 @@ class TestHogFlowAPI(APIBaseTest):assert response.status_code == 400, response.json()assert "Ambiguous" in response.json()["detail"], response.json()+ @parameterized.expand(+ [+ (+ "webhook",+ "template-source-webhook",+ "{request.body.event}",+ "{request.body.distinct_id}",+ ["$workflow_triggered", "request.query"],+ ),+ (+ "manual",+ "template-source-webhook",+ "$workflow_triggered",+ "{request.body.user_id}",+ ["{request.body.event}", "request.query"],+ ),+ (+ "tracking_pixel",+ "template-source-webhook-pixel",+ "{request.query.ph_event}",+ "{request.query.ph_distinct_id}",+ ["request.body"],+ ),+ ]+ )+ def test_unknown_trigger_template_id_names_the_source_template(+ self, trigger_type, expected_literal, expected_event, expected_distinct_id, forbidden_fragments+ ):+ # webhook/manual/tracking_pixel triggers are each backed by a fixed built-in source template that+ # isn't in the destination catalog agents search, so a wrong/guessed template_id left them looping+ # on a bare "Template not found". The error must name the exact source literal and say it isn't in+ # the destination catalog. The event/distinct_id hints have to be per-type too: the request that+ # reaches the template differs, so a webhook-shaped mapping saves fine and then 400s on every+ # manual run, or silently drops every pixel hit.+ hog_flow = {+ "name": "Test Flow",+ "actions": [+ {+ "id": "trigger_node",+ "name": "trigger_1",+ "type": "trigger",+ "config": {"type": trigger_type, "template_id": "not-a-real-template", "inputs": {}},+ },+ {"id": "exit_node", "name": "exit_1", "type": "exit", "config": {}},+ ],+ "edges": [{"from": "trigger_node", "to": "exit_node", "type": "continue"}],+ }++ response = self.client.post(f"/api/projects/{self.team.id}/hog_flows", hog_flow, HTTP_X_POSTHOG_CLIENT="mcp")++ assert response.status_code == 400, response.json()+ detail = response.json()["detail"]+ assert expected_literal in detail, detail+ assert "destination template catalog" in detail, detail+ assert expected_event in detail, detail+ assert expected_distinct_id in detail, detail+ # Another trigger type's mapping leaking in is the actual regression: it saves fine and only+ # fails once triggered, so the positive assertions alone would still pass a shared example.+ for fragment in forbidden_fragments:+ assert fragment not in detail, (fragment, detail)+def test_stale_update_is_rejected_with_409(self):flow_id = self._create_simple_flow()flow = HogFlow.objects.get(pk=flow_id)diff --git a/products/workflows/skills/building-workflows/references/graph-schema.md b/products/workflows/skills/building-workflows/references/graph-schema.mdindex a969f06f8cb3..b77f16febf74 100644--- a/products/workflows/skills/building-workflows/references/graph-schema.md+++ b/products/workflows/skills/building-workflows/references/graph-schema.md@@ -66,7 +66,10 @@ Use **only** these `type` values — they are the complete supported set. An unkDiscriminated on `config.type`:- `event` — `{ "type": "event", "filters": { "events": [{ "id": "<event>", "name": "<event>", "type": "events", "order": 0, "properties": [<cond>] }], "properties": [<cond>], "filter_test_accounts": false } }`. Fires on **every** matching occurrence. Throttle repeats with `trigger_masking` (dedup/sampling — not behavioral filtering).-- `webhook` / `manual` / `tracking_pixel` — `{ "type": "webhook", "template_id": "<id>", "inputs": { ... } }`. Function-style triggers, so `template_id` is **required** and is a fixed literal: `template-source-webhook` for both `webhook` and `manual`, `template-source-webhook-pixel` for `tracking_pixel`. Omitting it fails the create with `Template not found` against the trigger node. Discover the inputs the same way as `function` nodes (see "Discovering function templates").+- `webhook` / `manual` / `tracking_pixel` — `{ "type": "webhook", "template_id": "<literal>", "inputs": { ... } }`. These use a **fixed built-in source template**, not one you look up: `template-source-webhook` for both `webhook` and `manual`, `template-source-webhook-pixel` for `tracking_pixel`. Omitting or guessing it fails the create with `Template not found` against the trigger node. They are `source_webhook` templates and are **not** in the destination catalog (`cdp-function-templates-list` is `type=destination`), so don't try to discover them there. Each needs `inputs.event` and `inputs.distinct_id` wrapped in `{value: ...}`, and the right values **differ per trigger type** because a different request reaches the template:+ - `webhook` — a POST whose body you control: `"inputs": { "event": { "value": "{request.body.event}" }, "distinct_id": { "value": "{request.body.distinct_id}" } }`.+ - `manual` — the "trigger manually" button POSTs `{user_id, $variables}`, so the body carries no `event`: use the fixed literal `"inputs": { "event": { "value": "$workflow_triggered" }, "distinct_id": { "value": "{request.body.user_id}" } }`. Pointing `event` at `{request.body.event}` here makes every manual run return 400 (the source template rejects an empty `event`) while the UI still shows a success toast, so the workflow silently never fires.+ - `tracking_pixel` — a bodyless `GET`, so read query params: `"inputs": { "event": { "value": "{request.query.ph_event}" }, "distinct_id": { "value": "{request.query.ph_distinct_id}" } }`. Body references resolve empty here and the pixel still returns its 200 GIF, so every hit is dropped with no error to retry on.- `batch` — `{ "type": "batch", "filters": { "properties": [<cond>] } }`. The audience: person-property conditions and/or cohort references. **No event/action filters** (silently dropped, so rejected). Does not fire on enable — dispatch a one-off broadcast with `workflows-run-batch`, or make it **recurring** with `workflows-schedule-create` (attaches an RRULE schedule; each firing re-broadcasts to this same `config.filters.properties` audience). A recurring workflow is a `batch` trigger plus a schedule — there is no separate "schedule" trigger type to author.### Trigger masking (throttling an event trigger)@@ -175,6 +178,8 @@ Must match `^\d*\.?\d+[dhm]$` — a number plus unit `m` | `h` | `d`. Examples:- [ ] Every action `type` and `config` matches a row above (no types outside the supported set).- [ ] `on_error` is only `continue` or `abort`.- [ ] `function_email.template_id == "template-email"`, `function_sms.template_id == "template-twilio"`, `function_push.template_id == "template-native-push"`.+- [ ] A `webhook` / `manual` trigger sets `template_id == "template-source-webhook"` (a `tracking_pixel` uses `"template-source-webhook-pixel"`) — these are built-in source templates, not catalog lookups.+- [ ] That trigger's `inputs.event` / `inputs.distinct_id` match its own type: `{request.body.*}` for `webhook`, `$workflow_triggered` + `{request.body.user_id}` for `manual`, `{request.query.ph_*}` for `tracking_pixel`. The wrong pair saves fine and then fails at trigger time.- [ ] Every non-exit node has an outgoing edge; `branch` edges have an `index` matching a condition.- [ ] Every `conditional_branch` / `wait_until_condition` condition is wrapped: `{filters: {properties: [...]}}`, not `{properties: [...]}`.- [ ] All durations match `^\d*\.?\d+[dhm]$` and dodge the silent per-unit clamp.diff --git a/products/workflows/skills/building-workflows/references/lifecycle-and-debugging.md b/products/workflows/skills/building-workflows/references/lifecycle-and-debugging.mdindex 998013c4384f..7f1019e46eff 100644--- a/products/workflows/skills/building-workflows/references/lifecycle-and-debugging.md+++ b/products/workflows/skills/building-workflows/references/lifecycle-and-debugging.md@@ -37,10 +37,11 @@ The MCP tools for the workflows product, grouped by job. The lifecycle that stri- `workflows-list-invocations` — per-recipient outcomes (one per person/event): status, error_kind/error_message, distinct_id, person_id, timings. Filter `status=failed`.- `workflows-get-invocation` — a single invocation incl. `invocation_globals` (the raw triggering payload that ran). The broad→narrow drill-down (global-stats → stats → invocations → get-invocation → logs) is in [SKILL.md](../SKILL.md).-**Discover function templates** (for `function` nodes and webhook/manual/tracking_pixel triggers)+**Discover function templates** (for `function` nodes)- `cdp-function-templates-list` — the live catalog of function templates (filter `type=destination`). Source of truth for which integrations exist; don't hardcode template ids.- `cdp-function-templates-retrieve` — one template's full detail including its `inputs_schema`. Read this before building a `function` node's `inputs`.+- Don't come here for `webhook` / `manual` / `tracking_pixel` triggers or `function_email` / `function_sms` / `function_push` steps — those take fixed literal template ids that this catalog never lists. See [graph-schema.md](graph-schema.md).**Email templates** (compose and edit with the `designing-email-templates` skill)