: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(openai-ads): remove the changing plan segment from insights row ids (#111223)
posthog[bot]377015e25d1f
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/openai_ads/openai_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/openai_ads/openai_ads.pyindex fca59b965c78..604e0300da8a 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/openai_ads/openai_ads.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/openai_ads/openai_ads.py@@ -179,6 +179,19 @@ def _convert_insights_times(row: dict[str, Any]) -> dict[str, Any]:return row+# The insights row id can carry a `plan=<compressed query plan>` segment next to the+# `start=`, `end=` and `entity_id=` segments. The plan value changes between requests for the same+# bucket, so the merge on `id` would insert every re-read lookback row as a new row.+_INSIGHTS_ID_PLAN_SEGMENT = re.compile(r"(?:^|:)plan=[^:]*")+++def _stable_insights_id(row: dict[str, Any]) -> dict[str, Any]:+ row_id = row.get("id")+ if not isinstance(row_id, str):+ return row+ return {**row, "id": _INSIGHTS_ID_PLAN_SEGMENT.sub("", row_id).lstrip(":")}++def _make_client(api_key: str) -> RESTClient:return RESTClient(base_url=OPENAI_ADS_BASE_URL,@@ -290,7 +303,7 @@ def openai_ads_source(currency = _account_currency(api_key)def enrich_insights(row: dict[str, Any]) -> dict[str, Any]:- return {**_convert_insights_times(row), "currency_code": currency}+ return {**_convert_insights_times(_stable_insights_id(row)), "currency_code": currency}data_map = enrich_insightselse:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/openai_ads/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/openai_ads/settings.pyindex 350aedef1a32..7ddb4d80f477 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/openai_ads/settings.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/openai_ads/settings.py@@ -67,7 +67,8 @@ def _insights_endpoint(name: str, level: str, metadata: tuple[str, ...]) -> Openpath="/v1/ad_account/insights",# The API's composite bucket id ("start=<unix>:end=<unix>:entity_id=<id>") is unique and# stable per (bucket, entity) as long as no sort[]/segments[] params are sent (those- # append suffixes to the id).+ # append suffixes to the id). The id can also carry a `plan=` segment that changes between+ # requests, so the source removes that segment before the merge.primary_keys=["id"],partition_key="start_time",incremental_fields=[_datetime_incremental_field("start_time")],diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/openai_ads/tests/test_openai_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/openai_ads/tests/test_openai_ads.pyindex abc03c8f15bb..9c81b69ecb41 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/openai_ads/tests/test_openai_ads.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/openai_ads/tests/test_openai_ads.py@@ -271,6 +271,35 @@ class TestInsights:assert rows[0]["currency_code"] == "EUR"assert session.send.call_count == 2+ @parameterized.expand(+ [+ ("plan_in_middle", "start=1777075200:{plan}:end=1777161600:entity_id=cmpn_1"),+ ("plan_last", "start=1777075200:end=1777161600:entity_id=cmpn_1:{plan}"),+ ("plan_first", "{plan}:start=1777075200:end=1777161600:entity_id=cmpn_1"),+ ("no_plan", "start=1777075200:end=1777161600:entity_id=cmpn_1"),+ ]+ )+ @mock.patch(CLIENT_SESSION_PATCH)+ def test_row_id_stays_the_same_across_syncs_when_plan_segment_changes(+ self, _name: str, id_template: str, MockSession+ ) -> None:+ ids = []+ for plan in ("plan=eJzLSM3JyVcozy/KSQEAGgQEXQ", "plan=eJwrSS0uAQAEXQHB"):+ session = MockSession.return_value+ _wire(+ session,+ [+ _response({"currency_code": "EUR"}),+ _page(+ [{"id": id_template.format(plan=plan), "start_time": 1777075200, "end_time": 1777161600}],+ has_more=False,+ ),+ ],+ )+ ids.append(_rows(_source("campaign_insights", _make_manager()))[0]["id"])++ assert ids == ["start=1777075200:end=1777161600:entity_id=cmpn_1"] * 2+@time_machine.travel("2026-07-21", tick=False)@mock.patch(CLIENT_SESSION_PATCH)def test_checkpoint_pins_the_window_alongside_the_cursor(self, MockSession) -> None: