Repository navigation
Python: fix(serialization): include slot-backed instance fields in SerializationMixin.to_dict - #10375
Conversation
…ing _iter_instance_fields
…json and from_json
|
Hi Eduard van Valkenburg (@eavanvalkenburg), Could you please take a look when you have a moment? This PR fixes #10292 where SerializationMixin.to_dict() omitted public attributes declared via slots because it iterated self.dict.items(). We updated it to use _iter_instance_fields(self), which is already used for copy, deepcopy, and getstate (pickle), ensuring slot-backed fields are consistently serialized while preserving exclusion and private prefix filtering. All unit tests and lint checks are passing locally. Thank you! |
Nick (surlymochan)
left a comment
There was a problem hiding this comment.
Non-binding review (read the diff and the base file; not an approval).
What I verified:
_iter_instance_fieldsalready exists onmain(_serialization.py:233-243) and is already used by the pickle paths (get_pickle_state,restore_pickle_state) and by two other serialization paths (lines 409, 424), soto_dictwas the one remaining__dict__-only path. This change aligns it with the rest of the file rather than introducing a new mechanism.- The new tests cover the important cases: a slot field round-trips through
to_dict/from_dictandto_json/from_json,DEFAULT_EXCLUDE/explicitexclude/private-prefix rules still apply to slot fields, and mixed slot +__dict__inheritance serializes both sides.
Two questions:
- Key order in
to_dictoutput now differs for mixed classes:__dict__entries first, then slot fields in MRO order. The old code emitted__dict__order only. Is any consumer sensitive to that order (deterministic snapshots, golden files)? - The round-trip tests all use classes whose
__init__takes the slot values as kwargs. For a slot-backed class whose__init__signature does not match the serialized field names, doesfrom_dictstill restore the slot values (setattr path) or does it fail at construction? Worth a test if the latter is expected to work.
|
Thank you for taking the time to review and verify the changes! To answer your two questions:
Thanks again for the insightful questions and thoughtful review! |
Motivation & Context
Subclasses of
SerializationMixindefining__slots__were having their slot-backed public attributes omitted fromto_dict()andto_json(). Whilecopy(),deepcopy(), and__getstate__()(pickle) in_serialization.pyalready use_iter_instance_fields(instance)to collect attributes across both__dict__and__slots__,to_dict()was directly iteratingself.__dict__.items(). As a result, slot-backed fields were omitted during dictionary/JSON serialization, causing deserialization failures (TypeError: missing required positional argument) or silent data reversion to defaults.Resolves #10292.
Description & Review Guide
SerializationMixin.to_dict()inpackages/core/agent_framework/_serialization.pyto iterate_iter_instance_fields(self).items()instead ofself.__dict__.items().test_serializable_mixin.pyverifying slot-backed field serialization, exclusion and private prefix filtering, JSON round-trips (to_json/from_json), and inheritance hierarchies with mixed slot and dictionary fields.SerializationMixinclasses are now preserved acrossto_dict()andto_json()round trips, aligningto_dict()with the established behavior incopy(),deepcopy(), andpickle._iter_instance_fields(self)correctly preserves existing exclusion, none-exclusion, and private attribute filtering.Related Issue
Fixes #10292
AI Assistance
AI assistance details: Used AI assistance for drafting regression tests and verification.
Contribution Checklist