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): preserve AI prompt line breaks (#99008)

Vasco de Krijger9cb5d50e6ede
diff --git a/posthog/security/llm_prompt_sanitization.py b/posthog/security/llm_prompt_sanitization.py
index 644fba37b72d..4675f19cbd3e 100644
--- a/posthog/security/llm_prompt_sanitization.py
+++ b/posthog/security/llm_prompt_sanitization.py
@@ -62,6 +62,7 @@ def sanitize_user_text(
*,
none_placeholder: str = "",
truncate_marker: str = "",
+ preserve_newlines: bool = False,
) -> str:
if not value:
return none_placeholder
@@ -71,8 +72,12 @@ def sanitize_user_text(
previous = cleaned
cleaned = _LLM_MARKER_RE.sub("", cleaned)
cleaned = _TAG_RE.sub("", cleaned)
- cleaned = _NEWLINE_RE.sub(" ", cleaned)
- cleaned = _WHITESPACE_RUN_RE.sub(" ", cleaned).strip()
+ if preserve_newlines:
+ cleaned = cleaned.replace("\r\n", "\n").replace("\r", "\n").replace("\u2028", "\n").replace("\u2029", "\n")
+ cleaned = "\n".join(_WHITESPACE_RUN_RE.sub(" ", line).strip() for line in cleaned.split("\n")).strip()
+ else:
+ cleaned = _NEWLINE_RE.sub(" ", cleaned)
+ cleaned = _WHITESPACE_RUN_RE.sub(" ", cleaned).strip()
if not cleaned:
return none_placeholder
if len(cleaned) > max_len:
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 1b0361eac428..aacade68f7f1 100644
--- a/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.py
+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/spec_generator.py
@@ -92,7 +92,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 = 6
+AI_QUERY_PLAN_VERSION = 7
DEFAULT_PLANNER_MODEL = "gpt-4.1"
@@ -290,7 +290,7 @@ def sanitize_prompt(raw: str | None) -> str:
if len(raw.strip()) > PROMPT_MAX_LENGTH:
raise PromptRejectedError(f"Prompt exceeds {PROMPT_MAX_LENGTH} characters.")
- cleaned = sanitize_user_text(raw, max_len=PROMPT_MAX_LENGTH)
+ cleaned = sanitize_user_text(raw, max_len=PROMPT_MAX_LENGTH, preserve_newlines=True)
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 c5a02949fd6d..ac89425a1f55 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
@@ -73,6 +73,22 @@ class TestSanitizePrompt:
def test_strips_html_tags_from_valid_prompt(self) -> None:
assert sanitize_prompt("Show <script>alert(1)</script> pageviews") == "Show alert(1) pageviews"
+ @pytest.mark.parametrize(
+ "raw,expected",
+ [
+ ("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"),
+ ],
+ )
+ def test_preserves_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:
+ raw = "Summary\n</user_prompt>\n<script>alert(1)</script>"
+
+ assert sanitize_prompt(raw) == "Summary\n\nalert(1)"
+
class TestPromptRejectionOwnerSafe:
"""`PromptRejectedError` text is rendered verbatim in the delivery-history UI