: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(prompts): reject references inside json prompt payloads (#105405)
Juraj Majerik611eef85e450
diff --git a/posthog/api/test/test_llm_prompt.py b/posthog/api/test/test_llm_prompt.pyindex 4e8401a9392b..993c4476dddd 100644--- a/posthog/api/test/test_llm_prompt.py+++ b/posthog/api/test/test_llm_prompt.py@@ -2392,3 +2392,26 @@ class TestLLMPromptDependenciesAPI(APIBaseTest):)assert response.status_code == status.HTTP_409_CONFLICTassert "Try again" in response.json()["detail"]++ def test_create_rejects_references_inside_json_payloads(self):+ self._make_prompt("guardrails", label="production")++ response = self.client.post(+ f"/api/environments/{self.team.id}/llm_prompts/",+ data={+ "name": "structured",+ "prompt": {+ "messages": [{"role": "system", "content": "@@@prompt:name=guardrails|label=production@@@"}]+ },+ },+ format="json",+ )+ assert response.status_code == status.HTTP_400_BAD_REQUEST+ assert response.json()["code"] == "reference_in_non_text_prompt"++ tag_free = self.client.post(+ f"/api/environments/{self.team.id}/llm_prompts/",+ data={"name": "structured", "prompt": {"messages": [{"role": "system", "content": "hi"}]}},+ format="json",+ )+ assert tag_free.status_code == status.HTTP_201_CREATEDdiff --git a/products/ai_observability/backend/prompt_references.py b/products/ai_observability/backend/prompt_references.pyindex 1a0949ba4b88..3d18baa8d9f0 100644--- a/products/ai_observability/backend/prompt_references.py+++ b/products/ai_observability/backend/prompt_references.py@@ -162,6 +162,16 @@ def validate_reference_targets(team_id: int, *, prompt_name: str, prompt_payloadif not references:return+ # Splicing at fetch time inserts raw text into whatever surrounds the tag.+ # Inside a JSON payload that corrupts the document, so references only+ # live in plain-text prompts, the same rule referenced targets follow.+ if not isinstance(prompt_payload, str):+ raise _reference_error(+ "References are only supported in plain-text prompts. Move the reference into a "+ "plain-text prompt or remove it.",+ "reference_in_non_text_prompt",+ )+# Checked here rather than only at publish so content written before the# cap existed cannot activate more references through a label.if len(references) > MAX_PROMPT_REFERENCES: