: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): skip worksheets whose header range can't be parsed (#63772)
Tom Owers37633b37188a
diff --git a/posthog/temporal/data_imports/sources/google_sheets/google_sheets.py b/posthog/temporal/data_imports/sources/google_sheets/google_sheets.pyindex 7db3dd7c6b14..e886c804904b 100644--- a/posthog/temporal/data_imports/sources/google_sheets/google_sheets.py+++ b/posthog/temporal/data_imports/sources/google_sheets/google_sheets.py@@ -125,7 +125,19 @@ def get_schema_incremental_fields(config: GoogleSheetsSourceConfig, worksheet_naworksheet = _get_worksheet(config.spreadsheet_url, worksheet_id)- rows = worksheet.get_all_values("1:2") # Get the first two rows+ try:+ rows = worksheet.get_all_values("1:2") # Get the first two rows+ except gspread.exceptions.APIError as e:+ # Google rejects the unbounded "1:2" row range with a 400 "Unable to parse range" for+ # some worksheets (e.g. empty sheets, or sheets resized to have no columns). This is+ # deterministic, so retrying never helps. We're only probing the first rows to detect an+ # incremental "id" column here, so a worksheet we can't read simply has no detectable+ # incremental field — return [] rather than letting one odd sheet break schema discovery+ # for the entire spreadsheet. Other API errors (e.g. transient 5xx) still propagate so+ # they can be retried.+ if e.code == 400 and "Unable to parse range" in str(e):+ return []+ raiseif len(rows) > 1 and "id" in rows[0]:index_of_id = rows[0].index("id")diff --git a/posthog/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py b/posthog/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.pyindex 41d32d5bb3f9..ccf8ba39c1fb 100644--- a/posthog/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py+++ b/posthog/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py@@ -10,12 +10,19 @@ from posthog.temporal.data_imports.sources.generated_configs import GoogleSheetsfrom posthog.temporal.data_imports.sources.google_sheets.google_sheets import (_PERMISSION_DENIED_MESSAGE,_get_worksheet,+ get_schema_incremental_fields,get_schemas,google_sheets_source,)from posthog.temporal.data_imports.sources.google_sheets.source import GoogleSheetsSource+def _api_error(code: int, message: str, status: str) -> gspread.exceptions.APIError:+ response = mock.MagicMock()+ response.json.return_value = {"error": {"code": code, "message": message, "status": status}}+ return gspread.exceptions.APIError(response)++def test_get_worksheet_backoff():with (mock.patch(@@ -264,3 +271,49 @@ def test_not_found_api_error_is_non_retryable():assert any(key in error_msg for key in non_retryable_errors), (f"Google Sheets 404 error {error_msg!r} did not match any non-retryable pattern")+++def test_get_schema_incremental_fields_skips_unparseable_range():+ """Google rejects the unbounded "1:2" row range with a 400 'Unable to parse range' for some+ worksheets (e.g. empty sheets). That deterministic error must not break schema discovery — the+ worksheet should just report no incremental fields so the rest of the spreadsheet still syncs."""+ 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: 'csm_followups'!1:2", "INVALID_ARGUMENT"+ )++ with (+ mock.patch(+ "posthog.temporal.data_imports.sources.google_sheets.google_sheets.get_schemas",+ return_value=[("csm_followups", 123)],+ ),+ mock.patch(+ "posthog.temporal.data_imports.sources.google_sheets.google_sheets._get_worksheet",+ return_value=mock_worksheet,+ ),+ ):+ assert get_schema_incremental_fields(config, "csm_followups") == []+++def test_get_schema_incremental_fields_reraises_other_api_errors():+ """Only the deterministic 'Unable to parse range' 400 is swallowed. Other API errors (e.g.+ transient 5xx) must still propagate so Temporal can retry them."""+ config = GoogleSheetsSourceConfig(spreadsheet_url="https://docs.google.com/spreadsheets/d/fake")++ mock_worksheet = mock.MagicMock()+ mock_worksheet.get_all_values.side_effect = _api_error(500, "Internal error encountered.", "INTERNAL")++ with (+ mock.patch(+ "posthog.temporal.data_imports.sources.google_sheets.google_sheets.get_schemas",+ return_value=[("sheet1", 123)],+ ),+ mock.patch(+ "posthog.temporal.data_imports.sources.google_sheets.google_sheets._get_worksheet",+ return_value=mock_worksheet,+ ),+ pytest.raises(gspread.exceptions.APIError),+ ):+ get_schema_incremental_fields(config, "sheet1")