Skip to content

: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.

fix(cdp): teach PostHog AI that transformations are top-level scripts (#55855)

Steven Shults37cd60b9fe48
diff --git a/posthog/hogql/ai.py b/posthog/hogql/ai.py
index 1f6a7762ce0a..b1a4bf5102da 100644
--- a/posthog/hogql/ai.py
+++ b/posthog/hogql/ai.py
@@ -756,6 +756,28 @@ return returnEvent"""
TRANSFORMATION_LIMITATIONS_MESSAGE = """PostHog Transformations can only modify individual incoming events. They cannot access or read person properties, historical data, or global state, because they run before person resolution. Their only purpose is to transform the structure of a single event (e.g., add properties, rename fields, enrich data) before ingestion. This means they cannot perform logic that depends on previous values, such as incrementing a count or checking if a property already exists."""
+
+TRANSFORMATION_STRUCTURE_MESSAGE = """A Hog transformation is a top-level script, NOT a wrapped function.
+
+Required contract:
+- `event` is available as an implicit global - do NOT declare parameters or wrap the top level in a function
+- Code runs once per incoming event
+- End with `return event` (or a modified copy) to keep the event, or `return null` to drop it
+- You MAY define helper functions with `fun name(args) { ... }` and call them from the top level, but the main logic must live at the top level
+
+DO NOT produce a script like this - it defines an unused function and is a no-op at runtime (this shape is for site destinations/site apps, not transformations):
+
+ fun onEvent(event) {
+ // ...
+ return event
+ }
+
+Correct structure:
+
+ let returnEvent := event
+ // ...modifications on returnEvent...
+ return returnEvent
+"""
DESTINATION_LIMITATIONS_MESSAGE = """PostHog Destinations have access to the event properties, including person properties and group properties. Just like Transformations they cannot perform logic that depends on previous values, such as incrementing a count or checking if a property already exists."""
HOG_GRAMMAR_MESSAGE = """
diff --git a/products/cdp/backend/max_tools.py b/products/cdp/backend/max_tools.py
index 9706de04e800..f691ca2d6d2d 100644
--- a/products/cdp/backend/max_tools.py
+++ b/products/cdp/backend/max_tools.py
@@ -20,6 +20,7 @@ from posthog.hogql.ai import (
INPUT_SCHEMA_TYPES_MESSAGE,
PERSON_TAXONOMY_MESSAGE,
TRANSFORMATION_LIMITATIONS_MESSAGE,
+ TRANSFORMATION_STRUCTURE_MESSAGE,
)
from posthog.hogql.parser import parse_program
@@ -59,6 +60,8 @@ class CreateHogTransformationFunctionTool(MaxTool):
context_prompt_template: str = (
HOG_TRANSFORMATION_ASSISTANT_ROOT_SYSTEM_PROMPT
+ "\n\n"
+ + TRANSFORMATION_STRUCTURE_MESSAGE
+ + "\n\n"
+ TRANSFORMATION_LIMITATIONS_MESSAGE
+ "\n\n"
+ DESTINATION_LIMITATIONS_MESSAGE
@@ -69,6 +72,9 @@ class CreateHogTransformationFunctionTool(MaxTool):
system_content = (
IDENTITY_MESSAGE_HOG
+ + "\n\n<transformation_structure>\n"
+ + TRANSFORMATION_STRUCTURE_MESSAGE
+ + "\n</transformation_structure>\n\n"
+ "\n\n<example_hog_code>\n"
+ HOG_EXAMPLE_MESSAGE
+ "\n</example_hog_code>\n\n"