Skip to content

'ImmutableMutableProxy' object cannot be converted to 'PyDict' with Pydantic v2 #5103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
clemlesne opened this issue Apr 8, 2025 · 3 comments

Comments

@clemlesne
Copy link

clemlesne commented Apr 8, 2025

Describe the bug

Manipulating any Pydantic model type from a state raise a library error pydantic_core._pydantic_core.PydanticSerializationError: ... 'ImmutableMutableProxy' object cannot be converted to 'PyDict'.

To reproduce

  • Define two Pydantic v2 models, A and B, B a child if the first
  • Assign B to the app state as b: B = B()
  • Execute TypeAdapter(A).dump_json(self.b)
  • See the error

Expected behavior

Any manipulation of a Pydantic model inside the state work like it should out of the state.

Context

  • Python version: v3.13
  • Reflex version: v0.7.0
  • Pydantic version: v2.10.6
  • OS: Debian Bookworm
  • Browser (Optional): n/a
kshitiz305 added a commit to kshitiz305/reflex that referenced this issue Apr 10, 2025
@adhami3310
Copy link
Member

Reflex wraps the state arguments in proxies, you can get the inner value with __wrapped__.

TypeAdapter(A).dump_json(self.b.__wrapped__)

I'm not sure if we can provide a way to hook into pydantic serialization?

@clemlesne
Copy link
Author

Maybe test in ImmutableMutableProxy.__dict__ method override the caller package and return the original response with super().__dict__ instead of the wrapper? Hacky and not a good practice, but it should work out of the box and kept simple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants