Rework serialization to support pydantic codecs - #459
Draft
nikitakuklev wants to merge 3 commits into
Draft
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Collaborator
Author
|
@electronsandstuff opinion on overall arch? This would override #443 changes. |
nikitakuklev
marked this pull request as draft
August 21, 2026 11:46
Collaborator
|
I just gave it a skim. Overall, this feels like the most modern way to handle it in pydantic and I support. My understanding is that you make a "codec" and annotated type, NDArray = Annotated[np.ndarray, NDArrayCodec()]and pydantic knows to use Only other thoughts from quickly reading:
|
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Replace the recursive walker /
JSON_ENCODERSserialization machinery with context-aware codecs in a newxopt.typesmodule. Serialization behavior is now driven by a singleSerializationOptionspydantic context:array_mode:list|b64(binary numpy/torch payloads)module_mode:drop|file(.ptnext to the dump, load looks in same location (not always cwd)) |inlinedf_mode:dict|b64gzip/zstdcompressionEncoding is done with special prefixes, and compression detected through magic bytes, following
apsoptimplementation. We move to using pure pydantic functionality, using advanced hooks like__get_pydantic_core_schema__. See example notebook for usage.Backward compatibility
Old saves should load correctly. Legacy
base64:inline torch tag works but will be swapped on save.Format-affecting changes:
nullfor non-Optional fields now raises instead of being silently stripped.supports_*capability flags are frozen and no longer serialized. (bugfix)Other
orjsondependency dropped (pydantic already does fast json in Rust);zstandardadded; requirespydantic>=2.12Fixes #444