Skip to content
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

Add SerializeAsAny to migration guide and possibly move functionality into model_config #7043

Closed
1 task done
taddes opened this issue Aug 8, 2023 · 3 comments
Closed
1 task done

Comments

@taddes
Copy link

taddes commented Aug 8, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

We ran into some issues with serialization when migrating (subclass data loss). It wasn't until finding this bug that led us to discover the docs with the Migration warning stating you need to use SerializeAsAny.

This is less a bug and more a request to put this front and center in the migration doc. We lost a lot of time trying to figure this out and given that serialization is so essential for any API, esp FastAPI, this isn't something that should require a ton of searching to figure out.

Anyway, wondering if there is a possible plan to implement this logic within the model_config = ConfigDict()? I think it would make more intuitive sense.

From Docs, perhaps add to migration guide:
This behavior is different from how things worked in Pydantic V1, where we would always include all (subclass) fields when recursively dumping models to dicts. The motivation behind this change in behavior is that it helps ensure that you know precisely which fields could be included when serializing, even if subclasses get passed when instantiating the object. In particular, this can help prevent surprises when adding sensitive information like secrets as fields of subclasses.

Thanks so much!

Example Code

No response

Python, Pydantic & OS Version

pydantic version: 2.1.0
        pydantic-core version: 2.4.0
          pydantic-core build: profile=release pgo=false mimalloc=true
                 install path: /Users/taddeskorris/.pyenv/versions/3.11.0/envs/merino-311/lib/python3.11/site-packages/pydantic
               python version: 3.11.0 (main, Dec 13 2022, 15:17:55) [Clang 13.1.6 (clang-1316.0.21.2.5)]
                     platform: macOS-12.5-arm64-arm-64bit
     optional deps. installed: ['typing-extensions']

Selected Assignee: @dmontagu

@taddes taddes added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Aug 8, 2023
@adriangb
Copy link
Member

adriangb commented Aug 9, 2023

Anyway, wondering if there is a possible plan to implement this logic within the model_config = ConfigDict()? I think it would make more intuitive sense.

We decided it would be better as a runtime flag, see pydantic/pydantic-core#740. The constraint has been bandwidth in implementation. A contribution to move that work forward or to improve the docs would be very welcome!

@dmontagu dmontagu added feature request and removed unconfirmed Bug not yet confirmed as valid/applicable bug V2 Bug related to Pydantic V2 labels Aug 14, 2023
@dmontagu dmontagu removed their assignment Aug 14, 2023
@mjog
Copy link

mjog commented Aug 29, 2023

FWIW I'd like this to be configurable rather than a runtime flag. I'd like to be able to take advantage of the new default behaviour where it makes sense, but I'd also like to not have to shotgun SerializeAsAny throughout my codebases as well.

Libraries that embed Pydantic (e.g. FastAPI) may not expose this via it's API, which would be another reason to avoid the runtime arg.

@sydney-runkle
Copy link
Member

Closing as a duplicate of #6423, which we've included in our 2.6.0 milestone. Will remember to update the migration guide and docs as well 👍

@sydney-runkle sydney-runkle closed this as not planned Won't fix, can't repro, duplicate, stale Dec 15, 2023
@sydney-runkle sydney-runkle removed this from the Documentation Sprint milestone Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants