Skip to content

Commit e35f530

Browse files
moonbox3Copilot
andauthored
Python: Fix executor_completed event with non-copyable raw_representation in mixed workflows (#4493)
* Python: Fix `executor_completed` event with non-copyable raw_representation in mixed workflows Fixes #4455 * fix(#4455): use class-level sets for deepcopy field exclusion - SerializationMixin.__deepcopy__: check type(self).DEFAULT_EXCLUDE instead of hardcoding 'raw_representation' - Content.__deepcopy__: add _SHALLOW_COPY_FIELDS class variable and check against it instead of hardcoding - Fix tautological assertion in test (was always True) - Add second excluded field to test to verify DEFAULT_EXCLUDE is respected generically Co-authored-by: Copilot <[email protected]> * Decouple __deepcopy__ from DEFAULT_EXCLUDE in SerializationMixin (#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]> * Add test for _SHALLOW_COPY_FIELDS and DEFAULT_EXCLUDE independence Add test_deepcopy_shallow_copy_fields_override_default_exclude to verify that a field in both DEFAULT_EXCLUDE and _SHALLOW_COPY_FIELDS is shallow-copied (controlled by _SHALLOW_COPY_FIELDS), while a field in DEFAULT_EXCLUDE only is still deep-copied. This addresses review comment #11 ensuring the two class variables control independent concerns. Co-authored-by: Copilot <[email protected]> * Remove unnecessary local variable in __deepcopy__ Inline cls._SHALLOW_COPY_FIELDS directly in the loop check instead of assigning to a local variable first, per review feedback. Co-authored-by: Copilot <[email protected]> * Apply pre-commit auto-fixes --------- Co-authored-by: Copilot <[email protected]>
1 parent a3bfad4 commit e35f530

5 files changed

Lines changed: 360 additions & 0 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import copy
56
import json
67
import logging
78
import re
@@ -263,6 +264,25 @@ def __init__(self, **kwargs):
263264

264265
DEFAULT_EXCLUDE: ClassVar[set[str]] = set()
265266
INJECTABLE: ClassVar[set[str]] = set()
267+
_SHALLOW_COPY_FIELDS: ClassVar[set[str]] = {"raw_representation"}
268+
269+
def __deepcopy__(self, memo: dict[int, Any]) -> SerializationMixin:
270+
"""Create a deep copy, preserving ``_SHALLOW_COPY_FIELDS`` by reference.
271+
272+
Fields listed in ``_SHALLOW_COPY_FIELDS`` may contain LLM SDK objects
273+
(e.g., proto/gRPC responses) that are not safe to deep-copy. They are
274+
kept as shallow references in the copy; all other attributes are
275+
deep-copied normally.
276+
"""
277+
cls = type(self)
278+
result = cls.__new__(cls)
279+
memo[id(self)] = result
280+
for k, v in self.__dict__.items():
281+
if k in cls._SHALLOW_COPY_FIELDS:
282+
object.__setattr__(result, k, v)
283+
else:
284+
object.__setattr__(result, k, copy.deepcopy(v, memo))
285+
return result
266286

267287
def to_dict(self, *, exclude: set[str] | None = None, exclude_none: bool = True) -> dict[str, Any]:
268288
"""Convert the instance and any nested objects to a dictionary.

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ class Content:
445445
`Content.from_uri()`, etc. to create instances.
446446
"""
447447

448+
_SHALLOW_COPY_FIELDS: ClassVar[set[str]] = {"raw_representation"}
449+
448450
def __init__(
449451
self,
450452
type: ContentType,
@@ -546,6 +548,23 @@ def __init__(
546548
self.approved = approved
547549
self.consent_link = consent_link
548550

551+
def __deepcopy__(self, memo: dict[int, Any]) -> Content:
552+
"""Create a deep copy, preserving ``_SHALLOW_COPY_FIELDS`` by reference.
553+
554+
Fields listed in ``_SHALLOW_COPY_FIELDS`` may contain LLM SDK objects
555+
(e.g., proto/gRPC responses) that are not safe to deep-copy.
556+
"""
557+
cls = type(self)
558+
result = cls.__new__(cls)
559+
memo[id(self)] = result
560+
shallow = cls._SHALLOW_COPY_FIELDS
561+
for k, v in self.__dict__.items():
562+
if k in shallow:
563+
object.__setattr__(result, k, v)
564+
else:
565+
object.__setattr__(result, k, deepcopy(v, memo))
566+
return result
567+
549568
@classmethod
550569
def from_text(
551570
cls: type[ContentT],

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,103 @@ def __init__(self, value: str, options: dict | None = None):
427427

428428
assert obj.options["existing"] == "value"
429429
assert obj.options["injected"] == "option"
430+
431+
def test_deepcopy_preserves_shallow_copy_fields_by_reference(self):
432+
"""Test that deepcopy keeps _SHALLOW_COPY_FIELDS fields as shallow references."""
433+
import copy
434+
435+
class NonCopyable:
436+
def __deepcopy__(self, memo):
437+
raise TypeError("cannot deepcopy")
438+
439+
class TestClass(SerializationMixin):
440+
_SHALLOW_COPY_FIELDS = {"raw_representation", "other_opaque"}
441+
442+
def __init__(self, items: list, raw_representation: Any = None, other_opaque: Any = None):
443+
self.items = items
444+
self.raw_representation = raw_representation
445+
self.other_opaque = other_opaque
446+
447+
raw = NonCopyable()
448+
opaque = NonCopyable()
449+
original_items = ["a", "b"]
450+
obj = TestClass(items=original_items, raw_representation=raw, other_opaque=opaque)
451+
cloned = copy.deepcopy(obj)
452+
453+
# _SHALLOW_COPY_FIELDS fields should be the same object (shallow copy)
454+
assert cloned.raw_representation is raw
455+
assert cloned.other_opaque is opaque
456+
# Normal attributes should be independent copies
457+
assert cloned.items is not original_items
458+
assert cloned.items == ["a", "b"]
459+
460+
def test_deepcopy_deep_copies_non_shallow_copy_fields(self):
461+
"""Test that deepcopy fully copies fields not in _SHALLOW_COPY_FIELDS."""
462+
import copy
463+
464+
class TestClass(SerializationMixin):
465+
_SHALLOW_COPY_FIELDS = {"raw_representation"}
466+
467+
def __init__(self, items: list, raw_representation: Any = None):
468+
self.items = items
469+
self.raw_representation = raw_representation
470+
471+
original_list = ["a", "b"]
472+
obj = TestClass(items=original_list, raw_representation="raw")
473+
cloned = copy.deepcopy(obj)
474+
475+
# list should be a new object
476+
assert cloned.items is not original_list
477+
assert cloned.items == ["a", "b"]
478+
# raw_representation should be the same object
479+
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"}
499+
500+
def test_deepcopy_shallow_copy_fields_override_default_exclude(self):
501+
"""Test that _SHALLOW_COPY_FIELDS controls deepcopy independently of DEFAULT_EXCLUDE."""
502+
import copy
503+
504+
class NonCopyable:
505+
def __deepcopy__(self, memo):
506+
raise TypeError("cannot deepcopy")
507+
508+
class TestClass(SerializationMixin):
509+
DEFAULT_EXCLUDE = {"opaque", "additional_properties"}
510+
_SHALLOW_COPY_FIELDS = {"opaque"}
511+
512+
def __init__(self, items: list, opaque: Any = None, additional_properties: dict | None = None):
513+
self.items = items
514+
self.opaque = opaque
515+
self.additional_properties = additional_properties or {}
516+
517+
opaque = NonCopyable()
518+
original_props = {"key": "value"}
519+
obj = TestClass(items=["a"], opaque=opaque, additional_properties=original_props)
520+
cloned = copy.deepcopy(obj)
521+
522+
# Field in both DEFAULT_EXCLUDE and _SHALLOW_COPY_FIELDS: shallow-copied
523+
assert cloned.opaque is opaque
524+
# Field in DEFAULT_EXCLUDE only: deep-copied
525+
assert cloned.additional_properties is not original_props
526+
assert cloned.additional_properties == {"key": "value"}
527+
# Normal field: deep-copied
528+
assert cloned.items is not obj.items
529+
assert cloned.items == ["a"]

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

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,170 @@ def test_agent_run_response_update_all_content_types():
18601860
assert update_str.role == "user"
18611861

18621862

1863+
# region DeepCopy
1864+
1865+
1866+
class _NonCopyableRaw:
1867+
"""Simulates an LLM SDK response object that cannot be deep-copied (e.g., proto/gRPC)."""
1868+
1869+
def __deepcopy__(self, memo: dict) -> Any:
1870+
raise TypeError("Cannot deepcopy this object")
1871+
1872+
1873+
def test_content_deepcopy_preserves_raw_representation():
1874+
"""Test that deepcopy of Content keeps raw_representation by reference."""
1875+
import copy
1876+
1877+
raw = _NonCopyableRaw()
1878+
content = Content.from_text("hello", raw_representation=raw)
1879+
1880+
cloned = copy.deepcopy(content)
1881+
1882+
assert cloned.text == "hello"
1883+
assert cloned.raw_representation is raw
1884+
assert cloned.additional_properties is not content.additional_properties
1885+
1886+
1887+
def test_message_deepcopy_preserves_raw_representation():
1888+
"""Test that deepcopy of Message keeps raw_representation by reference."""
1889+
import copy
1890+
1891+
raw = _NonCopyableRaw()
1892+
msg = Message("assistant", ["hello"], raw_representation=raw)
1893+
1894+
cloned = copy.deepcopy(msg)
1895+
1896+
assert cloned.text == "hello"
1897+
assert cloned.raw_representation is raw
1898+
assert cloned.contents is not msg.contents
1899+
1900+
1901+
def test_agent_response_deepcopy_preserves_raw_representation():
1902+
"""Test that deepcopy of AgentResponse keeps raw_representation by reference."""
1903+
import copy
1904+
1905+
raw = _NonCopyableRaw()
1906+
response = AgentResponse(
1907+
messages=[Message("assistant", ["test"])],
1908+
raw_representation=raw,
1909+
)
1910+
1911+
cloned = copy.deepcopy(response)
1912+
1913+
assert cloned.text == "test"
1914+
assert cloned.raw_representation is raw
1915+
assert cloned.messages is not response.messages
1916+
1917+
1918+
def test_chat_response_deepcopy_preserves_raw_representation():
1919+
"""Test that deepcopy of ChatResponse keeps raw_representation by reference."""
1920+
import copy
1921+
1922+
raw = _NonCopyableRaw()
1923+
response = ChatResponse(
1924+
messages=[Message("assistant", ["test"])],
1925+
raw_representation=raw,
1926+
)
1927+
1928+
cloned = copy.deepcopy(response)
1929+
1930+
assert cloned.text == "test"
1931+
assert cloned.raw_representation is raw
1932+
assert cloned.messages is not response.messages
1933+
1934+
1935+
def test_chat_response_update_deepcopy_preserves_raw_representation():
1936+
"""Test that deepcopy of ChatResponseUpdate keeps raw_representation by reference."""
1937+
import copy
1938+
1939+
raw = _NonCopyableRaw()
1940+
update = ChatResponseUpdate(
1941+
contents=[Content.from_text("hello")],
1942+
role="assistant",
1943+
raw_representation=raw,
1944+
)
1945+
1946+
cloned = copy.deepcopy(update)
1947+
1948+
assert cloned.text == "hello"
1949+
assert cloned.raw_representation is raw
1950+
assert cloned.contents is not update.contents
1951+
1952+
1953+
def test_agent_response_update_deepcopy_preserves_raw_representation():
1954+
"""Test that deepcopy of AgentResponseUpdate keeps raw_representation by reference."""
1955+
import copy
1956+
1957+
raw = _NonCopyableRaw()
1958+
update = AgentResponseUpdate(
1959+
contents=[Content.from_text("hello")],
1960+
role="assistant",
1961+
raw_representation=raw,
1962+
)
1963+
1964+
cloned = copy.deepcopy(update)
1965+
1966+
assert cloned.text == "hello"
1967+
assert cloned.raw_representation is raw
1968+
assert cloned.contents is not update.contents
1969+
1970+
1971+
def test_nested_deepcopy_preserves_raw_representation():
1972+
"""Test that deepcopy of an AgentResponse with nested Message raw_representations works."""
1973+
import copy
1974+
1975+
raw_msg = _NonCopyableRaw()
1976+
raw_response = _NonCopyableRaw()
1977+
response = AgentResponse(
1978+
messages=[Message("assistant", ["hello"], raw_representation=raw_msg)],
1979+
raw_representation=raw_response,
1980+
)
1981+
1982+
cloned = copy.deepcopy(response)
1983+
1984+
assert cloned.raw_representation is raw_response
1985+
assert cloned.messages[0].raw_representation is raw_msg
1986+
assert cloned.messages is not response.messages
1987+
assert cloned.text == "hello"
1988+
1989+
1990+
def test_content_deepcopy_shallow_copy_fields_identity():
1991+
"""Test that Content._SHALLOW_COPY_FIELDS fields are identity-preserved while others are deep-copied."""
1992+
import copy
1993+
1994+
raw = _NonCopyableRaw()
1995+
content = Content.from_text("hello", raw_representation=raw)
1996+
content.additional_properties["key"] = "value"
1997+
1998+
cloned = copy.deepcopy(content)
1999+
2000+
# _SHALLOW_COPY_FIELDS (raw_representation) should be same object
2001+
assert cloned.raw_representation is raw
2002+
# Non-shallow fields should be independent deep copies
2003+
assert cloned.additional_properties is not content.additional_properties
2004+
assert cloned.additional_properties == {"key": "value"}
2005+
2006+
2007+
def test_chat_response_deepcopy_deep_copies_additional_properties():
2008+
"""Test that ChatResponse deepcopy deep-copies additional_properties despite it being in DEFAULT_EXCLUDE."""
2009+
import copy
2010+
2011+
response = ChatResponse(
2012+
messages=[Message("assistant", ["test"])],
2013+
additional_properties={"key": [1, 2, 3]},
2014+
)
2015+
2016+
cloned = copy.deepcopy(response)
2017+
2018+
# additional_properties is in DEFAULT_EXCLUDE for serialization but not in _SHALLOW_COPY_FIELDS,
2019+
# so it should be deep-copied (independent copy)
2020+
assert cloned.additional_properties is not response.additional_properties
2021+
assert cloned.additional_properties == {"key": [1, 2, 3]}
2022+
2023+
2024+
# endregion
2025+
2026+
18632027
# region Serialization
18642028

18652029

0 commit comments

Comments
 (0)