Skip to content

Commit c6e9822

Browse files
committed
update span utils
1 parent f849547 commit c6e9822

1 file changed

Lines changed: 30 additions & 14 deletions

File tree

  • util/opentelemetry-util-genai/src/opentelemetry/util/genai

util/opentelemetry-util-genai/src/opentelemetry/util/genai/span_utils.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,32 @@ def _get_embedding_span_name(invocation: EmbeddingInvocation) -> str:
111111
return _get_span_name(invocation)
112112

113113

114+
def _get_system_instructions_for_span(
115+
system_instruction: list[MessagePart] | None = None,
116+
) -> dict[str, Any]:
117+
"""Get system instructions attribute formatted for span (JSON string format).
118+
119+
Can be used with agent/llm/tool invocations.
120+
Returns empty dict if not in experimental mode or content capturing is disabled.
121+
"""
122+
if (
123+
not is_experimental_mode()
124+
or get_content_capturing_mode()
125+
not in (
126+
ContentCapturingMode.SPAN_ONLY,
127+
ContentCapturingMode.SPAN_AND_EVENT,
128+
)
129+
or not system_instruction
130+
):
131+
return {}
132+
133+
return {
134+
GenAI.GEN_AI_SYSTEM_INSTRUCTIONS: gen_ai_json_dumps(
135+
[asdict(p) for p in system_instruction]
136+
)
137+
}
138+
139+
114140
def _get_llm_messages_attributes_for_span(
115141
input_messages: list[InputMessage],
116142
output_messages: list[OutputMessage],
@@ -507,20 +533,9 @@ def _apply_creation_finish_attributes(
507533
attributes: dict[str, Any] = {}
508534
attributes.update(_get_base_agent_common_attributes(creation))
509535

510-
# System instructions (Opt-In)
511-
if (
512-
is_experimental_mode()
513-
and get_content_capturing_mode()
514-
in (
515-
ContentCapturingMode.SPAN_ONLY,
516-
ContentCapturingMode.SPAN_AND_EVENT,
517-
)
518-
and creation.system_instruction
519-
):
520-
attributes[GenAI.GEN_AI_SYSTEM_INSTRUCTIONS] = gen_ai_json_dumps(
521-
[asdict(p) for p in creation.system_instruction]
522-
)
523-
536+
attributes.update(
537+
_get_system_instructions_for_span(creation.system_instruction)
538+
)
524539
attributes.update(creation.attributes)
525540

526541
if attributes:
@@ -544,6 +559,7 @@ def _apply_creation_finish_attributes(
544559
"_get_base_agent_span_name",
545560
"_apply_agent_finish_attributes",
546561
"_apply_creation_finish_attributes",
562+
"_get_system_instructions_for_span",
547563
"_get_agent_common_attributes",
548564
"_get_agent_request_attributes",
549565
"_get_agent_response_attributes",

0 commit comments

Comments
 (0)