: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(data-warehouse): use one description order in read_data (#129992)
Sinan Kucukkoseler9713178e6204
diff --git a/ee/hogai/tools/read_data/test/test_tool.py b/ee/hogai/tools/read_data/test/test_tool.pyindex 8639dc1dd983..d7b3345997a8 100644--- a/ee/hogai/tools/read_data/test/test_tool.py+++ b/ee/hogai/tools/read_data/test/test_tool.py@@ -770,6 +770,53 @@ class TestReadDataTool(BaseTest):assert "- amount (integer) — charge amount in cents" in resultassert "- currency (string)" in result+ async def test_table_annotation_description_wins_over_source_schema_description(self):+ credential = await DataWarehouseCredential.objects.acreate(+ access_key="test_key", access_secret="test_secret", team=self.team+ )+ source = await ExternalDataSource.objects.acreate(+ source_id="src", connection_id="conn", team=self.team, source_type="Stripe"+ )+ table = await DataWarehouseTable.objects.acreate(+ name="stripe_charges",+ format="Parquet",+ team=self.team,+ credential=credential,+ external_data_source=source,+ url_pattern="https://bucket.s3/data/*",+ columns={+ "amount": {"hogql": "IntegerDatabaseField", "clickhouse": "Nullable(Int64)", "schema_valid": True}+ },+ )+ await ExternalDataSchema.objects.acreate(+ name="stripe_charges", team=self.team, source=source, table=table, description="Source text"+ )+ with team_scope(self.team.pk, canonical=True):+ await WarehouseColumnAnnotation.objects.acreate(+ team=self.team,+ table=table,+ column_name="",+ description="Curated text",+ description_source=WarehouseColumnAnnotation.DescriptionSource.USER_EDITED,+ )++ state = AssistantState(messages=[], root_tool_call_id=str(uuid4()))+ context_manager = MagicMock()+ context_manager.check_user_has_billing_access = AsyncMock(return_value=False)+ context_manager.check_has_audit_logs_access = AsyncMock(return_value=False)++ tool = await ReadDataTool.create_tool_class(+ team=self.team, user=self.user, state=state, context_manager=context_manager+ )++ table_list, _ = await tool._arun_impl({"kind": "data_warehouse_schema"})+ table_detail, _ = await tool._arun_impl({"kind": "data_warehouse_table", "table_name": "stripe_charges"})++ assert "- stripe_charges — Curated text" in table_list+ assert "Table `stripe_charges` — Curated text with fields:" in table_detail+ assert "Source text" not in table_list+ assert "Source text" not in table_detail+async def test_table_schema_returns_view_fields(self):"""Test that data_warehouse_table returns full schema for a view."""await DataWarehouseSavedQuery.objects.acreate(diff --git a/ee/hogai/tools/read_data/tool.py b/ee/hogai/tools/read_data/tool.pyindex a62235f7de73..70a926a5343a 100644--- a/ee/hogai/tools/read_data/tool.py+++ b/ee/hogai/tools/read_data/tool.py@@ -515,21 +515,17 @@ class ReadDataTool(HogQLDatabaseMixin, MaxTool):views = database.get_view_names()system_tables = database.get_system_table_names()- semantics = self._warehouse_table_semantics(set(warehouse_tables))-listify = lambda items: "\n".join(f"- {item}" for item in sorted(items))def describe(name: str) -> str | None:- # Descriptions come from the shared resolver (raw, so sanitize here); the source-table- # description on `semantics` is already sanitized.+ # The shared resolver returns raw text, so sanitize it here.raw = descriptions.for_table(database.get_table(name))return _sanitize_semantic_text(raw) if raw else None- def listify_described(items: list[str], external: bool) -> str:+ def listify_described(items: list[str]) -> str:lines: list[str] = []for item in sorted(items):- source_desc = (semantics.get(item) or {}).get("description") if external else None- description = source_desc or describe(item)+ description = describe(item)lines.append(f"- {item} — {description}" if description else f"- {item}")return "\n".join(lines)@@ -537,18 +533,17 @@ class ReadDataTool(HogQLDatabaseMixin, MaxTool):READ_DATA_WAREHOUSE_SCHEMA_PROMPT,template_format="mustache",posthog_tables="\n".join(system_table_lines),- data_warehouse_tables=listify_described(warehouse_tables, external=True),+ data_warehouse_tables=listify_described(warehouse_tables),system_tables=listify(system_tables),- data_warehouse_views=listify_described(views, external=False),+ data_warehouse_views=listify_described(views),)def _warehouse_table_semantics(self, table_names: set[str]) -> dict[str, dict]:- """Map warehouse table name -> {description, label, foreign_keys}.+ """Map warehouse table name -> {label, foreign_keys}.- Pulls the `ExternalDataSchema` context — the source-table description/label and the- foreign-key graph — so the agent sees what the data means, not just its column types. Column- and annotation descriptions come from `TableDescriptions` instead, so this doesn't re-read- the annotation models.+ Pulls the `ExternalDataSchema` context — the source-table label and the foreign-key graph — so+ the agent sees what the data means, not just its column types. Every description comes from+ `TableDescriptions` instead, so `read_data` and `system.information_schema` show the same text.Results are memoized per tool instance (including misses, stored as `{}`) so repeatedper-table lookups within a session don't re-fire the underlying queries.@@ -584,12 +579,10 @@ class ReadDataTool(HogQLDatabaseMixin, MaxTool):result: dict[str, dict] = {}for table in tables:schema = schemas_by_table_id.get(table.id)- # Descriptions/labels are untrusted (see _sanitize_semantic_text); sanitize at this- # chokepoint so every prompt surface that renders them gets the neutralized text.- description = schema.description if schema else None+ # Labels are untrusted (see _sanitize_semantic_text); sanitize at this chokepoint so every+ # prompt surface that renders them gets the neutralized text.label = schema.label if schema else Noneresult[table.name] = {- "description": _sanitize_semantic_text(description) if description else None,"label": _sanitize_semantic_text(label) if label else None,"foreign_keys": schema.foreign_keys if schema else None,}@@ -637,13 +630,9 @@ class ReadDataTool(HogQLDatabaseMixin, MaxTool):raw_table = database.get_table(db_name)raw_fields = raw_table.fields- # Source-table description (warehouse only) wins; otherwise fall back to the resolver, which- # covers warehouse/view annotations and native schema descriptions uniformly. The resolver- # returns raw text, so sanitize; `semantics` is already sanitized.- table_description = semantics.get("description")- if not table_description:- resolved = descriptions.for_table(raw_table)- table_description = _sanitize_semantic_text(resolved) if resolved else None+ # The resolver returns raw text, so sanitize it here.+ resolved = descriptions.for_table(raw_table)+ table_description = _sanitize_semantic_text(resolved) if resolved else Noneheader = f"Table `{table_name}`"if table_description: