: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(google_sheets): handle unparseable header range on empty worksheets (#86805)
Tom Owersf6700678655b
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/google_sheets.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/google_sheets.pyindex 440a91c9694a..03c9f4062c8b 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/google_sheets.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/google_sheets.py@@ -356,7 +356,16 @@ def google_sheets_source(worksheet = _get_worksheet(config.spreadsheet_url, worksheet_id, api_version)- headers = _retry_on_transient_api_error(lambda: worksheet.get_all_values("1:1")) # Get the first row+ try:+ headers = _retry_on_transient_api_error(lambda: worksheet.get_all_values("1:1")) # Get the first row+ except gspread.exceptions.APIError as e:+ # Same deterministic 400 handled in `get_schema_incremental_fields` above (e.g. empty+ # sheets, or sheets resized to have no columns) — treat it as a worksheet with no header+ # row rather than failing the sync.+ if e.code == 400 and "Unable to parse range" in str(e):+ headers = []+ else:+ raiseif len(headers) > 0:_assert_unique_normalized_column_names(headers[0])primary_keys = Nonediff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.pyindex 0a2e06b73c32..633951626373 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py@@ -675,6 +675,38 @@ def test_get_schema_incremental_fields_skips_unparseable_range():assert get_schema_incremental_fields(config, "csm_followups") == []+def test_google_sheets_source_skips_unparseable_header_range():+ """Google rejects the unbounded "1:1" header-row range with the same deterministic 400 'Unable+ to parse range' handled in `get_schema_incremental_fields` above (e.g. an empty sheet, or a+ sheet resized to have no columns). The sync must treat this as a worksheet with no header row+ instead of failing outright."""+ config = GoogleSheetsSourceConfig(spreadsheet_url="https://docs.google.com/spreadsheets/d/fake")++ mock_worksheet = mock.MagicMock()+ mock_worksheet.get_all_values.side_effect = _api_error(+ 400, "Unable to parse range: 'empty_sheet'!1:1", "INVALID_ARGUMENT"+ )+ mock_worksheet.get.return_value = [[]]++ with (+ mock.patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_sheets.google_sheets.get_schemas",+ return_value=[("empty_sheet", 123)],+ ),+ mock.patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_sheets.google_sheets._get_worksheet",+ return_value=mock_worksheet,+ ),+ ):+ response = google_sheets_source(+ config, "empty_sheet", db_incremental_field_last_value=None, api_version=GOOGLE_SHEETS_API_VERSION_V4+ )+ tables = list(cast(Iterable[Any], response.items()))++ assert response.primary_keys is None+ assert tables[0].num_rows == 0++def test_get_schema_incremental_fields_reraises_other_api_errors():"""Only the deterministic 'Unable to parse range' 400 is swallowed. Transient 5xx errors areretried with backoff and, once retries are exhausted, must still propagate so Temporal can retry