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(instagram): retry a Graph API success response that is not JSON (#102310)

Tom Owers4c7d85d2580e
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/instagram.py b/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/instagram.py
index 90154f812e48..0c0ca24c8718 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/instagram.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/instagram.py
@@ -261,7 +261,15 @@ class InstagramClient:
timeout=REQUEST_TIMEOUT_SECONDS,
)
self._raise_for_error(response)
- body = response.json()
+ try:
+ body = response.json()
+ except ValueError as e:
+ # Every Graph API success path returns JSON, so a 2xx with an empty or non-JSON body is a
+ # truncated response or an edge server answering in Meta's place, not real output. The body
+ # stays out of the message: it can carry user-authored content or an HTML error page.
+ raise InstagramRetryableError(
+ f"Instagram API error (retryable): status={response.status_code}, message=response body was not JSON"
+ ) from e
return body if isinstance(body, dict) else {"data": body}
def _raise_for_error(self, response: requests.Response) -> None:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/tests/test_instagram.py b/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/tests/test_instagram.py
index 66a395f2cc10..8d6f2d23cb8f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/tests/test_instagram.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/instagram/tests/test_instagram.py
@@ -286,6 +286,23 @@ class TestInstagramTransport:
assert len(session.requested_urls) == MAX_RETRY_ATTEMPTS
+ def test_a_non_json_success_body_is_retried_then_surfaced(self) -> None:
+ # Meta answering a 2xx with an empty body or an edge server's HTML makes `response.json()`
+ # raise, which must be retried like any other truncated response instead of ending the sync
+ # with an opaque JSONDecodeError.
+ response = FakeResponse(200, repeat=True)
+ response.json = mock.Mock( # type: ignore[method-assign]
+ side_effect=requests.exceptions.JSONDecodeError("Expecting value: line 1 column 1 (char 0)", "", 0)
+ )
+ session = FakeSession([(ACCOUNT_ID, response)])
+ with mock.patch(f"{MODULE}.make_tracked_session", return_value=session):
+ client = InstagramClient("tok", "v23.0", LOGGER)
+
+ with pytest.raises(InstagramRetryableError):
+ client.get(client.build_url(ACCOUNT_ID))
+
+ assert len(session.requested_urls) == MAX_RETRY_ATTEMPTS
+
def test_a_connection_reset_mid_response_body_is_retried(self) -> None:
# A reset that lands while urllib3 is still reading a chunked body surfaces as
# ChunkedEncodingError rather than ConnectionError, but it's the same transient