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(linkedin_ads): treat invalid account ID 400 as non-retryable (#63701)

Tom Owers097bd8eab836
diff --git a/posthog/temporal/data_imports/sources/linkedin_ads/source.py b/posthog/temporal/data_imports/sources/linkedin_ads/source.py
index d5ed401b683e..02b7e26027e2 100644
--- a/posthog/temporal/data_imports/sources/linkedin_ads/source.py
+++ b/posthog/temporal/data_imports/sources/linkedin_ads/source.py
@@ -52,6 +52,12 @@ class LinkedInAdsSource(ResumableSource[LinkedinAdsSourceConfig, LinkedInAdsResu
# or PostHog no longer has access to it. Retrying can't fix a missing resource, so we
# stop and surface a config-facing message instead of burning retries every schedule.
"No virtual resource found": "LinkedIn could not find the configured ad account. Please check that the Account ID is correct and that PostHog still has access to it, then try again.",
+ # The Account ID is a free-text field. A malformed value (a profile URL, a name, stray
+ # whitespace) makes LinkedIn reject the `urn:li:sponsoredAccount:<id>` accounts param with
+ # a deterministic 400 ("...is invalid. Reason: Deserializing output ... failed"). Retrying
+ # never succeeds, so fail fast and tell the user to fix the configured Account ID. Match on
+ # the stable prefix only — the offending value that follows varies per source.
+ "Array parameter 'accounts' value 'urn:li:sponsoredAccount:": "The LinkedIn Ads Account ID is invalid. Please check the Account ID in your source configuration — it should be the numeric account ID from your LinkedIn Campaign Manager.",
}
@property
diff --git a/posthog/temporal/data_imports/sources/linkedin_ads/tests/test_linkedin_source.py b/posthog/temporal/data_imports/sources/linkedin_ads/tests/test_linkedin_source.py
index 823f974b20de..075ef9b3ff4b 100644
--- a/posthog/temporal/data_imports/sources/linkedin_ads/tests/test_linkedin_source.py
+++ b/posthog/temporal/data_imports/sources/linkedin_ads/tests/test_linkedin_source.py
@@ -1,7 +1,10 @@
+import json
+
import pytest
from unittest import mock
from posthog.temporal.data_imports.sources.generated_configs import LinkedinAdsSourceConfig
+from posthog.temporal.data_imports.sources.linkedin_ads.client import LinkedinAdsClient
from posthog.temporal.data_imports.sources.linkedin_ads.source import LinkedInAdsSource
@@ -120,3 +123,55 @@ class TestLinkedInAdsSource:
non_retryable_errors = self.source.get_non_retryable_errors()
assert not any(pattern in transient_message for pattern in non_retryable_errors)
+
+ @pytest.mark.parametrize(
+ "invalid_account_id",
+ [
+ "Reed Lnkedin",
+ "https://www.linkedin.com/company/recruiteasy-ca",
+ ],
+ )
+ @mock.patch("posthog.temporal.data_imports.sources.linkedin_ads.client.RestliClient")
+ def test_invalid_account_id_error_is_non_retryable(self, mock_restli_client, invalid_account_id):
+ """A malformed Account ID makes LinkedIn reject the accounts URN with a deterministic 400.
+ The raised message must match a get_non_retryable_errors pattern, else the job retries forever."""
+ body = json.dumps(
+ {
+ "message": (
+ f"Array parameter 'accounts' value 'urn:li:sponsoredAccount:{invalid_account_id}' is invalid. "
+ f"Reason: Deserializing output 'urn:li:sponsoredAccount:{invalid_account_id}' failed"
+ ),
+ "status": 400,
+ }
+ )
+ mock_response = mock.MagicMock()
+ mock_response.status_code = 400
+ mock_response.response.text = body
+ mock_restli_client.return_value.finder.return_value = mock_response
+
+ client = LinkedinAdsClient("test_access_token")
+ with pytest.raises(Exception) as exc_info:
+ client.get_accounts()
+
+ error_message = str(exc_info.value)
+ patterns = self.source.get_non_retryable_errors()
+ assert any(pattern in error_message for pattern in patterns), (
+ f"LinkedIn invalid-account error '{error_message}' does not match any non-retryable pattern"
+ )
+
+ @mock.patch("posthog.temporal.data_imports.sources.linkedin_ads.client.RestliClient")
+ def test_unrelated_400_is_not_classified_as_non_retryable(self, mock_restli_client):
+ """The accounts-URN pattern must be specific — an unrelated 400 must not match it."""
+ body = json.dumps({"message": "Invalid 'fields' parameter", "status": 400})
+ mock_response = mock.MagicMock()
+ mock_response.status_code = 400
+ mock_response.response.text = body
+ mock_restli_client.return_value.finder.return_value = mock_response
+
+ client = LinkedinAdsClient("test_access_token")
+ with pytest.raises(Exception) as exc_info:
+ client.get_accounts()
+
+ error_message = str(exc_info.value)
+ patterns = self.source.get_non_retryable_errors()
+ assert not any(pattern in error_message for pattern in patterns)