Skip to content

Use Pydantic model_dump in serializer#761

Open
baumgold wants to merge 7 commits into
pydantic:mainfrom
baumgold:serializer
Open

Use Pydantic model_dump in serializer#761
baumgold wants to merge 7 commits into
pydantic:mainfrom
baumgold:serializer

Conversation

@baumgold

Copy link
Copy Markdown

Attempts to fix #754 by using the Pydantic serializer when serializing a model via the CLI

@kschwab

kschwab commented Jan 30, 2026

Copy link
Copy Markdown
Contributor

+@hramezani

@baumgold this is a good idea, thanks for the PR. We just need to make sure to maintain backwards compatibility. Can you add a bool kwarg (use_serializers?) to the serialize method with the default set to False. Then you can just make dumped_model an empty dictionary if flag is not set and update dumped_field to:

dumped_field = dumped_model.get(field_name, model_default)

You will also need to add some tests.

@baumgold

baumgold commented Feb 5, 2026

Copy link
Copy Markdown
Author

@kschwab - I've enhanced the PR as you suggested to maintain backwards compatibility.

@hramezani

Copy link
Copy Markdown
Collaborator

Thanks @baumgold for updating PR.

I think we still need

@baumgold
baumgold force-pushed the serializer branch 2 times, most recently from 6aed1be to b63cbaf Compare April 30, 2026 18:03
Addresses PR feedback requesting test coverage and documentation for
the use_serializers parameter added in the previous commits.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@baumgold

Copy link
Copy Markdown
Author

Apologies for the slow follow-up on this — thanks for your patience!

Here is a summary of what has been added since the initial review:

  • Backwards compatibility: The feature is opt-in via a use_serializers: bool = False parameter on CliApp.serialize(), so existing behaviour is completely unchanged by default.
  • Tests: Added four new test cases covering field_serializer transforms with use_serializers=True, the default-False behaviour, pydantic dataclasses, and propagation through nested subfields.
  • Docs: Added a paragraph and code example under the Serializing Arguments section of docs/index.md showing the opt-in use_serializers=True behaviour.

@kschwab — would you be able to take another look when you get a chance? Happy to make any further changes you suggest.

@hramezani

Copy link
Copy Markdown
Collaborator

thanks @baumgold for updating your PR.

Could you please add a test with BaseSettings? or change one of the existing tests and use BaseSettings
Also, a test with PlainSerializer/WrapSerializer from Annotated (the original issue's pattern) would be great

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

Successfully merging this pull request may close these issues.

Field PlainSerializer/WrapSerializer ignored during serialization

3 participants