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(data-warehouse): stop retrying stytch product line mismatches (#104092)

Tom Owers999508f773f0
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/source.py
index 5bdba2f9d67b..a89baef792c9 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/source.py
@@ -22,6 +22,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.stytch.set
STYTCH_ENDPOINTS,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.stytch.stytch import (
+ PRODUCT_LINE_MISMATCH_MESSAGES,
StytchResumeConfig,
check_endpoint_access,
stytch_source,
@@ -49,7 +50,7 @@ class StytchSource(ResumableSource[StytchSourceConfig, StytchResumeConfig]):
You can find both under [API keys](https://stytch.com/dashboard/api-keys) in your Stytch dashboard. Live credentials (`project-live-...`) sync from `api.stytch.com`; test credentials (`project-test-...`) sync your test environment's data from `test.stytch.com`.
-The `organizations` and `members` tables are only available for Stytch B2B projects.""",
+Every Stytch project is either a consumer project or a B2B project, and the two have separate tables. The `users` and `sessions` tables are only available for consumer projects, and the `organizations` and `members` tables only for B2B projects.""",
iconPath="/static/services/stytch.png",
docsUrl="https://posthog.com/docs/cdp/sources/stytch",
releaseStatus=ReleaseStatus.ALPHA,
@@ -84,13 +85,16 @@ The `organizations` and `members` tables are only available for Stytch B2B proje
return CANONICAL_DESCRIPTIONS
def get_non_retryable_errors(self) -> dict[str, str | None]:
- # Stytch reports credential failures as 400/401 JSON with a stable `error_type`, which the
- # transport surfaces in the raised message. Retrying can never fix these.
+ # Stytch reports credential and product-line failures as 400/401 JSON with a stable
+ # `error_type`, which the transport surfaces in the raised message. Retrying can never fix
+ # these.
return {
"error_type=invalid_project_id_authentication": "Your Stytch project ID is invalid. Check it in your Stytch dashboard under API keys, then reconnect.",
"error_type=invalid_secret_authentication": "Your Stytch secret is invalid or has been revoked. Create a new secret in your Stytch dashboard under API keys, then reconnect.",
"error_type=unauthorized_credentials": "Your Stytch credentials were rejected. Check your project ID and secret in your Stytch dashboard, then reconnect.",
"error_type=invalid_authorization_header": "Your Stytch credentials are malformed. Re-enter your project ID and secret, then reconnect.",
+ # Shared with the schema picker's per-table reason so both name the same next step.
+ **{f"error_type={error_type}": message for error_type, message in PRODUCT_LINE_MISMATCH_MESSAGES.items()},
}
def get_schemas(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/stytch.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/stytch.py
index b156b582372e..2e8888239f87 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/stytch.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/stytch.py
@@ -29,6 +29,13 @@ MAX_RETRIES = 5
# transient, self-recovering condition as one — worth the same in-process backoff, not a
# permanent client error.
TRANSIENT_ERROR_TYPES = {"search_timeout"}
+# A Stytch project is either a consumer project or a B2B project, and the two product lines have
+# disjoint API surfaces. Whichever tables belong to the other product line answer with one of these
+# error types on every request, so they can never sync against the connected project.
+PRODUCT_LINE_MISMATCH_MESSAGES: dict[str, str] = {
+ "invalid_consumer_endpoint": "The users and sessions tables only exist for Stytch consumer projects, and this is a B2B project. Sync the organizations and members tables instead.",
+ "invalid_b2b_endpoint": "The organizations and members tables only exist for Stytch B2B projects, and this is a consumer project. Sync the users and sessions tables instead.",
+}
class StytchRetryableError(Exception):
@@ -181,7 +188,7 @@ def check_endpoint_access(project_id: str, secret: str, path: str) -> str | None
error_type = response.json().get("error_type", "unknown")
except Exception:
error_type = "unknown"
- return f"Not available for this Stytch project ({error_type})"
+ return PRODUCT_LINE_MISMATCH_MESSAGES.get(error_type) or f"Not available for this Stytch project ({error_type})"
def _iter_search_pages(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch.py
index 3b77616fe3d0..b60e8d0cfa2a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch.py
@@ -99,6 +99,28 @@ class TestValidateCredentials:
assert validate_credentials("project-live-x", "secret") is False
+class TestCheckEndpointAccess:
+ @pytest.mark.parametrize(
+ "error_type, expected_reason",
+ [
+ (
+ "invalid_consumer_endpoint",
+ "The users and sessions tables only exist for Stytch consumer projects, and this is a B2B project. Sync the organizations and members tables instead.",
+ ),
+ (
+ "invalid_b2b_endpoint",
+ "The organizations and members tables only exist for Stytch B2B projects, and this is a consumer project. Sync the users and sessions tables instead.",
+ ),
+ ("some_other_denial", "Not available for this Stytch project (some_other_denial)"),
+ ],
+ )
+ @mock.patch(MOCK_PATH)
+ def test_product_line_mismatch_names_the_tables_to_use_instead(self, mock_session, error_type, expected_reason):
+ mock_session.return_value.post.return_value = _response({"error_type": error_type}, 400)
+
+ assert check_endpoint_access("project-live-x", "secret", "/v1/users/search") == expected_reason
+
+
class TestGetRowsUsers:
@mock.patch(MOCK_PATH)
def test_paginates_via_body_cursor_and_saves_state_after_yield(self, mock_session):
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch_source.py
index 205c24faddb8..29d983483eb1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stytch/tests/test_stytch_source.py
@@ -1,9 +1,11 @@
import pytest
from unittest import mock
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import error_message_matches
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.stytch import StytchSourceConfig
from products.warehouse_sources.backend.temporal.data_imports.sources.stytch.settings import ENDPOINTS
from products.warehouse_sources.backend.temporal.data_imports.sources.stytch.source import StytchSource
+from products.warehouse_sources.backend.temporal.data_imports.sources.stytch.stytch import StytchAPIError, get_rows
class TestStytchSource:
@@ -36,6 +38,40 @@ class TestStytchSource:
non_retryable_errors = self.source.get_non_retryable_errors()
assert not any(key in transient_error for key in non_retryable_errors)
+ @pytest.mark.parametrize(
+ "endpoint, error_type, advised_tables",
+ [
+ ("users", "invalid_consumer_endpoint", "organizations and members"),
+ ("sessions", "invalid_consumer_endpoint", "organizations and members"),
+ ("organizations", "invalid_b2b_endpoint", "users and sessions"),
+ ("members", "invalid_b2b_endpoint", "users and sessions"),
+ ],
+ )
+ @mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.stytch.stytch.make_tracked_session")
+ def test_product_line_mismatch_is_classified_with_the_tables_to_use_instead(
+ self, mock_session, endpoint, error_type, advised_tables
+ ):
+ # Drive the real transport so the raised message and the classifier's patterns cannot drift
+ # apart: an unclassified failure leaves the schema enabled and retrying a request Stytch
+ # will always reject.
+ response = mock.MagicMock(status_code=400, ok=False)
+ response.json.return_value = {"error_type": error_type}
+ mock_session.return_value.request.return_value = response
+ manager = mock.MagicMock()
+ manager.can_resume.return_value = False
+
+ with pytest.raises(StytchAPIError) as raised:
+ list(get_rows(self.config.project_id, self.config.secret, endpoint, mock.MagicMock(), manager))
+
+ classified = [
+ message
+ for pattern, message in self.source.get_non_retryable_errors().items()
+ if error_message_matches(str(raised.value), [pattern])
+ ]
+ assert len(classified) == 1
+ assert advised_tables in (classified[0] or "")
+ assert mock_session.return_value.request.call_count == 1
+
def test_get_schemas(self):
schemas = self.source.get_schemas(self.config, self.team_id)