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): end insights windows on yesterday so the range is not in the future (#125378)

Tom Owersf2d800d183ec
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 43c7e406a382..66fdc5dae4a4 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
@@ -189,10 +189,14 @@ def _insights_window(db_incremental_field_last_value: Optional[Any]) -> SyncWind
Incremental runs start at the watermark (the pipeline already rewinds it by the configured
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.
+ that day is moved up to it too. `until` is yesterday: the API reads a `date_range` `until` as
+ inclusive and moves it to the next midnight, and it rejects a bound in the future with a 400. So
+ `until` set to today asks for a range that ends tomorrow, and every request fails. The trailing
+ lookback re-reads the day once it is complete.
"""
- until = datetime.now(tz=UTC).date()
- earliest = until - timedelta(days=INSIGHTS_HISTORY_DAYS)
+ today = datetime.now(tz=UTC).date()
+ until = today - timedelta(days=1)
+ earliest = today - timedelta(days=INSIGHTS_HISTORY_DAYS)
since = earliest
if db_incremental_field_last_value is not None:
since = max(_to_utc_date(db_incremental_field_last_value), earliest)
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 b8d094059e0c..3a27f21e2166 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
@@ -155,6 +155,7 @@ class TestInsights:
@parameterized.expand(
[
("recent_watermark", datetime(2026, 7, 1, tzinfo=UTC), "2026-07-01"),
+ ("watermark_today", datetime(2026, 7, 21, tzinfo=UTC), "2026-07-20"),
("first_sync", None, "2025-07-22"),
("watermark_older_than_reporting_history", datetime(2024, 3, 1, tzinfo=UTC), "2025-07-22"),
]
@@ -175,7 +176,7 @@ class TestInsights:
)
assert result.raised is None
- assert result.saved_states == [OpenAIAdsResumeConfig(cursor="b1", since=expected_since, until="2026-07-21")]
+ assert result.saved_states == [OpenAIAdsResumeConfig(cursor="b1", since=expected_since, until="2026-07-20")]
@time_machine.travel("2026-07-21", tick=False)
def test_resume_reuses_the_pinned_window_with_the_saved_cursor(self) -> None: