: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(klaviyo): treat 401/403 credential errors as non-retryable (#64186)
Tom Owersf4fe54e60e40
diff --git a/posthog/temporal/data_imports/sources/klaviyo/source.py b/posthog/temporal/data_imports/sources/klaviyo/source.pyindex 0e6a67308eac..1f567fbdd35e 100644--- a/posthog/temporal/data_imports/sources/klaviyo/source.py+++ b/posthog/temporal/data_imports/sources/klaviyo/source.py@@ -68,6 +68,16 @@ Make sure to grant the following read permissions:),)+ def get_non_retryable_errors(self) -> dict[str, str | None]:+ return {+ # An invalid, revoked, or insufficiently-scoped Klaviyo API key surfaces as a requests+ # HTTPError when `fetch_page` calls `raise_for_status()`. Retrying can never satisfy a+ # credential problem, so stop the sync. Match the stable status text and base host, not+ # the per-request path/query/timestamp.+ "401 Client Error: Unauthorized for url: https://a.klaviyo.com": "Your Klaviyo API key is invalid or has been revoked. Create a new private API key in your Klaviyo account settings, then reconnect.",+ "403 Client Error: Forbidden for url: https://a.klaviyo.com": "Your Klaviyo API key is missing the read permissions needed to sync this data. Grant the required read scopes in your Klaviyo account settings, then reconnect.",+ }+def get_schemas(self,config: KlaviyoSourceConfig,diff --git a/posthog/temporal/data_imports/sources/klaviyo/test_klaviyo.py b/posthog/temporal/data_imports/sources/klaviyo/test_klaviyo.pyindex 47cecced204d..dd23133cc695 100644--- a/posthog/temporal/data_imports/sources/klaviyo/test_klaviyo.py+++ b/posthog/temporal/data_imports/sources/klaviyo/test_klaviyo.py@@ -11,6 +11,7 @@ from posthog.temporal.data_imports.sources.klaviyo.klaviyo import (_format_incremental_value,)from posthog.temporal.data_imports.sources.klaviyo.settings import KLAVIYO_ENDPOINTS, KlaviyoEndpointConfig+from posthog.temporal.data_imports.sources.klaviyo.source import KlaviyoSourceclass TestFormatIncrementalValue:@@ -145,3 +146,38 @@ class TestClampFutureValueToNow:def test_string_passthrough(self) -> None:assert _clamp_future_value_to_now("some-cursor-value") == "some-cursor-value"+++class TestNonRetryableErrors:+ @parameterized.expand(+ [+ # 401/403 surfaced as a requests HTTPError when `fetch_page` calls `raise_for_status()`.+ # The per-request path/query/timestamp varies, but the status text and base host are stable.+ (+ "unauthorized",+ "401 Client Error: Unauthorized for url: https://a.klaviyo.com/api/events?filter=greater-than(datetime,2026-06-15T13:03:18.000Z)",+ ),+ (+ "forbidden",+ "403 Client Error: Forbidden for url: https://a.klaviyo.com/api/metrics",+ ),+ ]+ )+ def test_credential_errors_are_non_retryable(self, _name: str, observed_error: str) -> None:+ non_retryable_errors = KlaviyoSource().get_non_retryable_errors()+ assert any(key in observed_error for key in non_retryable_errors)++ @parameterized.expand(+ [+ # Transient/infra errors and server-side failures must stay retryable.+ ("read_timeout", "HTTPSConnectionPool(host='a.klaviyo.com', port=443): Read timed out."),+ (+ "server_error",+ "500 Server Error: Internal Server Error for url: https://a.klaviyo.com/api/events",+ ),+ ("connection_reset", "Connection reset by peer"),+ ]+ )+ def test_transient_errors_remain_retryable(self, _name: str, other_error: str) -> None:+ non_retryable_errors = KlaviyoSource().get_non_retryable_errors()+ assert not any(key in other_error for key in non_retryable_errors)