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): handle string trace_timestamp in trace clustering (#89321)

posthog[bot]c8749136096d
diff --git a/posthog/temporal/ai_observability/trace_clustering/data.py b/posthog/temporal/ai_observability/trace_clustering/data.py
index b14ec6407990..3cb37576fbcd 100644
--- a/posthog/temporal/ai_observability/trace_clustering/data.py
+++ b/posthog/temporal/ai_observability/trace_clustering/data.py
@@ -176,6 +176,25 @@ def fetch_item_embeddings_for_clustering(
return item_ids, embeddings_map, batch_run_ids_map
+def _as_iso_timestamp(value: object) -> str:
+ """Return a ``trace_timestamp`` property value as an ISO 8601 string.
+
+ HogQL gives a datetime only when the team's property definition for the property is typed
+ DateTime. If it is not, HogQL gives the raw string, which the summarizer writes with a space
+ separator. Consumers read this value as ISO 8601, so make both shapes the same. A value that
+ does not parse passes through unchanged.
+ """
+ if isinstance(value, datetime):
+ return value.isoformat()
+ if not value:
+ return ""
+ raw = str(value)
+ try:
+ return datetime.fromisoformat(raw).isoformat()
+ except ValueError:
+ return raw
+
+
def fetch_item_summaries(
team: Team,
item_ids: list[ItemId],
@@ -269,10 +288,6 @@ def fetch_item_summaries(
skipped_wrong_batch += 1
continue
- # HogQL parses timestamp strings into datetime objects
- trace_ts = row[5]
- trace_ts_str = trace_ts.isoformat() if trace_ts else ""
-
# For trace-level, trace_id is the same as item_id (fallback ok)
# For generation-level, trace_id must come from $ai_trace_id property (no fallback)
if analysis_level == "generation":
@@ -285,7 +300,7 @@ def fetch_item_summaries(
"flow_diagram": row[2],
"bullets": row[3],
"interesting_notes": row[4],
- "trace_timestamp": trace_ts_str,
+ "trace_timestamp": _as_iso_timestamp(row[5]),
"trace_id": trace_id,
}
diff --git a/posthog/temporal/ai_observability/trace_clustering/tests/test_data.py b/posthog/temporal/ai_observability/trace_clustering/tests/test_data.py
index 4fcd485a3144..0bcf33de1b41 100644
--- a/posthog/temporal/ai_observability/trace_clustering/tests/test_data.py
+++ b/posthog/temporal/ai_observability/trace_clustering/tests/test_data.py
@@ -311,12 +311,22 @@ class TestFetchItemSummaries:
assert summaries == {}
+ @pytest.mark.parametrize(
+ "trace_ts,expected",
+ [
+ (datetime(2025, 1, 5, 10, 30, 45), datetime(2025, 1, 5, 10, 30, 45).isoformat()),
+ ("2025-01-05T10:30:45", "2025-01-05T10:30:45"),
+ # The summarizer writes the property with str(datetime), so a space separates the date.
+ ("2025-01-05 10:30:45+00:00", "2025-01-05T10:30:45+00:00"),
+ ("not a timestamp", "not a timestamp"),
+ (None, ""),
+ ],
+ )
@patch("posthog.temporal.ai_observability.trace_clustering.data.execute_hogql_query")
- def test_extracts_timestamp_as_iso_string(self, mock_execute, mock_team):
- test_timestamp = datetime(2025, 1, 5, 10, 30, 45)
+ def test_converts_timestamp_to_string(self, mock_execute, mock_team, trace_ts, expected):
mock_result = MagicMock()
mock_result.results = [
- ("trace_1", "Title", "Flow", "Bullets", "Notes", test_timestamp, "batch_123", "trace_1"),
+ ("trace_1", "Title", "Flow", "Bullets", "Notes", trace_ts, "batch_123", "trace_1"),
]
mock_result.clickhouse = "SELECT ..."
mock_execute.return_value = mock_result
@@ -329,4 +339,4 @@ class TestFetchItemSummaries:
window_end=datetime(2025, 1, 8, tzinfo=UTC),
)
- assert summaries["trace_1"]["trace_timestamp"] == test_timestamp.isoformat()
+ assert summaries["trace_1"]["trace_timestamp"] == expected