Skip to content

from_attributes=True does not recognize properties on StripeObject #12199

@ryancausey

Description

@ryancausey

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When attempting to create a Pydantic model to pull attribute values from a StripeObject returned from the Stripe SDK, I ran into the issue where the stripe_account property present on the StripeObject is not recognized.

>>> from pydantic import BaseModel, ConfigDict
... import stripe
...
... class ExampleModel(BaseModel):
...     model_config = ConfigDict(from_attributes=True)
...
...     stripe_account: str
...
...
... class Example:
...     @property
...     def stripe_account(self):
...         return "acct_foo"
...
...
... # This works as expected.
... example_obj = Example()
... print(ExampleModel.model_validate(example_obj))
...
... # This fails unexpectedly
... stripe_obj = stripe.StripeObject(stripe_account="acct_asdf")
... print(ExampleModel.model_validate(stripe_obj))
...
stripe_account='acct_foo'
Traceback (most recent call last):
  File "<python-input-39>", line 22, in <module>
    print(ExampleModel.model_validate(stripe_obj))
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/home/rccausey/.cache/pypoetry/virtualenvs/billing-api--M5yUIBR-py3.13/lib/python3.13/site-packages/pydantic/main.py", line 705, in model_validate
    return cls.__pydantic_validator__.validate_python(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        obj, strict=strict, from_attributes=from_attributes, context=context, by_alias=by_alias, by_name=by_name
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
pydantic_core._pydantic_core.ValidationError: 1 validation error for ExampleModel
stripe_account
  Field required [type=missing, input_value=<StripeObject at 0x716079d7b750> JSON: {}, input_type=StripeObject]
    For further information visit https://errors.pydantic.dev/2.11/v/missing
>>> hasattr(stripe_obj, "stripe_account")
True
>>> stripe_obj.stripe_account
'acct_asdf'

The only thing I could think of is that the way the StripeObject is typed is somehow causing issues: class StripeObject(Dict[str, Any]):

Library versions:

pydantic = "2.11.7"
stripe = "12.5.0"

Example Code

from pydantic import BaseModel, ConfigDict
import stripe

class ExampleModel(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    stripe_account: str


class Example:
    @property
    def stripe_account(self):
        return "foo"


# This works as expected.
example_obj = Example()
print(ExampleModel.model_validate(example_obj))

# This fails unexpectedly
stripe_obj = stripe.StripeObject(stripe_account="acct_asdf")
print(ExampleModel.model_validate(stripe_obj))

Python, Pydantic & OS Version

>>> import pydantic.version; print(pydantic.version.version_info())
             pydantic version: 2.11.7
        pydantic-core version: 2.33.2
          pydantic-core build: profile=release pgo=false
               python version: 3.13.3 (main, May 21 2025, 10:47:46) [GCC 11.4.0]
                     platform: Linux-6.6.87.2-microsoft-standard-WSL2-x86_64-with-glibc2.35
             related packages: typing_extensions-4.14.1
                       commit: unknown

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V2Bug related to Pydantic V2pendingIs unconfirmed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions