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(stripe): reduce subscription page size to avoid truncated responses (#66457)

Tom Owersddc17b27688d
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
index 530d1ba61c10..bb6d1850c98c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
@@ -56,6 +56,14 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.stripe.set
LOGGER = get_logger(__name__)
DEFAULT_LIMIT = 100
+# Subscriptions are fetched with two levels of `expand` (subscription discounts and per-line-item
+# discounts), so each object is far larger than a typical Stripe resource. A full page of 100 such
+# objects can grow past the size that reliably transfers intact, and the response then arrives
+# truncated mid-stream — the SDK only discovers the bad body while JSON-decoding it, after its retry
+# loop, and re-fetching the identical oversized page just truncates again. A smaller page keeps each
+# response well within transferable size; _is_truncated_stripe_list_response stays as a backstop for
+# genuinely transient cuts.
+SUBSCRIPTION_PAGE_LIMIT = 20
_JSON_WHITESPACE = frozenset(b" \t\n\r\f\v")
_OPEN_BRACE = ord("{")
@@ -292,6 +300,9 @@ def _build_resources(
method=client.subscriptions.list,
params={
"status": "all",
+ # Smaller page than DEFAULT_LIMIT because the expansions below bloat each object; see
+ # SUBSCRIPTION_PAGE_LIMIT. Overrides the default `limit` in the merged params.
+ "limit": SUBSCRIPTION_PAGE_LIMIT,
# Expand discount objects so coupon details (amount_off, percent_off, duration) are inline.
# Without expansion Stripe returns only discount IDs, which prevents revenue projection.
# Key must be "expand" (not "expand[]") for a list value: the SDK encodes it as
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
index e9fa4dba7ddd..ce650e9a6773 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
@@ -17,9 +17,11 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.stripe.con
CUSTOMER_PAYMENT_METHOD_RESOURCE_NAME,
CUSTOMER_RESOURCE_NAME,
RESOURCE_TO_STRIPE_WEBHOOK_EVENT,
+ SUBSCRIPTION_RESOURCE_NAME,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.stripe.source import StripeSource
from products.warehouse_sources.backend.temporal.data_imports.sources.stripe.stripe import (
+ SUBSCRIPTION_PAGE_LIMIT,
StripeAuthenticationError,
StripeNestedResource,
StripeResource,
@@ -306,6 +308,46 @@ class TestStripeNestedResourceGetRows:
assert {row["customer"] for row in rows} == {"cus_credit", "cus_owed"}
+class TestSubscriptionPageSize:
+ def test_build_resources_caps_subscription_page_size(self):
+ # Subscriptions expand discounts at two levels, so a full DEFAULT_LIMIT page can grow past the
+ # size that transfers intact and arrives truncated mid-stream. The endpoint must request a
+ # smaller page than the default to keep each response transferable.
+ resources = stripe_module._build_resources(MagicMock(), logger=None)
+
+ subscription = resources[SUBSCRIPTION_RESOURCE_NAME]
+ assert subscription.params["limit"] == SUBSCRIPTION_PAGE_LIMIT
+ assert SUBSCRIPTION_PAGE_LIMIT < stripe_module.DEFAULT_LIMIT
+
+ def test_get_rows_sends_resource_limit_over_default(self):
+ # A resource's own `limit` must win over DEFAULT_LIMIT in the merged params — otherwise the
+ # reduced subscription page size would be clobbered back up to 100.
+ captured: dict = {}
+
+ def capturing_list(params):
+ captured.update(params)
+ return _FakeStripeList([])
+
+ resource = StripeResource(method=capturing_list, params={"limit": SUBSCRIPTION_PAGE_LIMIT})
+ resumable_source_manager = MagicMock()
+ resumable_source_manager.can_resume.return_value = False
+
+ with patch.object(stripe_module, "_build_resources", return_value={SUBSCRIPTION_RESOURCE_NAME: resource}):
+ list(
+ get_rows(
+ api_key="sk_test_123",
+ endpoint=SUBSCRIPTION_RESOURCE_NAME,
+ account_id=None,
+ db_incremental_field_last_value=None,
+ db_incremental_field_earliest_value=None,
+ logger=MagicMock(),
+ resumable_source_manager=resumable_source_manager,
+ )
+ )
+
+ assert captured["limit"] == SUBSCRIPTION_PAGE_LIMIT
+
+
class TestCustomerMightHaveBalanceTransactions:
@pytest.mark.parametrize(
"customer,expected",