: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(exports): teach AI subscription planner and fixer the $group_N key (#69595)
Vasco de Krijger75d0981661e9
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/prompts.py b/products/exports/backend/temporal/subscriptions/ai_subscription/prompts.pyindex c4968c023f74..fd52ab14dc23 100644--- a/products/exports/backend/temporal/subscriptions/ai_subscription/prompts.py+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/prompts.py@@ -222,6 +222,10 @@ Joined data available WITHOUT writing a JOIN (the engine joins these automaticalavailable for this project are listed in <project_context>.- Group / account properties: `group_<index>.properties.<name>` (e.g. `group_0.properties.name`).The index-to-type mapping for this project is listed in <project_context>.+ To COUNT or aggregate the groups/accounts themselves (not a property of theirs), use the raw+ group-key column `$group_<index>` (leading `$`). A bare `group_<index>` without a trailing+ `.properties.<name>` is NOT a scalar column and fails with "Field not found: group_<index>". See+ the count-distinct-accounts pattern below.- Session attributes: `session.$session_duration` (seconds), `session.$pageview_count`,`session.$channel_type`, `session.$entry_pathname`, `session.$is_bounce`, `session.$end_timestamp`.Reference these as plain columns inside a single `FROM events` SELECT — never write `JOIN` for them.@@ -238,6 +242,14 @@ Breakdown by a person property (USE the dotted path, NOT a JOIN):ORDER BY event_count DESCLIMIT 50+Count distinct groups/accounts (the account itself, via the raw `$`-prefixed key, never bare+`group_<index>`, which is only valid as `group_<index>.properties.<name>`):+ SELECT+ uniq($group_2) AS accounts,+ uniqIf($group_2, event = 'signup') AS accounts_signed_up+ FROM events+ WHERE {{date_range}}+First-EVER occurrence of an event per user, landing in the window (e.g. "users whose first ever'Dashboard created' falls in the window", broken down by a property of that first event). "First ever" needs eachuser's earliest event across ALL history, so compute it in a FROM-subquery, then filter to the@@ -335,6 +347,11 @@ rewrite MUST follow the same HogQL syntax constraints used by the planner:UNNEST, or ARRAY JOIN on subqueries.- No JOINs of any kind, including self-joins on `event`. Use conditional aggregation instead(ClickHouse rejects HogQL's null-safe join keys).+- Schema note for "Field not found: group_<index>": to count or aggregate a group/account, the raw+ group-key column is `$group_<index>` with a leading `$` (e.g. `uniq($group_2)`,+ `uniqIf($group_2, cond)`). A bare `group_<index>` is only valid as `group_<index>.properties.<name>`;+ used as a scalar it does not resolve; replace it with `$group_<index>`. The same `$`-prefixed form+ applies to person/session keys only via their documented paths, so do not add `$` elsewhere.- Time window: PRESERVE the original query's window tokens (`{{date_range}}`,`{{compare_date_range}}`, `{{window_start}}`, `{{window_end}}`) or literal `toDateTime('…')` boundsverbatim — those are the report's fixed analysis window. Do NOT introduce `now()` /diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.py b/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.pyindex ac0193ca81df..c21c31f59d58 100644--- a/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.py+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.py@@ -87,7 +87,7 @@ WINDOW_PLACEHOLDERS = ()# Bumping invalidates every frozen plan (they lazily re-plan on next delivery), so prompt/harness# improvements reach existing subscriptions instead of only new ones.-AI_QUERY_PLAN_VERSION = 1+AI_QUERY_PLAN_VERSION = 2DEFAULT_PLANNER_MODEL = "gpt-4.1"@@ -504,8 +504,9 @@ def build_context_blob(team: Team, window: ReportWindow, relevant_events: Sequengroup_labels = _group_type_labels(team)if group_labels:lines.append(- "- Group/account types (reference as group_<index>.properties.<name>, no JOIN needed): "- + ", ".join(group_labels)+ "- Group/account types (properties via group_<index>.properties.<name>; count/aggregate "+ "the account itself via the raw key $group_<index>, e.g. uniq($group_2), never bare "+ "group_<index>; no JOIN needed): " + ", ".join(group_labels))return "\n".join(lines)diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_spec_generator.py b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_spec_generator.pyindex 3297912dd161..817dd17244bb 100644--- a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_spec_generator.py+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_spec_generator.py@@ -666,7 +666,10 @@ class TestContextBlob(APIBaseTest):assert "dormant_event" in blobassert "Person properties (reference as person.properties.<name>" in blobassert "plan" in blob- assert "Group/account types (reference as group_<index>.properties.<name>" in blob+ assert "properties via group_<index>.properties.<name>" in blob+ # the raw group-key aggregation hint must be present so the planner counts accounts with+ # $group_<index> rather than a bare group_<index> that fails with "Field not found"+ assert "$group_<index>" in blobassert "group_0 = organization" in blob@parameterized.expand(