Skip to content

Commit 49b384c

Browse files
committed
chore(trace): updated semantic conventions with tool mappings
1 parent ad87f9e commit 49b384c

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/strands/telemetry/tracer.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ def start_tool_call_span(self, tool: ToolUse, parent_span: Optional[Span] = None
371371
"type": "tool_call",
372372
"name": tool["name"],
373373
"id": tool["toolUseId"],
374-
"arguments": [{"content": tool["input"]}],
374+
"arguments": {"content": tool["input"]},
375375
}
376376
],
377377
}
@@ -727,7 +727,7 @@ def _add_event_messages(self, span: Span, messages: Messages) -> None:
727727
input_messages: list = []
728728
for message in messages:
729729
input_messages.append(
730-
{"role": message["role"], "parts": [{"type": "text", "content": message["content"]}]}
730+
{"role": message["role"], "parts": self._map_content_blocks_to_otel_parts(message["content"])}
731731
)
732732
self._add_event(
733733
span, "gen_ai.client.inference.operation.details", {"gen_ai.input.messages": serialize(input_messages)}
@@ -740,6 +740,41 @@ def _add_event_messages(self, span: Span, messages: Messages) -> None:
740740
{"content": serialize(message["content"])},
741741
)
742742

743+
def _map_content_blocks_to_otel_parts(self, content_blocks: list[ContentBlock]) -> list[dict[str, Any]]:
744+
"""Map ContentBlock objects to OpenTelemetry parts format."""
745+
parts: list[dict[str, Any]] = []
746+
747+
for block in content_blocks:
748+
if "text" in block:
749+
# Standard TextPart
750+
parts.append({"type": "text", "content": block["text"]})
751+
elif "toolUse" in block:
752+
# Standard ToolCallRequestPart
753+
tool_use = block["toolUse"]
754+
parts.append(
755+
{
756+
"type": "tool_call",
757+
"name": tool_use["name"],
758+
"id": tool_use["toolUseId"],
759+
"arguments": {"content": tool_use["input"]},
760+
}
761+
)
762+
elif "toolResult" in block:
763+
# Standard ToolCallResponsePart
764+
tool_result = block["toolResult"]
765+
parts.append(
766+
{
767+
"type": "tool_call_response",
768+
"id": tool_result["toolUseId"],
769+
"response": tool_result["content"],
770+
}
771+
)
772+
else:
773+
# For all other ContentBlock types, use the key as type and value as content
774+
for key, value in block.items():
775+
parts.append({"type": key, "content": value})
776+
return parts
777+
743778

744779
# Singleton instance for global access
745780
_tracer_instance = None

tests/strands/telemetry/test_tracer.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def test_start_model_invoke_span_latest_conventions(mock_tracer):
191191
[
192192
{
193193
"role": messages[0]["role"],
194-
"parts": [{"type": "text", "content": messages[0]["content"]}],
194+
"parts": [{"type": "text", "content": "Hello"}],
195195
}
196196
]
197197
)
@@ -324,7 +324,7 @@ def test_start_tool_call_span_latest_conventions(mock_tracer):
324324
"type": "tool_call",
325325
"name": tool["name"],
326326
"id": tool["toolUseId"],
327-
"arguments": [{"content": tool["input"]}],
327+
"arguments": {"content": tool["input"]},
328328
}
329329
],
330330
}
@@ -564,9 +564,7 @@ def test_start_event_loop_cycle_span_latest_conventions(mock_tracer):
564564
mock_span.add_event.assert_any_call(
565565
"gen_ai.client.inference.operation.details",
566566
attributes={
567-
"gen_ai.input.messages": serialize(
568-
[{"role": "user", "parts": [{"type": "text", "content": messages[0]["content"]}]}]
569-
)
567+
"gen_ai.input.messages": serialize([{"role": "user", "parts": [{"type": "text", "content": "Hello"}]}])
570568
},
571569
)
572570
assert span is not None
@@ -682,7 +680,7 @@ def test_start_agent_span_latest_conventions(mock_tracer):
682680
"gen_ai.client.inference.operation.details",
683681
attributes={
684682
"gen_ai.input.messages": serialize(
685-
[{"role": "user", "parts": [{"type": "text", "content": [{"text": "test prompt"}]}]}]
683+
[{"role": "user", "parts": [{"type": "text", "content": "test prompt"}]}]
686684
)
687685
},
688686
)

0 commit comments

Comments
 (0)