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(plain): cap incremental filter datetimes at millisecond precision (#63770)

Tom Owers300fe9f4e509
diff --git a/posthog/temporal/data_imports/sources/plain/plain.py b/posthog/temporal/data_imports/sources/plain/plain.py
index 7ecc2e99a4cc..eccd627a2b59 100644
--- a/posthog/temporal/data_imports/sources/plain/plain.py
+++ b/posthog/temporal/data_imports/sources/plain/plain.py
@@ -29,12 +29,20 @@ _MESSAGE_INFO_FIELDS: list[tuple[str, str]] = [
def _datetime_to_plain_iso8601(value: datetime) -> str:
- """Serialize a datetime to the ISO-8601 format Plain returns and accepts (e.g. 2024-01-15T10:30:00.000Z)."""
+ """Serialize a datetime to an ISO-8601 format Plain accepts.
+
+ Plain's ``DatetimeFilter`` only accepts ``yyyy-MM-dd'T'HH:mm:ss.SSS'Z'`` (millisecond precision)
+ or ``yyyy-MM-dd'T'HH:mm:ss'Z'`` (whole seconds). ``datetime.isoformat()`` emits microseconds
+ (six fractional digits) whenever the value has sub-second precision, which Plain rejects with a
+ generic ``There was a validation error.``. Incremental watermarks routinely carry microseconds,
+ so cap the precision at milliseconds (and drop the fractional part entirely for whole seconds).
+ """
if value.tzinfo is None:
value = value.replace(tzinfo=UTC)
else:
value = value.astimezone(UTC)
- return value.isoformat().replace("+00:00", "Z")
+ timespec = "seconds" if value.microsecond == 0 else "milliseconds"
+ return value.isoformat(timespec=timespec).replace("+00:00", "Z")
def _updated_at_filter(updated_at_gte: datetime) -> dict[str, Any]:
diff --git a/posthog/temporal/data_imports/sources/plain/tests/test_plain.py b/posthog/temporal/data_imports/sources/plain/tests/test_plain.py
index 7f540e68a787..ee9b40bebd4d 100644
--- a/posthog/temporal/data_imports/sources/plain/tests/test_plain.py
+++ b/posthog/temporal/data_imports/sources/plain/tests/test_plain.py
@@ -236,6 +236,15 @@ class TestDatetimeHelpers:
def test_datetime_to_plain_iso8601_assumes_utc_for_naive(self):
assert _datetime_to_plain_iso8601(datetime(2024, 1, 15, 10, 30, 0)) == "2024-01-15T10:30:00Z"
+ def test_datetime_to_plain_iso8601_caps_microseconds_at_milliseconds(self):
+ # Plain rejects 6-digit microsecond precision with "There was a validation error.";
+ # incremental watermarks carry microseconds, so we must emit millisecond precision.
+ assert (
+ _datetime_to_plain_iso8601(datetime(2026, 6, 7, 18, 3, 36, 624000, tzinfo=UTC))
+ == "2026-06-07T18:03:36.624Z"
+ )
+ assert _datetime_to_plain_iso8601(datetime(2024, 1, 15, 10, 30, 0, 1, tzinfo=UTC)) == "2024-01-15T10:30:00.000Z"
+
def test_parse_plain_datetime_from_string(self):
assert _parse_plain_datetime("2024-01-15T10:30:00Z") == datetime(2024, 1, 15, 10, 30, 0, tzinfo=UTC)