@@ -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
0 commit comments