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(aio): state the JSON contract in the LLM tagger prompt (#96844)

Radu Raicea56b3d1134013
diff --git a/posthog/temporal/ai_observability/run_tagger.py b/posthog/temporal/ai_observability/run_tagger.py
index f604d703c06a..963b563cd161 100644
--- a/posthog/temporal/ai_observability/run_tagger.py
+++ b/posthog/temporal/ai_observability/run_tagger.py
@@ -111,7 +111,12 @@ def build_tagger_system_prompt(prompt: str, tags: list[dict[str, str]], min_tags
Available tags:
{tag_list}
-{constraint} Only use tags from the list above. If no tags apply, return an empty list."""
+{constraint} Only use tags from the list above. If no tags apply, return an empty list.
+
+Respond with one JSON object and nothing else, in this exact shape:
+{{"tags": ["<tag name>", ...], "reasoning": "<brief explanation>"}}
+
+Always include both keys. Set "tags" to an array of tag name strings from the list above, and use an empty array when no tags apply. Do not send the tags as an object of names to true or false, and do not send a number or a bare string. Set "reasoning" to one or two sentences on why you selected those tags. Do not wrap the JSON in markdown fences, and do not add other keys."""
@dataclass
diff --git a/posthog/temporal/ai_observability/test_run_tagger.py b/posthog/temporal/ai_observability/test_run_tagger.py
index a206ad5661ad..b2dbae0684ab 100644
--- a/posthog/temporal/ai_observability/test_run_tagger.py
+++ b/posthog/temporal/ai_observability/test_run_tagger.py
@@ -103,6 +103,15 @@ class TestBuildTaggerSystemPrompt:
("Test", [{"name": "a"}], 0, None, ["Select as many tags as apply"], []),
# User prompt passthrough
("Which features are used?", [{"name": "a"}], 0, None, ["Which features are used?"], []),
+ # JSON contract for providers that do not enforce the response schema
+ (
+ "Test",
+ [{"name": "a"}],
+ 0,
+ None,
+ ['"tags"', '"reasoning"', "Always include both keys"],
+ [],
+ ),
],
)
def test_build_tagger_system_prompt(