1919
2020from typing_extensions import deprecated
2121
22+ from opentelemetry ._logs import LogRecord
23+ from opentelemetry .context import get_current
2224from opentelemetry .semconv ._incubating .attributes import (
2325 gen_ai_attributes as GenAI ,
2426)
2527from opentelemetry .semconv .attributes import server_attributes
26-
27- from opentelemetry ._logs import LogRecord
28- from opentelemetry .context import get_current
29- from opentelemetry .trace import SpanKind
28+ from opentelemetry .trace import INVALID_SPAN , SpanKind
3029from opentelemetry .trace .propagation import set_span_in_context
31-
3230from opentelemetry .util .genai .types import (
3331 ContentCapturingMode ,
3432 Error ,
@@ -86,12 +84,20 @@ def __init__(
8684 metric_attributes : dict [str , Any ] | None = None ,
8785 ) -> None :
8886 """Use handler.start_inference(provider) or handler.inference(provider) instead of calling this directly."""
89- super ().__init__ (handler , attributes = attributes , metric_attributes = metric_attributes )
87+ super ().__init__ (
88+ handler , attributes = attributes , metric_attributes = metric_attributes
89+ )
9090 self .provider = provider
9191 self .request_model = request_model
92- self .input_messages : list [InputMessage ] = [] if input_messages is None else input_messages
93- self .output_messages : list [OutputMessage ] = [] if output_messages is None else output_messages
94- self .system_instruction : list [MessagePart ] = [] if system_instruction is None else system_instruction
92+ self .input_messages : list [InputMessage ] = (
93+ [] if input_messages is None else input_messages
94+ )
95+ self .output_messages : list [OutputMessage ] = (
96+ [] if output_messages is None else output_messages
97+ )
98+ self .system_instruction : list [MessagePart ] = (
99+ [] if system_instruction is None else system_instruction
100+ )
95101 self .response_model_name = response_model_name
96102 self .response_id = response_id
97103 self .finish_reasons = finish_reasons
@@ -106,7 +112,11 @@ def __init__(
106112 self .seed = seed
107113 self .server_address = server_address
108114 self .server_port = server_port
109- self ._span_name = f"{ self .operation_name } { request_model } " if request_model else self .operation_name
115+ self ._span_name = (
116+ f"{ self .operation_name } { request_model } "
117+ if request_model
118+ else self .operation_name
119+ )
110120 self ._span_kind = SpanKind .CLIENT
111121 handler ._start (self )
112122
@@ -115,9 +125,15 @@ def _get_message_attributes(self, *, for_span: bool) -> dict[str, Any]:
115125 return {}
116126 mode = get_content_capturing_mode ()
117127 allowed_modes = (
118- (ContentCapturingMode .SPAN_ONLY , ContentCapturingMode .SPAN_AND_EVENT )
128+ (
129+ ContentCapturingMode .SPAN_ONLY ,
130+ ContentCapturingMode .SPAN_AND_EVENT ,
131+ )
119132 if for_span
120- else (ContentCapturingMode .EVENT_ONLY , ContentCapturingMode .SPAN_AND_EVENT )
133+ else (
134+ ContentCapturingMode .EVENT_ONLY ,
135+ ContentCapturingMode .SPAN_AND_EVENT ,
136+ )
121137 )
122138 if mode not in allowed_modes :
123139 return {}
@@ -127,17 +143,38 @@ def serialize(items: list[Any]) -> Any:
127143 return gen_ai_json_dumps (dicts ) if for_span else dicts
128144
129145 optional_attrs = (
130- (GenAI .GEN_AI_INPUT_MESSAGES , serialize (self .input_messages ) if self .input_messages else None ),
131- (GenAI .GEN_AI_OUTPUT_MESSAGES , serialize (self .output_messages ) if self .output_messages else None ),
132- (GenAI .GEN_AI_SYSTEM_INSTRUCTIONS , serialize (self .system_instruction ) if self .system_instruction else None ),
146+ (
147+ GenAI .GEN_AI_INPUT_MESSAGES ,
148+ serialize (self .input_messages )
149+ if self .input_messages
150+ else None ,
151+ ),
152+ (
153+ GenAI .GEN_AI_OUTPUT_MESSAGES ,
154+ serialize (self .output_messages )
155+ if self .output_messages
156+ else None ,
157+ ),
158+ (
159+ GenAI .GEN_AI_SYSTEM_INSTRUCTIONS ,
160+ serialize (self .system_instruction )
161+ if self .system_instruction
162+ else None ,
163+ ),
133164 )
134- return {key : value for key , value in optional_attrs if value is not None }
165+ return {
166+ key : value for key , value in optional_attrs if value is not None
167+ }
135168
136169 def _get_finish_reasons (self ) -> list [str ] | None :
137170 if self .finish_reasons is not None :
138171 return self .finish_reasons or None
139172 if self .output_messages :
140- reasons = [msg .finish_reason for msg in self .output_messages if msg .finish_reason ]
173+ reasons = [
174+ msg .finish_reason
175+ for msg in self .output_messages
176+ if msg .finish_reason
177+ ]
141178 return reasons or None
142179 return None
143180
@@ -188,11 +225,13 @@ def _emit_event(self) -> None:
188225 attributes .update (self ._get_message_attributes (for_span = False ))
189226 attributes .update (self .attributes )
190227 context = set_span_in_context (self .span , get_current ())
191- self ._handler ._logger .emit (LogRecord (
192- event_name = "gen_ai.client.inference.operation.details" ,
193- attributes = attributes ,
194- context = context ,
195- ))
228+ self ._handler ._logger .emit (
229+ LogRecord (
230+ event_name = "gen_ai.client.inference.operation.details" ,
231+ attributes = attributes ,
232+ context = context ,
233+ )
234+ )
196235
197236
198237@deprecated ("LLMInvocation is deprecated. Use InferenceInvocation instead." )
@@ -250,19 +289,24 @@ def __init__(
250289 )
251290 return
252291 # Old-style: data container pattern; span started later via handler.start_llm()
253- from opentelemetry .trace import INVALID_SPAN , SpanKind
254292 self ._handler = None
255293 self .attributes = {} if attributes is None else attributes
256- self .metric_attributes = {} if metric_attributes is None else metric_attributes
294+ self .metric_attributes = (
295+ {} if metric_attributes is None else metric_attributes
296+ )
257297 self .span = INVALID_SPAN
258298 self ._span_kind = SpanKind .CLIENT
259299 self ._context_token = None
260300 self ._monotonic_start_s = None
261301 self .provider = provider
262302 self .request_model = request_model
263303 self .input_messages = [] if input_messages is None else input_messages
264- self .output_messages = [] if output_messages is None else output_messages
265- self .system_instruction = [] if system_instruction is None else system_instruction
304+ self .output_messages = (
305+ [] if output_messages is None else output_messages
306+ )
307+ self .system_instruction = (
308+ [] if system_instruction is None else system_instruction
309+ )
266310 self .response_model_name = response_model_name
267311 self .response_id = response_id
268312 self .finish_reasons = finish_reasons
@@ -277,4 +321,8 @@ def __init__(
277321 self .seed = seed
278322 self .server_address = server_address
279323 self .server_port = server_port
280- self ._span_name = f"{ self .operation_name } { request_model } " if request_model else self .operation_name
324+ self ._span_name = (
325+ f"{ self .operation_name } { request_model } "
326+ if request_model
327+ else self .operation_name
328+ )
0 commit comments