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(stytch): retry search_timeout errors instead of failing the sync (#79060)

Tom Owers3d0b3a650333
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/stytch.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/stytch.py
index 064218f3d495..b156b582372e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/stytch.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/stytch.py
@@ -24,6 +24,11 @@ PAGE_SIZE = 500
MEMBER_SEARCH_ORG_CHUNK_SIZE = 100
REQUEST_TIMEOUT_SECONDS = 60
MAX_RETRIES = 5
+# Stytch surfaces its own search-query timeout as a 400 with this `error_type` (query complexity
+# can push search latency up to ~9s per Stytch's docs) rather than a 5xx, but it's the same
+# transient, self-recovering condition as one — worth the same in-process backoff, not a
+# permanent client error.
+TRANSIENT_ERROR_TYPES = {"search_timeout"}
class StytchRetryableError(Exception):
@@ -125,6 +130,12 @@ def _request(
error_type = response.json().get("error_type", "unknown")
except Exception:
error_type = "unknown"
+
+ if error_type in TRANSIENT_ERROR_TYPES:
+ raise StytchRetryableError(
+ f"Stytch API error (retryable): status={response.status_code}, error_type={error_type}, url={url}"
+ )
+
logger.error(f"Stytch API error: status={response.status_code}, error_type={error_type}, url={url}")
raise StytchAPIError(f"Stytch API error: status={response.status_code}, error_type={error_type}, url={url}")
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch.py
index 919131968da1..3b77616fe3d0 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch.py
@@ -166,6 +166,20 @@ class TestGetRowsUsers:
with pytest.raises(StytchAPIError, match="error_type=invalid_secret_authentication"):
list(get_rows("project-live-x", "secret", "users", mock.MagicMock(), _make_manager()))
+ @mock.patch(MOCK_PATH)
+ def test_search_timeout_is_retried_and_recovers(self, mock_session):
+ # Stytch's own backend search timeout comes back as a 400, not a 5xx, but it's the same
+ # transient condition — it must be retried in-process rather than raised as a hard error.
+ mock_session.return_value.request.side_effect = [
+ _response({"error_type": "search_timeout"}, 400),
+ _response(_search_page("results", [{"user_id": "u1"}], None)),
+ ]
+
+ batches = list(get_rows("project-live-x", "secret", "users", mock.MagicMock(), _make_manager()))
+
+ assert [item["user_id"] for batch in batches for item in batch] == ["u1"]
+ assert mock_session.return_value.request.call_count == 2
+
class TestGetRowsSessions:
@mock.patch(MOCK_PATH)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch_source.py
index 6a554186567b..c3b244878b9b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch_source.py
@@ -56,6 +56,7 @@ class TestStytchSource:
[
"Stytch API error (retryable): status=429, url=https://api.stytch.com/v1/users/search",
"Stytch API error: status=400, error_type=query_params_invalid, url=https://api.stytch.com/v1/users/search",
+ "Stytch API error (retryable): status=400, error_type=search_timeout, url=https://api.stytch.com/v1/b2b/organizations/search",
],
)
def test_non_retryable_errors_do_not_match_transient_or_query_errors(self, transient_error):