From e406a1759968e81649dbc9ad331b18bc804df908 Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Mon, 17 Aug 2026 21:59:21 +0100 Subject: [PATCH 1/2] fix(tracing): detach serialized trace metadata --- src/agents/tracing/traces.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/agents/tracing/traces.py b/src/agents/tracing/traces.py index a703acf317..adb7ac932a 100644 --- a/src/agents/tracing/traces.py +++ b/src/agents/tracing/traces.py @@ -2,6 +2,7 @@ import abc import contextvars +import copy import hashlib import threading from collections import OrderedDict @@ -178,7 +179,7 @@ def to_json(self, *, include_tracing_api_key: bool = False) -> dict[str, Any] | exported = self.export() if exported is None: return None - payload = dict(exported) + payload = copy.deepcopy(exported) if include_tracing_api_key and self.tracing_api_key: payload["tracing_api_key"] = self.tracing_api_key return payload From 53acf0a0bf46985d16625484eed273be95167f31 Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Mon, 17 Aug 2026 21:59:35 +0100 Subject: [PATCH 2/2] test(tracing): cover detached trace JSON metadata --- tests/test_trace_json_isolation.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/test_trace_json_isolation.py diff --git a/tests/test_trace_json_isolation.py b/tests/test_trace_json_isolation.py new file mode 100644 index 0000000000..07599d4852 --- /dev/null +++ b/tests/test_trace_json_isolation.py @@ -0,0 +1,20 @@ +from __future__ import annotations + +from agents.tracing.provider import SynchronousMultiTracingProcessor +from agents.tracing.traces import TraceImpl + + +def test_trace_to_json_detaches_nested_metadata() -> None: + trace = TraceImpl( + name="workflow", + trace_id="trace_test", + group_id=None, + metadata={"nested": {"value": "original"}}, + processor=SynchronousMultiTracingProcessor(), + ) + + payload = trace.to_json() + assert payload is not None + payload["metadata"]["nested"]["value"] = "changed" + + assert trace.metadata == {"nested": {"value": "original"}}