Skip to content
5 changes: 3 additions & 2 deletions python/packages/core/agent_framework/_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ def to_dict(self, *, exclude: set[str] | None = None, exclude_none: bool = True)

This method performs deep serialization, automatically converting nested
``SerializationProtocol`` objects, lists, and dictionaries containing
serializable objects. Non-serializable objects are skipped with debug logging.
serializable objects. Attributes stored in either ``__dict__`` or ``__slots__``
are included. Non-serializable objects are skipped with debug logging.

Fields marked in ``DEFAULT_EXCLUDE`` and ``INJECTABLE`` are automatically
excluded from the output, as are any private attributes (starting with '_').
Expand All @@ -464,7 +465,7 @@ def to_dict(self, *, exclude: set[str] | None = None, exclude_none: bool = True)

# Get all instance attributes
result: dict[str, Any] = {} if "type" in combined_exclude else {"type": self._get_type_identifier()}
for key, value in self.__dict__.items():
for key, value in _iter_instance_fields(self).items():
if key not in combined_exclude and not key.startswith("_"):
if exclude_none and value is None:
continue
Expand Down
89 changes: 89 additions & 0 deletions python/packages/core/tests/core/test_serializable_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,92 @@ def __init__(self, name: str, config: dict | None = None):
assert input_data == original_input
assert input_data["config"] == {"base": True}
assert "injected" not in input_data["config"]

def test_to_dict_includes_slot_fields(self):
"""Test that to_dict includes public fields defined in __slots__."""

class SlotBacked(SerializationMixin):
__slots__ = ("value",)

def __init__(self, value: str):
self.value = value

obj = SlotBacked("hello")
data = obj.to_dict()

assert data["type"] == "slot_backed"
assert data["value"] == "hello"

restored = SlotBacked.from_dict(data)
assert restored.value == "hello"

def test_slot_fields_respect_exclusion_and_private_rules(self):
"""Test that slot-backed fields respect DEFAULT_EXCLUDE, explicit exclude, and private prefixes."""

class SlotExclusions(SerializationMixin):
__slots__ = ("public_field", "secret_field", "_private_field")
DEFAULT_EXCLUDE = {"secret_field"}

def __init__(self, public_field: str, secret_field: str, private_field: str):
self.public_field = public_field
self.secret_field = secret_field
self._private_field = private_field

obj = SlotExclusions(public_field="pub", secret_field="sec", private_field="priv")
data = obj.to_dict()

assert data["type"] == "slot_exclusions"
assert data["public_field"] == "pub"
assert "secret_field" not in data
assert "_private_field" not in data

# Test explicit exclude
data_excluded = obj.to_dict(exclude={"public_field"})
assert "public_field" not in data_excluded

def test_slot_fields_to_json_and_from_json_round_trip(self):
"""Test that slot-backed objects round trip correctly through to_json and from_json."""

class Point3D(SerializationMixin):
__slots__ = ("x", "y", "z")

def __init__(self, x: float, y: float, z: float):
self.x = x
self.y = y
self.z = z

pt = Point3D(1.5, 2.5, 3.5)
raw_json = pt.to_json()
parsed = json.loads(raw_json)

assert parsed == {"type": "point3_d", "x": 1.5, "y": 2.5, "z": 3.5}

restored = Point3D.from_json(raw_json)
assert restored.x == 1.5
assert restored.y == 2.5
assert restored.z == 3.5

def test_slot_inheritance_hierarchy_serialization(self):
"""Test serialization across class hierarchies with mixed slots and regular attributes."""

class BaseSlot(SerializationMixin):
__slots__ = ("base_id",)

def __init__(self, base_id: str):
self.base_id = base_id

class DerivedWithDict(BaseSlot):
def __init__(self, base_id: str, derived_name: str):
super().__init__(base_id)
self.derived_name = derived_name

obj = DerivedWithDict(base_id="b1", derived_name="d1")
data = obj.to_dict()

assert data["type"] == "derived_with_dict"
assert data["base_id"] == "b1"
assert data["derived_name"] == "d1"

restored = DerivedWithDict.from_dict(data)
assert restored.base_id == "b1"
assert restored.derived_name == "d1"
Loading