Skip to content

Commit e0ee125

Browse files
committed
unify handler start/stop/fail
1 parent 25483cb commit e0ee125

1 file changed

Lines changed: 43 additions & 15 deletions

File tree

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

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

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
AgentCreation,
3636
AgentInvocation,
3737
Error,
38-
GenAIInvocation,
3938
InputMessage,
4039
LLMInvocation,
4140
MessagePart,
@@ -72,17 +71,9 @@ def _get_llm_common_attributes(
7271
}
7372

7473

75-
def _get_span_name(
76-
invocation: GenAIInvocation,
77-
) -> str:
78-
"""Get the span name for a GenAI invocation."""
79-
request_model = getattr(invocation, "request_model", None) or ""
80-
return f"{invocation.operation_name} {request_model}".strip()
81-
82-
8374
def _get_llm_span_name(invocation: LLMInvocation) -> str:
8475
"""Get the span name for an LLM invocation."""
85-
return _get_span_name(invocation)
76+
return f"{invocation.operation_name} {invocation.request_model}".strip()
8677

8778

8879
def _get_llm_messages_attributes_for_span(
@@ -266,8 +257,18 @@ def _get_request_attributes(
266257
def _get_llm_request_attributes(
267258
invocation: LLMInvocation,
268259
) -> dict[str, Any]:
269-
"""Get GenAI request semantic convention attributes for LLM invocations."""
270-
return _get_request_attributes(invocation)
260+
"""Get GenAI request semantic convention attributes."""
261+
optional_attrs = (
262+
(GenAI.GEN_AI_REQUEST_TEMPERATURE, invocation.temperature),
263+
(GenAI.GEN_AI_REQUEST_TOP_P, invocation.top_p),
264+
(GenAI.GEN_AI_REQUEST_FREQUENCY_PENALTY, invocation.frequency_penalty),
265+
(GenAI.GEN_AI_REQUEST_PRESENCE_PENALTY, invocation.presence_penalty),
266+
(GenAI.GEN_AI_REQUEST_MAX_TOKENS, invocation.max_tokens),
267+
(GenAI.GEN_AI_REQUEST_STOP_SEQUENCES, invocation.stop_sequences),
268+
(GenAI.GEN_AI_REQUEST_SEED, invocation.seed),
269+
)
270+
271+
return {key: value for key, value in optional_attrs if value is not None}
271272

272273

273274
def _get_response_attributes(
@@ -305,8 +306,36 @@ def _get_response_attributes(
305306
def _get_llm_response_attributes(
306307
invocation: LLMInvocation,
307308
) -> dict[str, Any]:
308-
"""Get GenAI response semantic convention attributes for LLM invocations."""
309-
return _get_response_attributes(invocation)
309+
"""Get GenAI response semantic convention attributes."""
310+
finish_reasons: list[str] | None
311+
if invocation.finish_reasons is not None:
312+
finish_reasons = invocation.finish_reasons
313+
elif invocation.output_messages:
314+
finish_reasons = [
315+
message.finish_reason
316+
for message in invocation.output_messages
317+
if message.finish_reason
318+
]
319+
else:
320+
finish_reasons = None
321+
322+
# De-duplicate finish reasons
323+
unique_finish_reasons = (
324+
sorted(set(finish_reasons)) if finish_reasons else None
325+
)
326+
327+
optional_attrs = (
328+
(
329+
GenAI.GEN_AI_RESPONSE_FINISH_REASONS,
330+
unique_finish_reasons if unique_finish_reasons else None,
331+
),
332+
(GenAI.GEN_AI_RESPONSE_MODEL, invocation.response_model_name),
333+
(GenAI.GEN_AI_RESPONSE_ID, invocation.response_id),
334+
(GenAI.GEN_AI_USAGE_INPUT_TOKENS, invocation.input_tokens),
335+
(GenAI.GEN_AI_USAGE_OUTPUT_TOKENS, invocation.output_tokens),
336+
)
337+
338+
return {key: value for key, value in optional_attrs if value is not None}
310339

311340

312341
def _get_base_agent_common_attributes(
@@ -440,7 +469,6 @@ def _apply_creation_finish_attributes(
440469
"_apply_llm_finish_attributes",
441470
"_apply_error_attributes",
442471
"_get_llm_common_attributes",
443-
"_get_span_name",
444472
"_get_request_attributes",
445473
"_get_llm_request_attributes",
446474
"_get_response_attributes",

0 commit comments

Comments
 (0)