Skip to content

Commit d2ac6a2

Browse files
committed
Python: Fix executor_completed event with non-copyable raw_representation in mixed workflows
Fixes #4455
1 parent 5ba1c6f commit d2ac6a2

5 files changed

Lines changed: 266 additions & 0 deletions

File tree

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

Lines changed: 18 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
@@ -264,6 +265,23 @@ def __init__(self, **kwargs):
264265
DEFAULT_EXCLUDE: ClassVar[set[str]] = set()
265266
INJECTABLE: ClassVar[set[str]] = set()
266267

268+
def __deepcopy__(self, memo: dict[int, Any]) -> SerializationMixin:
269+
"""Create a deep copy, preserving ``raw_representation`` by reference.
270+
271+
``raw_representation`` may contain LLM SDK objects (e.g., proto/gRPC
272+
responses) that are not safe to deep-copy. It is kept as a shallow
273+
reference in the copy; all other attributes are deep-copied normally.
274+
"""
275+
cls = type(self)
276+
result = cls.__new__(cls)
277+
memo[id(self)] = result
278+
for k, v in self.__dict__.items():
279+
if k == "raw_representation":
280+
object.__setattr__(result, k, v)
281+
else:
282+
object.__setattr__(result, k, copy.deepcopy(v, memo))
283+
return result
284+
267285
def to_dict(self, *, exclude: set[str] | None = None, exclude_none: bool = True) -> dict[str, Any]:
268286
"""Convert the instance and any nested objects to a dictionary.
269287

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,22 @@ def __init__(
551551
self.approved = approved
552552
self.consent_link = consent_link
553553

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

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,48 @@ 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_excluded_fields_by_reference(self):
432+
"""Test that deepcopy keeps DEFAULT_EXCLUDE 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+
DEFAULT_EXCLUDE = {"raw_representation"}
441+
442+
def __init__(self, value: str, raw_representation: Any = None):
443+
self.value = value
444+
self.raw_representation = raw_representation
445+
446+
raw = NonCopyable()
447+
obj = TestClass(value="hello", raw_representation=raw)
448+
cloned = copy.deepcopy(obj)
449+
450+
assert cloned.value == "hello"
451+
assert cloned.raw_representation is raw
452+
# Normal attributes should be independent copies
453+
assert cloned.value is not obj.value or cloned.value == obj.value
454+
455+
def test_deepcopy_deep_copies_non_excluded_fields(self):
456+
"""Test that deepcopy fully copies fields not in DEFAULT_EXCLUDE."""
457+
import copy
458+
459+
class TestClass(SerializationMixin):
460+
DEFAULT_EXCLUDE = {"raw_representation"}
461+
462+
def __init__(self, items: list, raw_representation: Any = None):
463+
self.items = items
464+
self.raw_representation = raw_representation
465+
466+
original_list = ["a", "b"]
467+
obj = TestClass(items=original_list, raw_representation="raw")
468+
cloned = copy.deepcopy(obj)
469+
470+
# list should be a new object
471+
assert cloned.items is not original_list
472+
assert cloned.items == ["a", "b"]
473+
# raw_representation should be the same object
474+
assert cloned.raw_representation is obj.raw_representation

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

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,136 @@ def test_agent_run_response_update_all_content_types():
18521852
assert update_str.role == "user"
18531853

18541854

1855+
# region DeepCopy
1856+
1857+
1858+
class _NonCopyableRaw:
1859+
"""Simulates an LLM SDK response object that cannot be deep-copied (e.g., proto/gRPC)."""
1860+
1861+
def __deepcopy__(self, memo: dict) -> Any:
1862+
raise TypeError("Cannot deepcopy this object")
1863+
1864+
1865+
def test_content_deepcopy_preserves_raw_representation():
1866+
"""Test that deepcopy of Content keeps raw_representation by reference."""
1867+
import copy
1868+
1869+
raw = _NonCopyableRaw()
1870+
content = Content.from_text("hello", raw_representation=raw)
1871+
1872+
cloned = copy.deepcopy(content)
1873+
1874+
assert cloned.text == "hello"
1875+
assert cloned.raw_representation is raw
1876+
assert cloned.additional_properties is not content.additional_properties
1877+
1878+
1879+
def test_message_deepcopy_preserves_raw_representation():
1880+
"""Test that deepcopy of Message keeps raw_representation by reference."""
1881+
import copy
1882+
1883+
raw = _NonCopyableRaw()
1884+
msg = Message("assistant", ["hello"], raw_representation=raw)
1885+
1886+
cloned = copy.deepcopy(msg)
1887+
1888+
assert cloned.text == "hello"
1889+
assert cloned.raw_representation is raw
1890+
assert cloned.contents is not msg.contents
1891+
1892+
1893+
def test_agent_response_deepcopy_preserves_raw_representation():
1894+
"""Test that deepcopy of AgentResponse keeps raw_representation by reference."""
1895+
import copy
1896+
1897+
raw = _NonCopyableRaw()
1898+
response = AgentResponse(
1899+
messages=[Message("assistant", ["test"])],
1900+
raw_representation=raw,
1901+
)
1902+
1903+
cloned = copy.deepcopy(response)
1904+
1905+
assert cloned.text == "test"
1906+
assert cloned.raw_representation is raw
1907+
assert cloned.messages is not response.messages
1908+
1909+
1910+
def test_chat_response_deepcopy_preserves_raw_representation():
1911+
"""Test that deepcopy of ChatResponse keeps raw_representation by reference."""
1912+
import copy
1913+
1914+
raw = _NonCopyableRaw()
1915+
response = ChatResponse(
1916+
messages=[Message("assistant", ["test"])],
1917+
raw_representation=raw,
1918+
)
1919+
1920+
cloned = copy.deepcopy(response)
1921+
1922+
assert cloned.text == "test"
1923+
assert cloned.raw_representation is raw
1924+
assert cloned.messages is not response.messages
1925+
1926+
1927+
def test_chat_response_update_deepcopy_preserves_raw_representation():
1928+
"""Test that deepcopy of ChatResponseUpdate keeps raw_representation by reference."""
1929+
import copy
1930+
1931+
raw = _NonCopyableRaw()
1932+
update = ChatResponseUpdate(
1933+
contents=[Content.from_text("hello")],
1934+
role="assistant",
1935+
raw_representation=raw,
1936+
)
1937+
1938+
cloned = copy.deepcopy(update)
1939+
1940+
assert cloned.text == "hello"
1941+
assert cloned.raw_representation is raw
1942+
assert cloned.contents is not update.contents
1943+
1944+
1945+
def test_agent_response_update_deepcopy_preserves_raw_representation():
1946+
"""Test that deepcopy of AgentResponseUpdate keeps raw_representation by reference."""
1947+
import copy
1948+
1949+
raw = _NonCopyableRaw()
1950+
update = AgentResponseUpdate(
1951+
contents=[Content.from_text("hello")],
1952+
role="assistant",
1953+
raw_representation=raw,
1954+
)
1955+
1956+
cloned = copy.deepcopy(update)
1957+
1958+
assert cloned.text == "hello"
1959+
assert cloned.raw_representation is raw
1960+
assert cloned.contents is not update.contents
1961+
1962+
1963+
def test_nested_deepcopy_preserves_raw_representation():
1964+
"""Test that deepcopy of an AgentResponse with nested Message raw_representations works."""
1965+
import copy
1966+
1967+
raw_msg = _NonCopyableRaw()
1968+
raw_response = _NonCopyableRaw()
1969+
response = AgentResponse(
1970+
messages=[Message("assistant", ["hello"], raw_representation=raw_msg)],
1971+
raw_representation=raw_response,
1972+
)
1973+
1974+
cloned = copy.deepcopy(response)
1975+
1976+
assert cloned.raw_representation is raw_response
1977+
assert cloned.messages[0].raw_representation is raw_msg
1978+
assert cloned.messages is not response.messages
1979+
assert cloned.text == "hello"
1980+
1981+
1982+
# endregion
1983+
1984+
18551985
# region Serialization
18561986

18571987

‎python/packages/core/tests/workflow/test_agent_executor.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,60 @@ async def test_agent_executor_run_with_messages_kwarg_does_not_raise() -> None:
408408
result = await workflow.run("hello", messages=["stale"])
409409
assert result is not None
410410
assert agent.call_count == 1
411+
412+
413+
class _NonCopyableRaw:
414+
"""Simulates an LLM SDK response object that cannot be deep-copied (e.g., proto/gRPC)."""
415+
416+
def __deepcopy__(self, memo: dict) -> Any:
417+
raise TypeError("Cannot deepcopy this object")
418+
419+
420+
class _AgentWithRawRepr(BaseAgent):
421+
"""Agent that returns responses with a non-copyable raw_representation."""
422+
423+
def __init__(self, raw: Any, **kwargs: Any):
424+
super().__init__(**kwargs)
425+
self._raw = raw
426+
427+
def run(
428+
self,
429+
messages: str | Message | list[str] | list[Message] | None = None,
430+
*,
431+
stream: bool = False,
432+
session: AgentSession | None = None,
433+
**kwargs: Any,
434+
) -> Awaitable[AgentResponse] | ResponseStream[AgentResponseUpdate, AgentResponse]:
435+
async def _run() -> AgentResponse:
436+
return AgentResponse(
437+
messages=[Message("assistant", [f"reply from {self.name}"])],
438+
raw_representation=self._raw,
439+
)
440+
441+
return _run()
442+
443+
444+
async def test_agent_executor_workflow_with_non_copyable_raw_representation() -> None:
445+
"""Workflow should complete when AgentResponse contains a raw_representation that cannot be deep-copied."""
446+
raw = _NonCopyableRaw()
447+
448+
agent_a = _AgentWithRawRepr(raw=raw, id="a", name="AgentA")
449+
agent_b = _CountingAgent(id="b", name="AgentB")
450+
451+
exec_a = AgentExecutor(agent_a, id="exec_a")
452+
exec_b = AgentExecutor(agent_b, id="exec_b")
453+
454+
workflow = SequentialBuilder(participants=[exec_a, exec_b]).build()
455+
events = await workflow.run("hello")
456+
457+
completed = [e for e in events if isinstance(e, WorkflowEvent) and e.type == "executor_completed"]
458+
completed_a = [e for e in completed if e.executor_id == "exec_a"]
459+
460+
assert len(completed_a) == 1
461+
assert completed_a[0].data is not None
462+
463+
# The yielded AgentResponse should preserve its raw_representation reference
464+
agent_responses = [d for d in completed_a[0].data if isinstance(d, AgentResponse)]
465+
assert len(agent_responses) > 0
466+
assert agent_responses[0].text == "reply from AgentA"
467+
assert agent_responses[0].raw_representation is raw

0 commit comments

Comments
 (0)