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(klaviyo): clamp future incremental cursor to now (#63769)

Tom Owersc9b973d6cc00
diff --git a/posthog/temporal/data_imports/sources/klaviyo/klaviyo.py b/posthog/temporal/data_imports/sources/klaviyo/klaviyo.py
index e14fec875dab..2649fff5ba80 100644
--- a/posthog/temporal/data_imports/sources/klaviyo/klaviyo.py
+++ b/posthog/temporal/data_imports/sources/klaviyo/klaviyo.py
@@ -44,6 +44,25 @@ def _format_incremental_value(value: Any) -> str:
return str(value)
+def _clamp_future_value_to_now(value: Any) -> Any:
+ """Cap a future datetime/date incremental cursor at the current time.
+
+ The incremental cursor tracks the max value seen for the endpoint's datetime field
+ (e.g. an event's customer-supplied `datetime`). If the source's data contains a
+ future-dated record, the cursor advances past now and every subsequent sync builds
+ a `greater-than(<field>,<future>)` filter that Klaviyo rejects with a 400, wedging
+ the sync. Asking for records newer than now is a no-op anyway, so capping the value
+ keeps the request valid and lets the sync self-heal.
+ """
+ now = datetime.now(UTC)
+ if isinstance(value, datetime):
+ aware_value = value if value.tzinfo is not None else value.replace(tzinfo=UTC)
+ return now if aware_value > now else value
+ if isinstance(value, date):
+ return now.date() if value > now.date() else value
+ return value
+
+
def _build_filter(
config: KlaviyoEndpointConfig,
incremental_field: str | None,
@@ -115,6 +134,10 @@ def _build_initial_params(
if should_use_incremental_field and not db_incremental_field_last_value and config.default_lookback_days:
db_incremental_field_last_value = datetime.now(UTC) - timedelta(days=config.default_lookback_days)
+ # Future-dated source data can push the cursor past now; Klaviyo 400s on a future filter value.
+ if should_use_incremental_field and db_incremental_field_last_value:
+ db_incremental_field_last_value = _clamp_future_value_to_now(db_incremental_field_last_value)
+
formatted_last_value = (
_format_incremental_value(db_incremental_field_last_value)
if should_use_incremental_field and db_incremental_field_last_value
diff --git a/posthog/temporal/data_imports/sources/klaviyo/test_klaviyo.py b/posthog/temporal/data_imports/sources/klaviyo/test_klaviyo.py
index f435c06fa8aa..47cecced204d 100644
--- a/posthog/temporal/data_imports/sources/klaviyo/test_klaviyo.py
+++ b/posthog/temporal/data_imports/sources/klaviyo/test_klaviyo.py
@@ -1,10 +1,13 @@
from datetime import UTC, date, datetime
+from freezegun import freeze_time
+
from parameterized import parameterized
from posthog.temporal.data_imports.sources.klaviyo.klaviyo import (
_build_filter,
_build_initial_params,
+ _clamp_future_value_to_now,
_format_incremental_value,
)
from posthog.temporal.data_imports.sources.klaviyo.settings import KLAVIYO_ENDPOINTS, KlaviyoEndpointConfig
@@ -88,3 +91,57 @@ class TestBuildInitialParams:
assert "filter" in params
assert "+00:00" not in params["filter"]
assert params["filter"].endswith("Z)")
+
+ @freeze_time("2026-06-15T12:00:00Z")
+ def test_future_cursor_is_clamped_to_now(self) -> None:
+ # A future-dated cursor would otherwise build greater-than(datetime,<future>),
+ # which Klaviyo rejects with a 400 and wedges every subsequent sync.
+ config = KLAVIYO_ENDPOINTS["events"]
+ params = _build_initial_params(
+ config,
+ should_use_incremental_field=True,
+ db_incremental_field_last_value=datetime(2027, 2, 5, 21, 46, 42, tzinfo=UTC),
+ incremental_field="datetime",
+ )
+ assert params["filter"] == "greater-than(datetime,2026-06-15T12:00:00.000Z)"
+
+ @freeze_time("2026-06-15T12:00:00Z")
+ def test_past_cursor_is_not_modified(self) -> None:
+ config = KLAVIYO_ENDPOINTS["events"]
+ params = _build_initial_params(
+ config,
+ should_use_incremental_field=True,
+ db_incremental_field_last_value=datetime(2026, 3, 4, 2, 58, 14, tzinfo=UTC),
+ incremental_field="datetime",
+ )
+ assert params["filter"] == "greater-than(datetime,2026-03-04T02:58:14.000Z)"
+
+
+class TestClampFutureValueToNow:
+ @freeze_time("2026-06-15T12:00:00Z")
+ def test_future_datetime_is_clamped(self) -> None:
+ assert _clamp_future_value_to_now(datetime(2027, 2, 5, 21, 46, 42, tzinfo=UTC)) == datetime(
+ 2026, 6, 15, 12, 0, 0, tzinfo=UTC
+ )
+
+ @freeze_time("2026-06-15T12:00:00Z")
+ def test_naive_future_datetime_is_clamped(self) -> None:
+ assert _clamp_future_value_to_now(datetime(2027, 2, 5, 21, 46, 42)) == datetime(
+ 2026, 6, 15, 12, 0, 0, tzinfo=UTC
+ )
+
+ @freeze_time("2026-06-15T12:00:00Z")
+ def test_past_datetime_is_unchanged(self) -> None:
+ value = datetime(2026, 3, 4, 2, 58, 14, tzinfo=UTC)
+ assert _clamp_future_value_to_now(value) == value
+
+ @freeze_time("2026-06-15T12:00:00Z")
+ def test_future_date_is_clamped(self) -> None:
+ assert _clamp_future_value_to_now(date(2027, 2, 5)) == date(2026, 6, 15)
+
+ @freeze_time("2026-06-15T12:00:00Z")
+ def test_past_date_is_unchanged(self) -> None:
+ assert _clamp_future_value_to_now(date(2026, 3, 4)) == date(2026, 3, 4)
+
+ def test_string_passthrough(self) -> None:
+ assert _clamp_future_value_to_now("some-cursor-value") == "some-cursor-value"