Repository navigation
Description
Background
The objects you get back from the Stripe API all share a common base class: StripeObject (source). It holds the raw API response and has helpers for reading, writing, and updating data using a convenient dot notation: obj.name.whatever.
For many years, StripeObject has inherited from dict, which means you get dict-like behavior "for free": len(obj) shows how many keys are in the StripeObject, for key in obj iterates through those keys, etc.
While implementing all of the dict methods confers certain advantages when working with maps of data, there are also drawbacks. Most notably, any API response fields that share a name with a built-in method are inaccessible with dot notation:
o = Subscription() # an API response
o.whatever # works
o.whatever.sub_property # also fine
o.items # builtin method of `dict`
o.items[0] # error, method isn't subscriptable
o['items'] # list of StripeObjects, unintuitiveOver the years, we've gotten many issues filed about this counterintuitive behavior. But, we're hesitant to make breaking changes for behavior that's existed for so long. To better understand the implications of these changes, we're soliciting developer feedback to make sure we're serving your needs well.
Proposed Solution
Our proposal is that StripeObject would not inherit from anything. As a result, it would lose its dict-like behavior (len(obj), obj.items(), {**obj}, json.dumps(obj) etc) but retain property access via dot notation. Most documented examples don't take advantage (or even mention) that StripeObjects are dictionaries, so we're hoping to not lose much functionality here.
In the interest of minimizing the impact of this change, we'd add a StripeObject.as_dict() (name not final) method to return the underlying dictionary and enable all the behavior from before. As a result, the migration to keep existing behavior would be clear and relatively simple: call that method when you need dict-specific functionality.
Impact
As a result of no longer being a dict, any. This includes (but is not limited to):
- Passing the properties of a
StripeObjectto a function usingsome_func(**stripe_obj) - Dumping a
StripeObjectto json:json.dumps(stripe_obj)would need thedefault=varskwarg isintance(obj, dict)checks would change behavior, if you have any of those
Alternative Solution
We have an existing mechanism to separate the name of the class property from the API value. We already use this for tax.Registration.CountryOptions.Is (docs) because tax.registration.country_options.is is a SyntaxError; we useis_ instead.
We could do the same thing for subscription.items. .items would continue to be dict.items() and .items_ would be the list of SubscriptionItems. The type annotations would indicate this and the existing obj['items'] approach would continue to work.
We're not thrilled with this because it doesn't match the data you get from the API and it's an extra thing to think about and remember. But, in a world where most users write code with rich typing support, it may not be a big deal. Plus, this wouldn't be a breaking change, which is useful for reducing general churn and toil.
Our Questions for You:
- What code, if any, do you have that takes advantage of the fact that StripeObject is a dict?
- If you called
some_obj.as_dict()and made modifications to that dictionary, would you expect those changes to be reflected insome_obj?. Does your answer change if the method is calledto_dict()instead? - Would you prefer that we fixed the
itemsbug using the alternative solution mentioned above and kept the currentdictinheritance? Why or why not?
Feel free to provide any other suggestions, comments, or use cases that you think can be helpful. And as always, thank you for helping us make the Python SDK the best it can be!
Activity
Benoss commentedon Feb 8, 2025
I think not extending dict is the way to go. Maybe you should support both behaviors by having a DictClient returning the current behavior and another one maybe using Pydantic models instead.
This would help a lot with autocomplete and data validation via Static analyse but also at runtime before it even calls the API.
This would also probably help to build a mock client for unit testing
brendanator commentedon Mar 24, 2025
I'm in favour of @Benoss suggestion of having Pydantic models
Another issue that it would solve is that some fields marked as Optional actually might not even be available at all. e.g.
Customer.deletedis an optional fieldBut I use
if customer.deleted:when the customer is not deleted I getFailures like this decrease my confidence that my code will just work. I do not know which other fields might go wrong.
I strongly support a single breaking change to migrate to using Pydantic models. I expect this would resolve all these issues in a single swipe.
A lot of behaviour is built in to pydantic that supports the use-cases you mention:
Can use
some_func(**stripe_obj.model_dump())stripe_obj.model_dump_json()I wouldn't expect this to work, but Pydantic can do something better. Dump a dict, make changes, then validate that they are correct:
mdepinet commentedon May 23, 2025
Thank you for working on this!
items.to_dictmakes that more obvious.adambirds commentedon Aug 19, 2025
I hadn't realised there was a seperate thread for the comments and originally posted my comments on #1344. I got the issue after moving to Basil and having to access items for
current_period_start. Having to access items bysubscription["items"]is causing all my types underneath to be incorrect as of right now making confidence in my code harder.After reading the above and the various other comments here are my observations:
StripeObjectinherited fromdictuntil I had the issue accessing.items.In conclusion whether it is
to_dict()oras_dict()they would be seperate objects to the original object stored in a seperate veriable..items_whilst more beneficial than the current situation, just seems unnatural and would end up probably causing a lot of confusion when developers (especially new ones) look at the API docs and see both["items"]and.items_which wouldn't match all the other API clients.I would however also back @Benoss's suggestion of one massive breaking change to using pydantic models instead. This would be a lot more reliable, trust worthy and gives a lot fo great features builtin.
rminderhoud commentedon Oct 9, 2025
Conveniently serialize stripe event to backend to send to worker process
No, I consider this functionality a one-way serialization step.
getattrandsetattrcan be used in case for need for dynamic key name changes.Prefer explicit methods for converting to dict vs. dict inheritance.
nachogarcia commentedon Feb 3, 2026
Currently this has been one of the worst developer experience I have had in python so far.
Heavily in favour of the change.
xavdid-stripe commentedon Mar 26, 2026
Thanks for the feedback, everyone!
This change was merged last week and released yesterday in
v15.0.0.StripeObjectis now a standalone class.