Skip to content

Commit dd58935

Browse files
committed
Done suggested change
1 parent 83787d9 commit dd58935

File tree

1 file changed

+23
-18
lines changed
  • packages/opentelemetry-instrumentation-openai_agents/opentelemetry/instrumentation/openai_agents

1 file changed

+23
-18
lines changed

packages/opentelemetry-instrumentation-openai_agents/opentelemetry/instrumentation/openai_agents/patch.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,40 +64,45 @@ def set_prompt_attributes(span, message_history):
6464
if not message_history:
6565
return
6666

67-
for msg in message_history:
67+
for i, msg in enumerate(message_history):
6868
if isinstance(msg, dict):
69-
7069
role = msg.get("role", "user")
7170
content = msg.get("content", "")
72-
73-
span.set_attribute(f"{SpanAttributes.LLM_PROMPTS}.role", role)
74-
span.set_attribute(f"{SpanAttributes.LLM_PROMPTS}.content", content)
71+
span.set_attribute(f"{SpanAttributes.LLM_PROMPTS}.{i}.role", role)
72+
span.set_attribute(f"{SpanAttributes.LLM_PROMPTS}.{i}.content",
73+
content)
7574

7675

7776
def set_response_content_span_attribute(response, span):
78-
7977
if hasattr(response, "output") and isinstance(response.output, list):
78+
roles = []
79+
types = []
80+
contents = []
81+
8082
for output_message in response.output:
81-
# Extract role and type from output_message
8283
role = getattr(output_message, "role", None)
8384
msg_type = getattr(output_message, "type", None)
8485

8586
if role:
86-
span.set_attribute(
87-
f"{SpanAttributes.LLM_COMPLETIONS}.role", role)
87+
roles.append(role)
8888
if msg_type:
89-
span.set_attribute(
90-
f"{SpanAttributes.LLM_COMPLETIONS}.type", msg_type)
89+
types.append(msg_type)
9190

92-
if hasattr(output_message, "content") and isinstance(
93-
output_message.content, list
94-
):
91+
if hasattr(output_message, "content") and \
92+
isinstance(output_message.content, list):
9593
for content_item in output_message.content:
9694
if hasattr(content_item, "text"):
97-
span.set_attribute(
98-
f"{SpanAttributes.LLM_COMPLETIONS}.content",
99-
content_item.text,
100-
)
95+
contents.append(content_item.text)
96+
97+
if roles:
98+
span.set_attribute(f"{SpanAttributes.LLM_COMPLETIONS}.roles",
99+
roles)
100+
if types:
101+
span.set_attribute(f"{SpanAttributes.LLM_COMPLETIONS}.types",
102+
types)
103+
if contents:
104+
span.set_attribute(f"{SpanAttributes.LLM_COMPLETIONS}.contents",
105+
contents)
101106

102107

103108
def set_token_usage_span_attributes(response, span):

0 commit comments

Comments
 (0)