: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 Invalid range 400 on the full-sheet data read (#86887)
Tom Owers4d75c2434483
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 03c9f4062c8b..1ae7e6cdf81d 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@@ -388,7 +388,17 @@ def google_sheets_source(# Read the raw grid and build the records ourselves rather than calling# `get_all_records`, which can't cope with a blank header cell. `pad_values=True` mirrors# what `get_all_records` asks for, so every row is the same width as the widest one.- grid = cast(list[list[str]], _retry_on_transient_api_error(lambda: worksheet.get(pad_values=True)))+ try:+ grid = cast(list[list[str]], _retry_on_transient_api_error(lambda: worksheet.get(pad_values=True)))+ except gspread.exceptions.APIError as e:+ # Same deterministic 400 handled above for the header/incremental-field reads (e.g. an+ # empty sheet, or a sheet resized to have no columns) — this call has no explicit range,+ # so Google rejects the bare sheet-name reference as "Invalid range: '<title>'" instead+ # of "Unable to parse range". Treat it as an empty grid rather than failing the sync.+ if e.code == 400 and "Invalid range" in str(e):+ grid = []+ else:+ raisevalues = _records_from_grid(grid)if should_use_incremental_field and db_incremental_field_last_value is not None:diff --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 633951626373..38a9dc2d72ba 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@@ -707,6 +707,36 @@ def test_google_sheets_source_skips_unparseable_header_range():assert tables[0].num_rows == 0+def test_google_sheets_source_skips_unparseable_invalid_range_on_data_read():+ """The full-grid read in `get_rows` has no explicit A1 range (it asks for the whole sheet by+ name), so the same deterministic 400 handled above for the header/incremental-field reads+ surfaces as 'Invalid range: <title>' instead of 'Unable to parse range'. This call had no+ handling for it at all, so a zero-dimension worksheet crashed the sync outright instead of+ syncing as an empty table."""+ config = GoogleSheetsSourceConfig(spreadsheet_url="https://docs.google.com/spreadsheets/d/fake")++ mock_worksheet = mock.MagicMock()+ mock_worksheet.get_all_values.return_value = [[]]+ mock_worksheet.get.side_effect = _api_error(400, "Invalid range: 'empty_sheet'", "INVALID_ARGUMENT")++ 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 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