Skip to content

Python: fix(serialization): include slot-backed instance fields in SerializationMixin.to_dict - #10375

Open
Hardik K (hardikkaurani) wants to merge 6 commits into
microsoft:mainfrom
hardikkaurani:fix/serialization-slot-backed-fields
Open

Hardik K (hardikkaurani) wants to merge 6 commits into
microsoft:mainfrom
hardikkaurani:fix/serialization-slot-backed-fields

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Motivation & Context

Subclasses of SerializationMixin defining __slots__ were having their slot-backed public attributes omitted from to_dict() and to_json(). While copy(), deepcopy(), and __getstate__() (pickle) in _serialization.py already use _iter_instance_fields(instance) to collect attributes across both __dict__ and __slots__, to_dict() was directly iterating self.__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

  • What are the major changes?
    • Updated SerializationMixin.to_dict() in packages/core/agent_framework/_serialization.py to iterate _iter_instance_fields(self).items() instead of self.__dict__.items().
    • Added unit tests in test_serializable_mixin.py verifying 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.
  • What is the impact of these changes?
    • Slot-backed attributes on SerializationMixin classes are now preserved across to_dict() and to_json() round trips, aligning to_dict() with the established behavior in copy(), deepcopy(), and pickle.
    • No breaking changes; dictionary-backed classes behave identically.
  • What do you want reviewers to focus on?
    • Verification that using _iter_instance_fields(self) correctly preserves existing exclusion, none-exclusion, and private attribute filtering.

Related Issue

Fixes #10292

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: Used AI assistance for drafting regression tests and verification.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

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!

@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Nov 15, 2026

@surlymochan Nick (surlymochan) left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-binding review (read the diff and the base file; not an approval).

What I verified:

  • _iter_instance_fields already exists on main (_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), so to_dict was 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_dict and to_json/from_json, DEFAULT_EXCLUDE/explicit exclude/private-prefix rules still apply to slot fields, and mixed slot + __dict__ inheritance serializes both sides.

Two questions:

  1. Key order in to_dict output 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)?
  2. 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, does from_dict still restore the slot values (setattr path) or does it fail at construction? Worth a test if the latter is expected to work.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Nick (@surlymochan),

Thank you for taking the time to review and verify the changes! To answer your two questions:

  1. Key Order in to_dict:

    • For mixed classes, slot-backed fields were previously completely dropped from to_dict() because it only iterated self.__dict__.items(). As a result, no existing consumer or golden snapshot could have relied on the presence or relative ordering of slot attributes from to_dict().
    • In Python (3.7+), dictionary equality is order-insensitive, and deserialization paths (such as from_dict, which unpacks **kwargs) do not rely on key insertion order.
    • For consumers requiring deterministic serialized key ordering (e.g., golden files or snapshot tests), to_json forwards keyword arguments directly to json.dumps(), allowing sort_keys=True (e.g., obj.to_json(sort_keys=True)).
    • Reusing _iter_instance_fields aligns to_dict with the rest of _serialization.py (__copy__, __deepcopy__, and pickle state hooks).
  2. from_dict Construction and Slot Restoration:

    • SerializationMixin.from_dict is intentionally designed around constructor unpacking (return cls(**kwargs) at line 674 of _serialization.py). Unlike restore_pickle_state (which uses cls.__new__ and object.__setattr__), from_dict deliberately executes __init__ so that type validation, default field resolution, and dependency injection take effect.
    • Because of this, if a class (whether slot-backed or __dict__-backed) has public attributes that its __init__ does not accept as keyword arguments, from_dict will fail at construction with TypeError: __init__() got an unexpected keyword argument.
    • Classes with attributes not accepted by their constructor are expected to either mark them in DEFAULT_EXCLUDE (or private _), accept **kwargs in __init__, or provide a custom from_dict override.

Thanks again for the insightful questions and thoughtful review!

This branch was successfully deployed

1 active deployment
github-app-auth — c7e3e982 Deployed Nov 15, 2026 by hardikkaurani via add_label #27890
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: SerializationMixin drops slot-backed fields in dict/JSON round trips

3 participants