Skip to content

Commit 97ad071

Browse files
authored
Fix minor tracing issues in agents (#39752)
* Fix minor issues in agents telemetry * validate message status
1 parent 2294aa1 commit 97ad071

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Diff for: sdk/ai/azure-ai-projects/azure/ai/projects/telemetry/agents/_ai_agents_instrumentor.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def _create_event_attributes(
268268
attrs[GEN_AI_MESSAGE_ID] = message_id
269269

270270
if message_status:
271-
attrs[GEN_AI_MESSAGE_STATUS] = message_status
271+
attrs[GEN_AI_MESSAGE_STATUS] = self._status_to_string(message_status)
272272

273273
if usage:
274274
attrs[GEN_AI_USAGE_INPUT_TOKENS] = usage.prompt_tokens
@@ -287,7 +287,7 @@ def add_thread_message_event(
287287
content_details = {"value": self._get_field(typed_content, "value")}
288288
annotations = self._get_field(typed_content, "annotations")
289289
if annotations:
290-
content_details["annotations"] = annotations
290+
content_details["annotations"] = [a.as_dict() for a in annotations]
291291
content_body[content.type] = content_details
292292

293293
self._add_message_event(
@@ -386,6 +386,9 @@ def _get_role(self, role: Optional[Union[str, MessageRole]]) -> str:
386386

387387
return role
388388

389+
def _status_to_string(self, status: Any) -> str:
390+
return status.value if hasattr(status, "value") else status
391+
389392
def _add_tool_assistant_message_event(self, span, step: RunStep) -> None:
390393
# do we want a new event for it ?
391394
tool_calls = [
@@ -418,7 +421,7 @@ def _add_tool_assistant_message_event(self, span, step: RunStep) -> None:
418421

419422
def set_end_run(self, span: "AbstractSpan", run: Optional[ThreadRun]) -> None:
420423
if run and span and span.span_instance.is_recording:
421-
span.add_attribute(GEN_AI_THREAD_RUN_STATUS, run.status)
424+
span.add_attribute(GEN_AI_THREAD_RUN_STATUS, self._status_to_string(run.status))
422425
span.add_attribute(GEN_AI_RESPONSE_MODEL, run.model)
423426
if run and run.usage:
424427
span.add_attribute(GEN_AI_USAGE_INPUT_TOKENS, run.usage.prompt_tokens)
@@ -897,7 +900,7 @@ async def trace_create_run_async(self, operation_name, function, *args, **kwargs
897900
try:
898901
result = await function(*args, **kwargs)
899902
if span.span_instance.is_recording:
900-
span.add_attribute(GEN_AI_THREAD_RUN_STATUS, result.status)
903+
span.add_attribute(GEN_AI_THREAD_RUN_STATUS, self._status_to_string(result.status))
901904
span.add_attribute(GEN_AI_RESPONSE_MODEL, result.model)
902905
if result.usage:
903906
span.add_attribute(GEN_AI_USAGE_INPUT_TOKENS, result.usage.prompt_tokens)
@@ -1700,7 +1703,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
17001703
self.instrumentor.set_end_run(self.span, self.last_run)
17011704

17021705
if self.last_run and self.last_run.last_error:
1703-
self.span.set_status(
1706+
self.span.span_instance.set_status(
17041707
StatusCode.ERROR, # pyright: ignore [reportPossiblyUnboundVariable]
17051708
self.last_run.last_error.message,
17061709
)

Diff for: sdk/ai/azure-ai-projects/tests/telemetry/test_ai_agents_instrumentor.py

+2
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ def test_agent_chat_with_tracing_content_recording_enabled(self, **kwargs):
391391
"gen_ai.agent.id": "*",
392392
"gen_ai.thread.run.id": "*",
393393
"gen_ai.message.id": "*",
394+
"gen_ai.message.status": "completed",
394395
"gen_ai.event.content": '{"content": {"text": {"value": "*"}}, "role": "assistant"}',
395396
},
396397
},
@@ -1000,6 +1001,7 @@ def fetch_weather(location: str) -> str:
10001001
"gen_ai.agent.id": "*",
10011002
"gen_ai.thread.run.id": "*",
10021003
"gen_ai.message.id": "*",
1004+
"gen_ai.message.status": "completed",
10031005
"gen_ai.event.content": '{"role": "assistant"}',
10041006
},
10051007
},

0 commit comments

Comments
 (0)