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(brevo): handle empty collections that omit the array key (#62129)

Tom Owers5ce06c01ae23
diff --git a/posthog/temporal/data_imports/sources/brevo/brevo.py b/posthog/temporal/data_imports/sources/brevo/brevo.py
index 31434e5c9f17..ef790df503d7 100644
--- a/posthog/temporal/data_imports/sources/brevo/brevo.py
+++ b/posthog/temporal/data_imports/sources/brevo/brevo.py
@@ -134,9 +134,10 @@ def get_rows(
params["offset"] = offset
data = fetch_page(params)
- # Fail loudly if the expected envelope key is missing (e.g. an API-shape change),
- # rather than silently reporting a successful sync of zero rows.
- items = data[config.data_key]
+ # Brevo omits the array key entirely (or sets it to null) for an empty collection,
+ # e.g. {"count": 0} with no "campaigns"/"segments" key. Treat that as an empty page
+ # rather than crashing the sync.
+ items = data.get(config.data_key) or []
if items:
yield items
diff --git a/posthog/temporal/data_imports/sources/brevo/tests/test_brevo.py b/posthog/temporal/data_imports/sources/brevo/tests/test_brevo.py
index 3efd0c272158..217f006186ac 100644
--- a/posthog/temporal/data_imports/sources/brevo/tests/test_brevo.py
+++ b/posthog/temporal/data_imports/sources/brevo/tests/test_brevo.py
@@ -208,14 +208,25 @@ class TestGetRowsErrors:
with pytest.raises(HTTPError):
_drive_get_rows("contacts", manager, responses)
- def test_missing_envelope_key_raises(self) -> None:
+ @pytest.mark.parametrize(
+ ("endpoint", "body"),
+ [
+ # Brevo omits the array key entirely for an empty collection (just {"count": 0}).
+ ("email_campaigns", {"count": 0}),
+ ("sms_campaigns", {"count": 0}),
+ ("contact_segments", {"count": 0}),
+ # Some responses set the key to null instead of omitting it.
+ ("email_campaigns", {"campaigns": None, "count": 0}),
+ ],
+ )
+ def test_missing_or_null_envelope_key_yields_nothing(self, endpoint: str, body: dict[str, Any]) -> None:
manager = MagicMock(spec=ResumableSourceManager)
manager.can_resume.return_value = False
- # 200 OK with an unexpected body shape must fail loudly, not sync zero rows.
- responses = [_make_response({"unexpected": []})]
- with pytest.raises(KeyError):
- _drive_get_rows("contacts", manager, responses)
+ _, rows = _drive_get_rows(endpoint, manager, [_make_response(body)])
+
+ assert rows == []
+ manager.save_state.assert_not_called()
class TestGetRowsSession: