⚡ Solution Summary

  • Importing stripe library adds 500ms to startup time
  • Lazy loading parts of the library may help
  • Consider using a central get_stripe_client() function
  • Upgrade to the latest version for potential improvements
  • Monitor memory usage and cold start implications.
### Describe the bug I looked around, searched the issues and even googled. Surprised this hasn't come up. Just importing the library adds 500ms to startup time. There's got to be a way to lazy load parts. This is on a top of the line Mac with SSD. ### To Reproduce ```python # Takes about 20 ms time python -c "print('ok')"; # Takes bout 500ms time python -c "print('ok'); import stripe" ``` ### Expected behavior For serverless setups, startup time is pretty important. Even for local development, django's runserver or uvicorn's reload are both much slower with imports like these. Of course you can do the import locally everywhere stripe is used, but this can be problematic. At the very least, some guidance in the docs on how to do imports and have a lazy-loaded client would help. Something like a central `get_stripe_client() -> Stripe` or the like. And cover thread-safety in here. ### Code snippets _No response_ ### OS macOS ### Language version 3.12.1 ### Library version 11.3.0 ### API version 2020-08-27 ### Additional context _No response_

Discussion & Fixes

devraj 2024-11-25
Out of curiosity what serverless platform are you deploying to? This would only be an issue on cold starts, is that correct?
silviogutierrez 2024-11-25
For production, yes, it's mostly an issue with cold starts. If you use python's `importtime` you can see Stripe takes up a big, big chunk. See more here: https://adamj.eu/tech/2023/03/02/django-profile-and-improve-import-time/ Cold start platforms: Google Cloud Run in my case. But it also has local dev implications. Django and FastAPI rely on an autoreloading server. Unlike frontend tools like vite, there's no concept of hot module reloading. So this library — with no usage, just importing — is responsible for a 500ms lag in restarts. Imagine a few other libraries like this (Google storage does the same thing), and it starts to add up.
prathmesh-stripe 2024-11-25
Thanks for reporting this. We will be looking into SDK optimizations in the new year.
demoh2019 2025-01-21
+
bufke 2025-02-04
I noticed that that ram usage has crept up to very high levels with just `import stripe` - 4.2.0 - 11mb - 7.14.0 - 28mb - 11.5.0 - 57mb I tested with Docker python:3.13 image. I ran pip install stripe==version. Run python. Measure process memory. Run `import stripe`. Measure again and take the difference. I discovered this by noticing a major memory usage regression in my web application, which is attributed to upgrading stripe. This is likely the same issue, though I'd argue the memory usage is just as bad or worse as the slow start time. For comparison, my entire Celery process is only 80mb, this increase was enough to halt release and investigate for me.
bufke 2025-02-04
I took a peek with memray. I see that it imports both aiohttp and requests, lovely. Those were installed in my testing environment. _test_helpers_service looks questionable for importing. https://gitlab.com/-/snippets/4802529
stianjensen 2025-02-04
Some of the import time regression could be inside requests itself, depending on which version of requests you're getting with the different versions of stripe you tested: https://github.com/psf/requests/issues/6790
rsinger86 2025-02-16
I had to increase the capacity of my Fargate ECS service after adding this SDK to my project. Some optimizations would be much appreciated.
chris-mui 2025-02-18
Can confirm the memory usage has been an issue for us as well. We ended up refactoring out the stripe-python sdk. Claude makes short work out of building your own stripe.construct_event() method, dropped 50mb per worker from our memory use.
silviogutierrez 2025-02-18
@chris-mui could you elaborate on this? Do you mean still installing the library and going more low level? Or just actual API requests directly?
chris-mui 2025-02-18
Completely removed stripe sdk. Copy pasted api spec into claude to build out pydantic models for handling the requests. Construct_event() which handles webhook signature verification built in a similar fashion (didn't need any context to build this one though).
ludo-idemenager 2025-03-13
Just upgraded Stripe from a very old version to the latest, and memory usage also peaked. If someone could share a working solution to import on demand, that would be nice!
silviogutierrez 2025-03-13
@ludo-idemenager I have a file named `slow.py` In it: ```python from __future__ import annotations from typing import TYPE_CHECKING, Protocol, Type from django.conf import settings if TYPE_CHECKING: import stripe class StripeTestHelpers(Protocol): TestClock: Type[stripe.test_helpers.TestClock] class StripeBilling(Protocol): Session: Type[stripe.billing_portal.Session] class StripeModule(Protocol): billing_portal: StripeBilling test_helpers: StripeTestHelpers Customer: Type[stripe.Customer] Invoice: Type[stripe.Invoice] CardError: Type[stripe.CardError] InvalidRequestError: Type[stripe.InvalidRequestError] AuthenticationError: Type[stripe.AuthenticationError] APIConnectionError: Type[stripe.APIConnectionError] StripeError: Type[stripe.StripeError] Subscription: Type[stripe.Subscription] Token: Type[stripe.Token] Price: Type[stripe.Price] BalanceTransaction: Type[stripe.BalanceTransaction] PaymentMethod: Type[stripe.PaymentMethod] def get_stripe_client() -> StripeModule: import stripe stripe.api_key = settings.STRIPE_SECRET_KEY stripe.api_version = "2019-12-03" return stripe ``` Those are all the typings I need, otherwise they are broken as `mypy` has no way to just re-export all the types of a module unchanged. Then in my code: ```python def create_subscription( request: Request, form: forms.CreateSubscriptionForm ) -> SomeReturnType: stripe = slow.get_stripe_client() ```
ludo-idemenager 2025-03-13
Thanks, that's what I ended up doing too! > [@ludo-idemenager](https://github.com/ludo-idemenager) > > I have a file named `slow.py` > > In it: > > from __future__ import annotations > > from typing import TYPE_CHECKING, Protocol, Type > > from django.conf import settings > > if TYPE_CHECKING: > import stripe > > > class StripeTestHelpers(Protocol): > TestClock: Type[stripe.test_helpers.TestClock] > > > class StripeBilling(Protocol): > Session: Type[stripe.billing_portal.Session] > > > class StripeModule(Protocol): > billing_portal: StripeBilling > test_helpers: StripeTestHelpers > Customer: Type[stripe.Customer] > Invoice: Type[stripe.Invoice] > CardError: Type[stripe.CardError] > InvalidRequestError: Type[stripe.InvalidRequestError] > AuthenticationError: Type[stripe.AuthenticationError] > APIConnectionError: Type[stripe.APIConnectionError] > StripeError: Type[stripe.StripeError] > Subscription: Type[stripe.Subscription] > Token: Type[stripe.Token] > Price: Type[stripe.Price] > BalanceTransaction: Type[stripe.BalanceTransaction] > PaymentMethod: Type[stripe.PaymentMethod] > > > def get_stripe_client() -> StripeModule: > import stripe > > stripe.api_key = settings.STRIPE_SECRET_KEY > stripe.api_version = "2019-12-03" > return stripe > Those are all the typings I need, otherwise they are broken as `mypy` has no way to just re-export all the types of a module unchanged. > > Then in my code: > > def create_subscription( > request: Request, form: forms.CreateSubscriptionForm > ) -> SomeReturnType: > stripe = slow.get_stripe_client()
rushilsrivastava 2025-04-04
Over 50% of start time for our app is by the stripe SDK, and like others have observed, RAM usage has crept up as the library version has matured
bjornhol 2025-06-12
Upgraded from an old version and got a major increase of memory usage. Tried several versions from major 9 to 12. None seemed to be fixed. When can a fixed version be expected?
fortzi 2025-07-02
I can add that on AWS Lambda with python3.10 I'm sometimes seeing 3 second imports 😱
Dev63 2025-07-27
The size of this library and its memory demands are a big problem for my project. I have relatively simple needs at the moment, and am stuck using v6.0.0, which has a disk footprint of 900KB. Sadly, v12.3.0 has a disk footprint of 15MB+. With respect, that size is out of control. Why do I care? I, too, run in a "serverless" environment (Google App Engine). I'm less concerned about startup time than I am about absolute storage needs. I would have to increase the instance machine class (at significant expense) to be able to handle this library, which is rarely used. I really don't understand why the library needs to be so large... presumably the complexity could be/should be handled at Stripe's servers. At a minimum, perhaps we could have different versions or complementary Stripe modules so we don't need such a large footprint. Or maybe a REST API so we don't need any of your software.
bjornhol 2025-07-28
Maybe somone at Stripe can spend some time to investigate the performance problems, @ramya-stripe?
rushilsrivastava 2025-07-28
> Maybe somone at Stripe can spend some time to investigate the performance problems, [@ramya-stripe](https://github.com/ramya-stripe)? Incredibly overdue, this SDK is really beginning to feel like bloatware at this point. Adding payments to an app shouldn't require suffering to performance issues