This repository was archived by the owner on Apr 11, 2026. It is now read-only.
Introduce a schema variant to reuse Validators, Serializers and CoreSchema#1414
Closed
Introduce a schema variant to reuse Validators, Serializers and CoreSchema#1414
Conversation
BoxyUwU
commented
Aug 20, 2024
BoxyUwU
commented
Aug 20, 2024
Contributor
|
@adriangb, would love to get your feedback on this as well! |
Contributor
sydney-runkle
left a comment
There was a problem hiding this comment.
Interesting concepts here. I have some reservations about application for things like schemas with forward refs, but this looks like a promising start.
A few questions:
- What type of benchmarking have you been able to do to in order to look at both time / memory comparisons against
main? - This strikes me as pretty similar to our current pattern used here, at least conceptually. Why should we go about it in this way, instead? Perhaps the memory usage is better because we're not actually rebuilding the schema validator / serializer itself?
- I feel like integration with
pydantichere would be a bit of a challenge, especially in the namespace realm. Have you thought about how we might handle refs to other models (schemas) in different modules, functions, etc? Definitely ok if not, just wanted to gauge progress here.
I think some helpful next steps would be:
- Get some feedback from @samuelcolvin, @davidhewitt, and @adriangb.
- Post folks familiarizing themselves with said changes, we could set aside some time to chat about this in our next oss sync 🚀.
3c5a8ad to
0961627
Compare
0961627 to
b65d178
Compare
CodSpeed Performance ReportMerging #1414 will not alter performanceComparing Summary
|
5 tasks
40da0ee to
a78828e
Compare
8cd91b5 to
8ddba74
Compare
8ddba74 to
f6508d2
Compare
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces a
nestedCoreSchemavariant that reuses the specified model's schema/validator/serializer. As an example, the core schema for the following pydantic models:Compare to the schema generated on main
This is similar to the current definitions-ref setup but it has a number of benefits:
CoreSchemais smaller which means walking it can be significantly faster in cases with lots of nested models when we do not need to recurse into `nested-modeldefinitionsschemas where we walk the built schema and ensure that any nested models' schemas does not have adefinitionsschema. This is what I believe is a significant portion of the performance wins since we now have to do significantly less complex tree traversals over python datastructures in python.more times than once
See pydantic/pydantic#10246 for the implementation of generating this new schema variant and also the benchmak results.
Before Merging
serde_serializePyGcTraversibleimplementation forResultignores the error variant asPyErris not traversiblenestedschema variant is defined flexibly enough to be useable for more than just models.There is an issue pydantic/pydantic#10394 tracking future work on the pydantic side to use this schema more