: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-imports): coerce warehouse column values to JSON-safe scalars (#83950)
Tom Owersabf9652042a2
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync.pyindex f09e2d8239cc..0dd52b9cf830 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync.py@@ -22,6 +22,9 @@ import jsonimport asyncioimport hashlibimport dataclasses+from datetime import date, datetime, time+from decimal import Decimal+from typing import Anyfrom django.conf import settings@@ -103,16 +106,36 @@ def bundle_hash(bundle: dict) -> str:return hashlib.sha256(json.dumps(bundle, sort_keys=True, default=str).encode("utf-8")).hexdigest()+def _json_safe(value: Any) -> Any:+ """Coerce a warehouse column value into something ``json.dumps`` accepts. Timestamp/date/time+ columns arrive as datetime objects and numeric columns as Decimal — neither is JSON-serializable,+ so the Kafka produce (which serializes the intent with plain ``json.dumps``) would raise. Mirrors+ ``bundle_hash``'s ``default=str`` posture: temporal types get an ISO-8601 string PostHog reads as a+ DateTime property, decimals become floats, and anything else falls back to ``str``."""+ if value is None or isinstance(value, (str, bool, int, float)):+ return value+ if isinstance(value, (datetime, date, time)):+ return value.isoformat()+ if isinstance(value, Decimal):+ return float(value)+ if isinstance(value, dict):+ return {k: _json_safe(v) for k, v in value.items()}+ if isinstance(value, (list, tuple)):+ return [_json_safe(v) for v in value]+ return str(value)++def build_bundles(rows: list[dict], key_column: str, column_property_map: dict[str, str]) -> list[tuple[str, dict]]:"""(distinct_id, {person_property: value}) for each row with a non-null key and at least onenon-null mapped value. Missing columns are simply absent (a misconfigured source degrades to- fewer properties rather than erroring)."""+ fewer properties rather than erroring). Values are coerced to JSON-safe scalars so a timestamp or+ numeric column doesn't crash the downstream Kafka produce."""bundles: list[tuple[str, dict]] = []for row in rows:key = row.get(key_column)if key is None:continue- bundle = {prop: row[col] for col, prop in column_property_map.items() if row.get(col) is not None}+ bundle = {prop: _json_safe(row[col]) for col, prop in column_property_map.items() if row.get(col) is not None}if bundle:bundles.append((str(key), bundle))return bundlesdiff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync_test.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync_test.pyindex aaf0905729e1..abc16257a4d2 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync_test.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/person_property_sync_test.py@@ -1,4 +1,6 @@-from datetime import datetime+import json+from datetime import date, datetime+from decimal import Decimalimport pytestfrom unittest.mock import AsyncMock, MagicMock, patch@@ -35,6 +37,25 @@ class TestBuildBundles:("a", {"plan_tier": "pro"})]+ def test_coerces_non_json_scalars_so_the_produce_can_serialize(self):+ # A timestamp column arrives as a datetime and a numeric column as a Decimal; the Kafka+ # producer serializes intents with plain json.dumps, so these must be coerced or it crashes.+ rows = [+ {+ "distinct_id": "a",+ "signed_up": datetime(2026, 1, 2, 3, 4, 5),+ "born": date(2026, 1, 2),+ "ltv": Decimal("9.99"),+ }+ ]+ bundles = pps.build_bundles(+ rows, "distinct_id", {"signed_up": "signed_up_at", "born": "born_on", "ltv": "lifetime_value"}+ )+ assert bundles == [+ ("a", {"signed_up_at": "2026-01-02T03:04:05", "born_on": "2026-01-02", "lifetime_value": 9.99})+ ]+ json.dumps(bundles[0][1]) # the produced bundle round-trips without a default handler+class TestBundleHash:def test_is_order_independent(self):