: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(data-imports): treat empty REST response body as no data (#66452)
Tom Owers53e7fd4e6222
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.pyindex feeffc2cc744..7a3df8beffd7 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py@@ -211,6 +211,13 @@ class RESTClient:try:body = response.json()except RequestsJSONDecodeError as e:+ # An empty body on an otherwise-successful response is a complete "no data"+ # answer (e.g. an endpoint with nothing to return), not a truncated page —+ # retrying can't conjure rows, so treat it as an empty page and let the+ # paginator stop. A non-empty body that fails to parse is a partial/truncated+ # read, which stays retryable.+ if not response.content or not response.content.strip():+ return response, Noneraise RESTClientRetryableError(f"Malformed JSON response from {response.url}: {e}") from ereturn response, bodydiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.pyindex 0b5b6ec082ce..596e8a917ee1 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py@@ -32,6 +32,18 @@ def _make_response(json_body: Any, status_code: int = 200) -> Response:return resp+def _make_empty_response(content: bytes = b"", status_code: int = 200) -> Response:+ # A successful response whose body is empty (or whitespace-only) — `response.json()`+ # raises "Expecting value: line 1 column 1 (char 0)", the shape an endpoint returns+ # when it has nothing to send back.+ resp = Response()+ resp.status_code = status_code+ resp._content = content+ resp.headers["Content-Type"] = "application/json"+ resp.url = "https://api.example.com/items"+ return resp++def _make_truncated_response(status_code: int = 200) -> Response:# Simulates a body cut off mid-stream — `response.json()` raises a JSONDecodeError# ("Unterminated string"), the same shape we see when an upstream truncates a large page.@@ -331,6 +343,26 @@ class TestRESTClient:assert mock_session.send.call_count == 5+ @pytest.mark.parametrize("content", [b"", b" \n\t"], ids=["empty", "whitespace_only"])+ @patch("tenacity.nap.time.sleep")+ @patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.rest_client.make_tracked_session"+ )+ def test_empty_response_body_yields_empty_page_without_retrying(self, MockSession, mock_sleep, content) -> None:+ # A successful response with an empty/whitespace-only body is a complete "no data"+ # answer, not a truncated page: yield an empty page and stop, never retrying.+ mock_session = MockSession.return_value+ mock_session.headers = {}+ mock_session.prepare_request.return_value = MagicMock()+ mock_session.send.return_value = _make_empty_response(content)++ client = RESTClient(base_url="https://api.example.com")+ pages = list(client.paginate(path="/items", data_selector="results", paginator=SinglePagePaginator()))++ assert pages == [[]]+ assert mock_session.send.call_count == 1+ mock_sleep.assert_not_called()+@patch("tenacity.nap.time.sleep")@patch("products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.rest_client.make_tracked_session"