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.

feat(ai): surface property definition descriptions to Max taxonomy (#73360)

Sufi Sba98910d89be
diff --git a/ee/hogai/chat_agent/taxonomy/format.py b/ee/hogai/chat_agent/taxonomy/format.py
index b3fc609cdb06..7122ec21e2b0 100644
--- a/ee/hogai/chat_agent/taxonomy/format.py
+++ b/ee/hogai/chat_agent/taxonomy/format.py
@@ -1,4 +1,4 @@
-from collections.abc import Iterable, Sequence
+from collections.abc import Iterable, Mapping, Sequence
from typing import Optional
# nosemgrep: python.lang.security.use-defused-xml.use-defused-xml (XML generation only, no parsing - no XXE risk)
@@ -83,7 +83,18 @@ def format_properties_yaml(children: list[tuple[str, str | None, str | None]]):
return yaml.dump(result, default_flow_style=False, sort_keys=False)
-def enrich_props_with_descriptions(entity: str, props: Iterable[tuple[str, str | None]]):
+def enrich_props_with_descriptions(
+ entity: str,
+ props: Iterable[tuple[str, str | None]],
+ stored_descriptions: Mapping[str, str] | None = None,
+):
+ """Attach a description to each property.
+
+ The built-in core taxonomy wins. For custom properties it has never heard of, fall back to the
+ user-authored description stored on the property definition (passed in already sanitized). This
+ only enriches properties the caller already discovered via ClickHouse — it never sources the
+ property list itself from Postgres.
+ """
enriched_props = []
mapping = {
"session": CORE_FILTER_DEFINITIONS_BY_GROUP["session_properties"],
@@ -92,11 +103,14 @@ def enrich_props_with_descriptions(entity: str, props: Iterable[tuple[str, str |
}
# Entities other than the well-known ones are group types.
entity_definitions = mapping.get(entity, CORE_FILTER_DEFINITIONS_BY_GROUP["groups"])
+ stored_descriptions = stored_descriptions or {}
for prop_name, prop_type in props:
description = None
if entity_definition := entity_definitions.get(prop_name):
if entity_definition.get("system") or entity_definition.get("ignored_in_assistant"):
continue
description = entity_definition.get("description_llm") or entity_definition.get("description")
+ else:
+ description = stored_descriptions.get(prop_name)
enriched_props.append((prop_name, prop_type, description))
return enriched_props
diff --git a/ee/hogai/chat_agent/taxonomy/test/test_toolkit.py b/ee/hogai/chat_agent/taxonomy/test/test_toolkit.py
index b30a971e5ec1..f2c6e9216679 100644
--- a/ee/hogai/chat_agent/taxonomy/test/test_toolkit.py
+++ b/ee/hogai/chat_agent/taxonomy/test/test_toolkit.py
@@ -48,6 +48,17 @@ class TestTaxonomyAgentToolkit(BaseTest):
self.assertEqual(browser_prop[1], "String")
self.assertIsNotNone(browser_prop[2])
+ def test_enrich_props_uses_stored_description_only_for_custom_props(self):
+ # Custom properties fall back to the user-authored description; core taxonomy still wins so a
+ # stored description can never override (or spoof) a built-in one.
+ props = [("$browser", "String"), ("custom_prop", "Numeric")]
+ stored = {"custom_prop": "Revenue in cents", "$browser": "should be ignored"}
+ enriched = self.toolkit._enrich_props_with_descriptions("event", props, stored)
+ by_name = {name: description for name, _, description in enriched}
+
+ self.assertEqual(by_name["custom_prop"], "Revenue in cents")
+ self.assertNotEqual(by_name["$browser"], "should be ignored")
+
@parameterized.expand(
[
([], 0, False, "The property does not have any values"),
@@ -293,6 +304,25 @@ class TestTaxonomyAgentToolkit(BaseTest):
self.assertIn("<name>visible</name>", result.result)
self.assertNotIn("<name>secret</name>", result.result)
+ async def test_person_properties_surface_stored_descriptions_sanitized(self):
+ from ee.models.property_definition import EnterprisePropertyDefinition
+
+ # An EnterprisePropertyDefinition also creates the base PropertyDefinition row the person path
+ # discovers, so its user-authored description should ride along on the surfaced property.
+ await EnterprisePropertyDefinition.objects.acreate(
+ team=self.team,
+ type=PropertyDefinition.Type.PERSON,
+ name="plan_tier",
+ property_type="String",
+ description="Subscription tier\nof the account",
+ )
+ task = AssistantToolCall(id="1", name="retrieve_entity_properties", args={"entity": "person"})
+ result = await self.toolkit._handle_entity_properties_task({"task": task})
+
+ self.assertIn("<name>plan_tier</name>", result.result)
+ # Sanitization collapses the newline so a description can't break out of its line.
+ self.assertIn("<description>Subscription tier of the account</description>", result.result)
+
@patch("ee.hogai.chat_agent.taxonomy.toolkit.restricted_property_names")
async def test_retrieve_multiple_entity_property_values_hides_restricted(self, mock_restricted):
mock_restricted.return_value = {"secret"}
diff --git a/ee/hogai/chat_agent/taxonomy/toolkit.py b/ee/hogai/chat_agent/taxonomy/toolkit.py
index 13834816d1a9..0c67f0d583eb 100644
--- a/ee/hogai/chat_agent/taxonomy/toolkit.py
+++ b/ee/hogai/chat_agent/taxonomy/toolkit.py
@@ -29,6 +29,7 @@ from posthog.hogql_queries.ai.actors_property_taxonomy_query_runner import Actor
from posthog.hogql_queries.ai.event_taxonomy_query_runner import EventTaxonomyQueryRunner
from posthog.hogql_queries.query_runner import ExecutionMode
from posthog.models import Team, User
+from posthog.settings import EE_AVAILABLE
from posthog.sync import database_sync_to_async
from posthog.taxonomy.property_access import restricted_property_names
from posthog.taxonomy.taxonomy import CORE_FILTER_DEFINITIONS_BY_GROUP, CoreFilterDefinition
@@ -53,6 +54,7 @@ from ee.hogai.chat_agent.taxonomy.virtual_properties import (
virtual_group_for_entity,
virtual_property_no_values_message,
)
+from ee.hogai.utils.helpers import sanitize_event_description
from ee.hogai.utils.types.base import AssistantState, BaseStateWithTasks, TaskArtifact, TaskResult
from ..parallel_task_execution.nodes import BaseTaskExecutorNode, TaskExecutionInputTuple
@@ -200,8 +202,45 @@ class TaxonomyAgentToolkit:
def _get_team_group_types(self) -> list[str]:
return self._team_group_types
- def _enrich_props_with_descriptions(self, entity: str, props: Iterable[tuple[str, str | None]]):
- return enrich_props_with_descriptions(entity, props)
+ def _enrich_props_with_descriptions(
+ self,
+ entity: str,
+ props: Iterable[tuple[str, str | None]],
+ stored_descriptions: dict[str, str] | None = None,
+ ):
+ return enrich_props_with_descriptions(entity, props, stored_descriptions)
+
+ async def _get_stored_property_descriptions(
+ self,
+ property_type: PropertyDefinition.Type,
+ names: list[str],
+ group_type_index: int | None = None,
+ ) -> dict[str, str]:
+ """Map property name -> sanitized user-authored description from the team's property definitions.
+
+ Only the enterprise `PropertyDefinition` model carries a `description` field, so this is a
+ no-op on non-EE builds. Descriptions live only in Postgres and never influence which
+ properties are surfaced — the list still comes from ClickHouse. Runs a single batched query.
+ """
+ if not EE_AVAILABLE or not names:
+ return {}
+
+ from ee.models.property_definition import (
+ EnterprisePropertyDefinition, # noqa: PLC0415 — EE-only model, keep off the OSS import path
+ )
+
+ qs = (
+ EnterprisePropertyDefinition.objects.filter(team=self._team, type=property_type, name__in=names)
+ .exclude(description__isnull=True)
+ .exclude(description="")
+ )
+ if group_type_index is not None:
+ qs = qs.filter(group_type_index=group_type_index)
+ return {
+ name: sanitize_event_description(description)
+ async for name, description in qs.values_list("name", "description")
+ if description
+ }
def _format_property_values(
self,
@@ -529,7 +568,12 @@ class TaxonomyAgentToolkit:
status=TaskExecutionStatus.FAILED,
)
- formatted_properties = self._format_properties(self._enrich_props_with_descriptions("event", props))
+ stored_descriptions = await self._get_stored_property_descriptions(
+ PropertyDefinition.Type.EVENT, [name for name, _ in props]
+ )
+ formatted_properties = self._format_properties(
+ self._enrich_props_with_descriptions("event", props, stored_descriptions)
+ )
return TaskResult(
id=task.id,
result=formatted_properties,
@@ -580,8 +624,15 @@ class TaxonomyAgentToolkit:
properties += list_virtual_properties(
"groups", exclude={name for name, _ in properties} | restricted
)
+ stored_descriptions = await self._get_stored_property_descriptions(
+ PropertyDefinition.Type.GROUP,
+ [name for name, _ in properties],
+ group_type_index=group_index,
+ )
result = (
- self._format_properties(self._enrich_props_with_descriptions(entity, properties))
+ self._format_properties(
+ self._enrich_props_with_descriptions(entity, properties, stored_descriptions)
+ )
if properties
else TaxonomyErrorMessages.properties_not_found(entity)
)
@@ -613,7 +664,12 @@ class TaxonomyAgentToolkit:
"person_properties", exclude={name for name, _ in person_definitions} | restricted
)
if person_definitions:
- result = self._format_properties(self._enrich_props_with_descriptions("person", person_definitions))
+ stored_descriptions = await self._get_stored_property_descriptions(
+ PropertyDefinition.Type.PERSON, [name for name, _ in person_definitions]
+ )
+ result = self._format_properties(
+ self._enrich_props_with_descriptions("person", person_definitions, stored_descriptions)
+ )
status = TaskExecutionStatus.COMPLETED
else:
result = TaxonomyErrorMessages.properties_not_found(entity)