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(openai-ads): start insights windows within the reporting history (#123155)

Tom Owers6ac4bddcd412
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.py
index 604e0300da8a..9869aac2370e 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
@@ -2,7 +2,7 @@ import re
import json
import dataclasses
from collections.abc import Callable, Iterator
-from datetime import UTC, date, datetime
+from datetime import UTC, date, datetime, timedelta
from typing import Any, Optional
from dateutil import parser
@@ -45,10 +45,10 @@ UNREACHABLE_ERROR = (
"Couldn't reach OpenAI Ads to validate your API key. This is usually temporary, so try again in a moment."
)
-# Floor for the insights window on a full refresh. OpenAI Ads launched to advertisers in 2026, so
-# no reporting data can predate this; the whole window rides a single date_range request, so a
-# generous floor costs nothing while staying within the API's 5-year time-range bound.
-DEFAULT_INSIGHTS_SINCE = date(2025, 1, 1)
+# OpenAI Ads keeps daily delivery reporting for the most recent 365 days and rejects an earlier
+# `since` with a 400 (https://developers.openai.com/ads/reporting). The window is one day shorter
+# because the API reads dates in the ad account's timezone and this source sends them in UTC.
+INSIGHTS_HISTORY_DAYS = 364
@dataclasses.dataclass
@@ -157,13 +157,14 @@ def _insights_window(db_incremental_field_last_value: Optional[Any]) -> SyncWind
"""The [since, until] ISO date window for one insights sync.
Incremental runs start at the watermark (the pipeline already rewinds it by the configured
- lookback); full refreshes start at the product-launch floor. `until` is today — the API
- rejects future dates.
+ lookback); full refreshes start at the oldest day the API still reports. A watermark older than
+ that day is moved up to it too. `until` is today — the API rejects future dates.
"""
until = datetime.now(tz=UTC).date()
- since = DEFAULT_INSIGHTS_SINCE
+ earliest = until - timedelta(days=INSIGHTS_HISTORY_DAYS)
+ since = earliest
if db_incremental_field_last_value is not None:
- since = _to_utc_date(db_incremental_field_last_value)
+ since = max(_to_utc_date(db_incremental_field_last_value), earliest)
since = min(since, until)
return SyncWindow(start=since.isoformat(), end=until.isoformat())
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.py
index 25d43052c8ce..a1ea3d9b0db8 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
@@ -102,8 +102,17 @@ class TestInsights:
assert ids == ["start=1777075200:end=1777161600:entity_id=cmpn_1"] * 2
+ @parameterized.expand(
+ [
+ ("recent_watermark", datetime(2026, 7, 1, tzinfo=UTC), "2026-07-01"),
+ ("first_sync", None, "2025-07-22"),
+ ("watermark_older_than_reporting_history", datetime(2024, 3, 1, tzinfo=UTC), "2025-07-22"),
+ ]
+ )
@time_machine.travel("2026-07-21", tick=False)
- def test_checkpoint_pins_the_window_alongside_the_cursor(self) -> None:
+ def test_checkpoint_pins_the_window_alongside_the_cursor(
+ self, _name: str, watermark: datetime | None, expected_since: str
+ ) -> None:
result = _driver().run(
"campaign_insights",
[
@@ -111,12 +120,12 @@ class TestInsights:
_page([{"id": "b1", "start_time": 1, "end_time": 2}], has_more=True, last_id="b1"),
_page([{"id": "b2", "start_time": 2, "end_time": 3}], has_more=False, last_id="b2"),
],
- db_incremental_field_last_value=datetime(2026, 7, 1, tzinfo=UTC),
+ db_incremental_field_last_value=watermark,
should_use_incremental_field=True,
)
assert result.raised is None
- assert result.saved_states == [OpenAIAdsResumeConfig(cursor="b1", since="2026-07-01", until="2026-07-21")]
+ assert result.saved_states == [OpenAIAdsResumeConfig(cursor="b1", since=expected_since, until="2026-07-21")]
@time_machine.travel("2026-07-21", tick=False)
def test_resume_reuses_the_pinned_window_with_the_saved_cursor(self) -> None: