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(data-warehouse): stop decagon syncs truncating at the first page (#78129)

jake sciotto6dddd14069ee
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/decagon.py b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/decagon.py
index 81de3193772c..998d63e5745a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/decagon.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/decagon.py
@@ -16,6 +16,16 @@ DECAGON_BASE_URL = "https://api.decagon.ai"
REQUEST_TIMEOUT_SECONDS = 60
+# Server-side page size of /conversation/export, per Decagon's docs.
+DECAGON_PAGE_SIZE = 100
+
+# Decagon's export reference names the next-page response field three different ways:
+# `next_page_cursor` in the parameter prose, `next_cursor` in the official example code,
+# and `next_page_updated_after` in the example response. Production responses carry no
+# usable `next_page_cursor` (reading only that name made the walk stop after one page),
+# so accept every documented name. All of them feed the same `cursor` request param.
+NEXT_CURSOR_KEYS = ("next_page_cursor", "next_cursor", "next_page_updated_after")
+
# Decagon enforces a hard limit of 1 request/second across all API endpoints and
# automatically IP-bans gross violators, so requests are spaced client-side rather
# than relying on 429 backoff alone.
@@ -28,7 +38,7 @@ class DecagonRetryableError(Exception):
@dataclasses.dataclass
class DecagonResumeConfig:
- # The next-page export cursor returned by Decagon (`next_page_cursor`).
+ # The next-page export cursor returned by Decagon (see NEXT_CURSOR_KEYS).
cursor: str
@@ -54,6 +64,18 @@ def _get_headers(api_key: str) -> dict[str, str]:
}
+def _next_cursor(data: dict[str, Any]) -> Optional[str]:
+ # Skip falsy values rather than returning the first key present: a response that
+ # carries `next_page_cursor: null` alongside a populated alias must keep paginating.
+ for key in NEXT_CURSOR_KEYS:
+ value = data.get(key)
+ if value:
+ # Cursors can be integers (a last-updated epoch watermark in Decagon's example
+ # response); requests and the saved resume state both want strings.
+ return str(value)
+ return None
+
+
def validate_credentials(api_key: str) -> bool:
"""Probe the export endpoint (the only one this source calls) to confirm the key works."""
try:
@@ -116,7 +138,7 @@ def get_rows(
data = fetch_page(params)
items = data.get(config.data_key) or []
- next_cursor = data.get("next_page_cursor")
+ next_cursor = _next_cursor(data)
fresh: list[dict[str, Any]] = []
for item in items:
@@ -135,15 +157,24 @@ def get_rows(
# than skipping it (the duplicate rows a resumed re-yield can produce are
# bounded to one page and cleaned up by the next full refresh).
if next_cursor:
- resumable_source_manager.save_state(DecagonResumeConfig(cursor=str(next_cursor)))
+ resumable_source_manager.save_state(DecagonResumeConfig(cursor=next_cursor))
- # `next_page_cursor` is null once the stream is exhausted. Also stop if the
+ # The next-page cursor is null once the stream is exhausted. Also stop if the
# server ever returns the cursor we just used, to guard against spinning on
# one page forever.
- if not next_cursor or str(next_cursor) == cursor:
+ if not next_cursor or next_cursor == cursor:
+ if not next_cursor and len(items) >= DECAGON_PAGE_SIZE:
+ # A full page that ends the walk is legitimate only when the total row
+ # count happens to be a multiple of the page size; far more often it means
+ # Decagon renamed the pagination field again and rows were truncated.
+ logger.warning(
+ f"Decagon: {endpoint} stream ended on a full page of {len(items)} items without a "
+ f"next-page cursor (response keys: {sorted(data.keys())}). If the synced row count "
+ f"looks truncated, check the export pagination contract."
+ )
break
- cursor = str(next_cursor)
+ cursor = next_cursor
def decagon_source(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/settings.py
index 7a03734722df..9f8677c00492 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/settings.py
@@ -18,11 +18,14 @@ class DecagonEndpointConfig:
# Decagon's syncable surface is a single stream: /conversation/export returns
-# conversations with their messages, CSAT ratings, tags, and metadata, paginated
-# with a `cursor` request param and a `next_page_cursor` response field (null once
-# exhausted, up to 100 conversations per page). There is no server-side timestamp
-# filter, so the stream is full refresh only — the export cursor is an opaque
-# stream position, not a durable watermark our incremental machinery can use.
+# conversations with their messages, CSAT ratings, tags, and metadata, up to 100
+# per page. Pagination is a `cursor` request param fed from a next-page response
+# field whose name varies across Decagon's own docs (see NEXT_CURSOR_KEYS in
+# decagon.py); it is null once the stream is exhausted. The export also accepts
+# optional min_timestamp/max_timestamp filters on a conversation's last-updated
+# time, but rows expose no last-updated column our incremental machinery could
+# store as a watermark (only created_at, and a conversation re-enters the stream
+# whenever it receives new messages), so the stream is full refresh only.
DECAGON_ENDPOINTS: dict[str, DecagonEndpointConfig] = {
"conversations": DecagonEndpointConfig(
name="conversations",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/tests/test_decagon.py b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/tests/test_decagon.py
index f6c0a5804229..5e3dbf24508d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/tests/test_decagon.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/decagon/tests/test_decagon.py
@@ -89,21 +89,44 @@ class TestGetRows:
manager.can_resume.return_value = False
return manager
- def test_paginates_with_cursor_and_saves_state_after_each_yield(self) -> None:
+ # Decagon's docs name the next-page field three different ways (reference prose,
+ # example code, example response); the connector must paginate with whichever one
+ # the API returns, else a full refresh silently truncates to the first 100 rows.
+ @parameterized.expand(
+ [
+ ("prose_name", "next_page_cursor", "cur-1", "cur-2"),
+ ("example_code_name", "next_cursor", "cur-1", "cur-2"),
+ ("example_response_name_int_watermark", "next_page_updated_after", 1704067200, 1704153600),
+ ]
+ )
+ def test_paginates_with_each_documented_cursor_key_and_saves_state_after_each_yield(
+ self, _name: str, cursor_key: str, cursor_1: Any, cursor_2: Any
+ ) -> None:
manager = self._fresh_manager()
responses = [
- _make_response({"conversations": [_conversation("c1")], "next_page_cursor": "cur-1"}),
- _make_response({"conversations": [_conversation("c2")], "next_page_cursor": "cur-2"}),
- _make_response({"conversations": [_conversation("c3")], "next_page_cursor": None}),
+ _make_response({"conversations": [_conversation("c1")], cursor_key: cursor_1}),
+ _make_response({"conversations": [_conversation("c2")], cursor_key: cursor_2}),
+ _make_response({"conversations": [_conversation("c3")], cursor_key: None}),
]
sent_params, yielded_ids = self._drive(manager, responses)
# First request omits the cursor (starts at the oldest conversations).
- assert sent_params == [{}, {"cursor": "cur-1"}, {"cursor": "cur-2"}]
+ assert sent_params == [{}, {"cursor": str(cursor_1)}, {"cursor": str(cursor_2)}]
assert yielded_ids == [["c1"], ["c2"], ["c3"]]
saved = [call.args[0] for call in manager.save_state.call_args_list]
- assert saved == [DecagonResumeConfig(cursor="cur-1"), DecagonResumeConfig(cursor="cur-2")]
+ assert saved == [DecagonResumeConfig(cursor=str(cursor_1)), DecagonResumeConfig(cursor=str(cursor_2))]
+
+ def test_null_prose_cursor_key_does_not_mask_a_populated_alias(self) -> None:
+ manager = self._fresh_manager()
+ responses = [
+ _make_response({"conversations": [_conversation("c1")], "next_page_cursor": None, "next_cursor": "cur-1"}),
+ _make_response({"conversations": [_conversation("c2")], "next_page_cursor": None, "next_cursor": None}),
+ ]
+ sent_params, yielded_ids = self._drive(manager, responses)
+
+ assert sent_params == [{}, {"cursor": "cur-1"}]
+ assert yielded_ids == [["c1"], ["c2"]]
def test_resume_seeds_cursor_from_saved_state(self) -> None:
manager = MagicMock(spec=ResumableSourceManager)