Skip to content

Commit f1e7c5d

Browse files
CopilotnikhilNava
andcommitted
Make record_output_messages append messages instead of replacing
Changed the behavior of record_output_messages to append new messages to the accumulated list rather than replacing all messages. This allows collecting output messages over multiple calls during the scope's lifetime. Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
1 parent 2274828 commit f1e7c5d

2 files changed

Lines changed: 42 additions & 9 deletions

File tree

libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/spans_scopes/output_scope.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,19 @@ def __init__(
6060
parent_id=parent_id,
6161
)
6262

63+
# Initialize accumulated messages list
64+
self._output_messages: list[str] = list(response.messages)
65+
6366
# Set response messages
64-
self.set_tag_maybe(GEN_AI_OUTPUT_MESSAGES_KEY, safe_json_dumps(response.messages))
67+
self.set_tag_maybe(GEN_AI_OUTPUT_MESSAGES_KEY, safe_json_dumps(self._output_messages))
6568

6669
def record_output_messages(self, messages: list[str]) -> None:
6770
"""Records the output messages for telemetry tracking.
6871
72+
Appends the provided messages to the accumulated output messages list.
73+
6974
Args:
70-
messages: List of output messages
75+
messages: List of output messages to append
7176
"""
72-
self.set_tag_maybe(GEN_AI_OUTPUT_MESSAGES_KEY, safe_json_dumps(messages))
77+
self._output_messages.extend(messages)
78+
self.set_tag_maybe(GEN_AI_OUTPUT_MESSAGES_KEY, safe_json_dumps(self._output_messages))

tests/observability/core/test_output_scope.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ def test_multiple_output_messages(self):
141141
self.assertIn("Third response", output_value)
142142

143143
def test_record_output_messages_updates_span(self):
144-
"""Test that record_output_messages updates the span with new messages."""
144+
"""Test that record_output_messages appends messages to the span."""
145145
response = Response(messages=["Initial message"])
146146

147147
scope = OutputScope.start(self.agent_details, self.tenant_details, response)
148148

149149
if scope is not None:
150-
# Record updated messages
151-
scope.record_output_messages(["Updated message 1", "Updated message 2"])
150+
# Record additional messages (should append, not replace)
151+
scope.record_output_messages(["Appended message 1", "Appended message 2"])
152152
scope.dispose()
153153

154154
finished_spans = self.span_exporter.get_finished_spans()
@@ -163,10 +163,37 @@ def test_record_output_messages_updates_span(self):
163163
"Expected output messages key to be set on span",
164164
)
165165

166-
# The span should have the updated messages
166+
# The span should have all messages (initial + appended)
167167
output_value = span_attributes[GEN_AI_OUTPUT_MESSAGES_KEY]
168-
self.assertIn("Updated message 1", output_value)
169-
self.assertIn("Updated message 2", output_value)
168+
self.assertIn("Initial message", output_value)
169+
self.assertIn("Appended message 1", output_value)
170+
self.assertIn("Appended message 2", output_value)
171+
172+
def test_record_output_messages_multiple_appends(self):
173+
"""Test that multiple calls to record_output_messages accumulate messages."""
174+
response = Response(messages=["First message"])
175+
176+
scope = OutputScope.start(self.agent_details, self.tenant_details, response)
177+
178+
if scope is not None:
179+
# First append
180+
scope.record_output_messages(["Second message"])
181+
# Second append
182+
scope.record_output_messages(["Third message", "Fourth message"])
183+
scope.dispose()
184+
185+
finished_spans = self.span_exporter.get_finished_spans()
186+
self.assertTrue(finished_spans, "Expected at least one span to be created")
187+
188+
span = finished_spans[-1]
189+
span_attributes = getattr(span, "attributes", {}) or {}
190+
191+
output_value = span_attributes[GEN_AI_OUTPUT_MESSAGES_KEY]
192+
# All four messages should be present
193+
self.assertIn("First message", output_value)
194+
self.assertIn("Second message", output_value)
195+
self.assertIn("Third message", output_value)
196+
self.assertIn("Fourth message", output_value)
170197

171198
def test_output_scope_context_manager(self):
172199
"""Test that OutputScope works as a context manager."""

0 commit comments

Comments
 (0)