Skip to content

Commit 77ee1bd

Browse files
moonbox3Copilot
andcommitted
Decouple __deepcopy__ from DEFAULT_EXCLUDE in SerializationMixin (microsoft#4455)
Introduce _SHALLOW_COPY_FIELDS class variable in SerializationMixin to separate deep-copy semantics from serialization semantics. Previously, __deepcopy__ used DEFAULT_EXCLUDE to decide which fields to shallow-copy, conflating 'not serialized' with 'not safe to deep-copy'. A field added to DEFAULT_EXCLUDE purely for serialization (e.g. additional_properties) would be silently shared between original and copy. - Add _SHALLOW_COPY_FIELDS (default {'raw_representation'}) to SerializationMixin, matching the pattern already used by Content - Update __deepcopy__ to read from _SHALLOW_COPY_FIELDS instead of DEFAULT_EXCLUDE - Add test verifying DEFAULT_EXCLUDE fields are deep-copied unless also in _SHALLOW_COPY_FIELDS - Add test for Content._SHALLOW_COPY_FIELDS identity preservation - Add test for ChatResponse deep-copying additional_properties Co-authored-by: Copilot <[email protected]>
1 parent 06b23ed commit 77ee1bd

3 files changed

Lines changed: 65 additions & 11 deletions

File tree

‎python/packages/core/agent_framework/_serialization.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,21 +264,22 @@ def __init__(self, **kwargs):
264264

265265
DEFAULT_EXCLUDE: ClassVar[set[str]] = set()
266266
INJECTABLE: ClassVar[set[str]] = set()
267+
_SHALLOW_COPY_FIELDS: ClassVar[set[str]] = {"raw_representation"}
267268

268269
def __deepcopy__(self, memo: dict[int, Any]) -> SerializationMixin:
269-
"""Create a deep copy, preserving ``DEFAULT_EXCLUDE`` fields by reference.
270+
"""Create a deep copy, preserving ``_SHALLOW_COPY_FIELDS`` by reference.
270271
271-
Fields listed in ``DEFAULT_EXCLUDE`` may contain LLM SDK objects
272+
Fields listed in ``_SHALLOW_COPY_FIELDS`` may contain LLM SDK objects
272273
(e.g., proto/gRPC responses) that are not safe to deep-copy. They are
273274
kept as shallow references in the copy; all other attributes are
274275
deep-copied normally.
275276
"""
276277
cls = type(self)
277278
result = cls.__new__(cls)
278279
memo[id(self)] = result
279-
exclude = cls.DEFAULT_EXCLUDE
280+
shallow = cls._SHALLOW_COPY_FIELDS
280281
for k, v in self.__dict__.items():
281-
if k in exclude:
282+
if k in shallow:
282283
object.__setattr__(result, k, v)
283284
else:
284285
object.__setattr__(result, k, copy.deepcopy(v, memo))

‎python/packages/core/tests/core/test_serializable_mixin.py‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -428,16 +428,16 @@ def __init__(self, value: str, options: dict | None = None):
428428
assert obj.options["existing"] == "value"
429429
assert obj.options["injected"] == "option"
430430

431-
def test_deepcopy_preserves_excluded_fields_by_reference(self):
432-
"""Test that deepcopy keeps DEFAULT_EXCLUDE fields as shallow references."""
431+
def test_deepcopy_preserves_shallow_copy_fields_by_reference(self):
432+
"""Test that deepcopy keeps _SHALLOW_COPY_FIELDS fields as shallow references."""
433433
import copy
434434

435435
class NonCopyable:
436436
def __deepcopy__(self, memo):
437437
raise TypeError("cannot deepcopy")
438438

439439
class TestClass(SerializationMixin):
440-
DEFAULT_EXCLUDE = {"raw_representation", "other_opaque"}
440+
_SHALLOW_COPY_FIELDS = {"raw_representation", "other_opaque"}
441441

442442
def __init__(self, items: list, raw_representation: Any = None, other_opaque: Any = None):
443443
self.items = items
@@ -450,19 +450,19 @@ def __init__(self, items: list, raw_representation: Any = None, other_opaque: An
450450
obj = TestClass(items=original_items, raw_representation=raw, other_opaque=opaque)
451451
cloned = copy.deepcopy(obj)
452452

453-
# Excluded fields should be the same object (shallow copy)
453+
# _SHALLOW_COPY_FIELDS fields should be the same object (shallow copy)
454454
assert cloned.raw_representation is raw
455455
assert cloned.other_opaque is opaque
456456
# Normal attributes should be independent copies
457457
assert cloned.items is not original_items
458458
assert cloned.items == ["a", "b"]
459459

460-
def test_deepcopy_deep_copies_non_excluded_fields(self):
461-
"""Test that deepcopy fully copies fields not in DEFAULT_EXCLUDE."""
460+
def test_deepcopy_deep_copies_non_shallow_copy_fields(self):
461+
"""Test that deepcopy fully copies fields not in _SHALLOW_COPY_FIELDS."""
462462
import copy
463463

464464
class TestClass(SerializationMixin):
465-
DEFAULT_EXCLUDE = {"raw_representation"}
465+
_SHALLOW_COPY_FIELDS = {"raw_representation"}
466466

467467
def __init__(self, items: list, raw_representation: Any = None):
468468
self.items = items
@@ -477,3 +477,22 @@ def __init__(self, items: list, raw_representation: Any = None):
477477
assert cloned.items == ["a", "b"]
478478
# raw_representation should be the same object
479479
assert cloned.raw_representation is obj.raw_representation
480+
481+
def test_deepcopy_deep_copies_default_exclude_fields(self):
482+
"""Test that DEFAULT_EXCLUDE fields are deep-copied unless also in _SHALLOW_COPY_FIELDS."""
483+
import copy
484+
485+
class TestClass(SerializationMixin):
486+
DEFAULT_EXCLUDE = {"additional_properties"}
487+
488+
def __init__(self, items: list, additional_properties: dict | None = None):
489+
self.items = items
490+
self.additional_properties = additional_properties or {}
491+
492+
original_props = {"key": "value"}
493+
obj = TestClass(items=["a"], additional_properties=original_props)
494+
cloned = copy.deepcopy(obj)
495+
496+
# DEFAULT_EXCLUDE field should be deep-copied (independent copy)
497+
assert cloned.additional_properties is not original_props
498+
assert cloned.additional_properties == {"key": "value"}

‎python/packages/core/tests/core/test_types.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,40 @@ def test_nested_deepcopy_preserves_raw_representation():
19791979
assert cloned.text == "hello"
19801980

19811981

1982+
def test_content_deepcopy_shallow_copy_fields_identity():
1983+
"""Test that Content._SHALLOW_COPY_FIELDS fields are identity-preserved while others are deep-copied."""
1984+
import copy
1985+
1986+
raw = _NonCopyableRaw()
1987+
content = Content.from_text("hello", raw_representation=raw)
1988+
content.additional_properties["key"] = "value"
1989+
1990+
cloned = copy.deepcopy(content)
1991+
1992+
# _SHALLOW_COPY_FIELDS (raw_representation) should be same object
1993+
assert cloned.raw_representation is raw
1994+
# Non-shallow fields should be independent deep copies
1995+
assert cloned.additional_properties is not content.additional_properties
1996+
assert cloned.additional_properties == {"key": "value"}
1997+
1998+
1999+
def test_chat_response_deepcopy_deep_copies_additional_properties():
2000+
"""Test that ChatResponse deepcopy deep-copies additional_properties despite it being in DEFAULT_EXCLUDE."""
2001+
import copy
2002+
2003+
response = ChatResponse(
2004+
messages=[Message("assistant", ["test"])],
2005+
additional_properties={"key": [1, 2, 3]},
2006+
)
2007+
2008+
cloned = copy.deepcopy(response)
2009+
2010+
# additional_properties is in DEFAULT_EXCLUDE for serialization but not in _SHALLOW_COPY_FIELDS,
2011+
# so it should be deep-copied (independent copy)
2012+
assert cloned.additional_properties is not response.additional_properties
2013+
assert cloned.additional_properties == {"key": [1, 2, 3]}
2014+
2015+
19822016
# endregion
19832017

19842018

0 commit comments

Comments
 (0)