48
48
)
49
49
from opentelemetry .semconv .attributes import error_attributes
50
50
from opentelemetry .trace .span import Span
51
- from opentelemetry .util .genai .types import ContentCapturingMode , MessagePart
51
+ from opentelemetry .util .genai .types import (
52
+ ContentCapturingMode ,
53
+ MessagePart ,
54
+ InputMessage ,
55
+ OutputMessage ,
56
+ )
52
57
from opentelemetry .util .genai .upload_hook import load_upload_hook
53
58
54
59
from .allowlist_util import AllowList
55
60
from .custom_semconv import GCP_GENAI_OPERATION_CONFIG
56
61
from .dict_util import flatten_dict
57
62
from .flags import is_content_recording_enabled
58
63
from .message import (
59
- InputMessage ,
60
- OutputMessage ,
61
64
to_input_messages ,
62
65
to_output_messages ,
63
66
to_system_instructions ,
@@ -263,11 +266,17 @@ def _create_completion_details_attributes(
263
266
as_str : bool = False ,
264
267
) -> dict [str , Any ]:
265
268
attributes : dict [str , Any ] = {
266
- "gen_ai.input.messages" : [dataclasses .asdict (input_message ) for input_message in input_messages ],
267
- "gen_ai.output.messages" : [dataclasses .asdict (output_message ) for output_message in output_messages ],
269
+ gen_ai_attributes .GEN_AI_INPUT_MESSAGES : [
270
+ dataclasses .asdict (input_message ) for input_message in input_messages
271
+ ],
272
+ gen_ai_attributes .GEN_AI_OUTPUT_MESSAGES : [
273
+ dataclasses .asdict (output_message ) for output_message in output_messages
274
+ ],
268
275
}
269
276
if system_instructions :
270
- attributes ["gen_ai.system.instructions" ] = [dataclasses .asdict (sys_instr ) for sys_instr in system_instructions ]
277
+ attributes [gen_ai_attributes .GEN_AI_SYSTEM_INSTRUCTIONS ] = [
278
+ dataclasses .asdict (sys_instr ) for sys_instr in system_instructions
279
+ ]
271
280
272
281
if as_str :
273
282
return {k : json .dumps (v ) for k , v in attributes .items ()}
@@ -455,15 +464,16 @@ def _maybe_log_completion_details(
455
464
input_messages = to_input_messages (contents = transformers .t_contents (request ))
456
465
output_messages = to_output_messages (candidates = response .candidates or [])
457
466
458
-
459
-
460
467
span = None
461
468
if self ._content_recording_enabled in [
462
469
ContentCapturingMode .SPAN_ONLY ,
463
470
ContentCapturingMode .SPAN_AND_EVENT ,
464
471
]:
465
472
completion_details_attributes = _create_completion_details_attributes (
466
- input_messages , output_messages , system_instructions , as_str = True ,
473
+ input_messages ,
474
+ output_messages ,
475
+ system_instructions ,
476
+ as_str = True ,
467
477
)
468
478
span = trace .get_current_span ()
469
479
span .set_attributes (completion_details_attributes )
@@ -472,10 +482,14 @@ def _maybe_log_completion_details(
472
482
ContentCapturingMode .SPAN_AND_EVENT ,
473
483
]:
474
484
completion_details_attributes = _create_completion_details_attributes (
475
- input_messages , output_messages , system_instructions ,
485
+ input_messages ,
486
+ output_messages ,
487
+ system_instructions ,
476
488
)
477
489
attributes .update (completion_details_attributes )
478
- event = Event (name = "gen_ai.client.inference.operation.details" , attributes = attributes )
490
+ event = Event (
491
+ name = "gen_ai.client.inference.operation.details" , attributes = attributes
492
+ )
479
493
hook = load_upload_hook ()
480
494
hook .upload (
481
495
inputs = input_messages ,
0 commit comments