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(subscriptions): normalize escaped prompt newlines (#100770)

Vasco de Krijgere766bf017eac
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.py b/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.py
index aacade68f7f1..de00b4293dd5 100644
--- a/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.py
+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.py
@@ -77,6 +77,7 @@ MAX_PINNED_EVENTS = 25
# Tokens the user quoted in the prompt to name a specific event: `event name`, "event name",
# or 'event name'. The capture groups are non-greedy so adjacent quotes don't merge into one token.
_QUOTED_TOKEN_RE = re.compile(r"`([^`]+)`|\"([^\"]+)\"|'([^']+)'")
+_ESCAPED_NEWLINE_LINE_RE = re.compile(r"(?:\\n|\\r\\n)+")
# Placeholder tokens the planner writes instead of concrete dates, so frozen HogQL stays
# window-agnostic; ReportWindow.render_window_filter substitutes the run's fresh bounds.
@@ -291,6 +292,9 @@ def sanitize_prompt(raw: str | None) -> str:
raise PromptRejectedError(f"Prompt exceeds {PROMPT_MAX_LENGTH} characters.")
cleaned = sanitize_user_text(raw, max_len=PROMPT_MAX_LENGTH, preserve_newlines=True)
+ cleaned = "\n".join(
+ "" if _ESCAPED_NEWLINE_LINE_RE.fullmatch(line) else line for line in cleaned.split("\n")
+ ).strip()
if not cleaned:
raise PromptRejectedError("Prompt is empty.")
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.py
index ac89425a1f55..287f874b43eb 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
@@ -79,9 +79,13 @@ class TestSanitizePrompt:
("First section\n\nSecond section", "First section\n\nSecond section"),
("First section\r\n\r\nSecond section", "First section\n\nSecond section"),
("First section\u2028Second section", "First section\nSecond section"),
+ ("First section\n\\n\n\\n\nSecond section", "First section\n\n\nSecond section"),
+ ("First section\n\\r\\n\nSecond section", "First section\n\nSecond section"),
+ ("First section\n\\n\\r\\n\\n\nSecond section", "First section\n\nSecond section"),
+ ("Revenue: \\nTotal", "Revenue: \\nTotal"),
],
)
- def test_preserves_prompt_line_breaks(self, raw: str, expected: str) -> None:
+ def test_normalizes_prompt_line_breaks(self, raw: str, expected: str) -> None:
assert sanitize_prompt(raw) == expected
def test_preserves_line_breaks_while_stripping_prompt_framing_and_html_tags(self) -> None: