3333from opentelemetry .trace import Span , SpanKind , Tracer
3434from opentelemetry .trace .propagation import set_span_in_context
3535from opentelemetry .util .genai .handler import TelemetryHandler
36+ from opentelemetry .util .genai .invocation import InferenceInvocation
3637from opentelemetry .util .genai .types import (
3738 ContentCapturingMode ,
3839 Error ,
39- LLMInvocation , # pylint: disable=no-name-in-module # TODO: migrate to InferenceInvocation
4040 OutputMessage ,
4141 Text ,
4242 ToolCallRequest ,
@@ -128,10 +128,8 @@ def chat_completions_create_v_new(
128128 capture_content = content_capturing_mode != ContentCapturingMode .NO_CONTENT
129129
130130 def traced_method (wrapped , instance , args , kwargs ):
131- chat_invocation = handler .start_llm (
132- create_chat_invocation (
133- kwargs , instance , capture_content = capture_content
134- )
131+ chat_invocation = create_chat_invocation (
132+ handler , kwargs , instance , capture_content = capture_content
135133 )
136134
137135 try :
@@ -143,18 +141,16 @@ def traced_method(wrapped, instance, args, kwargs):
143141 parsed_result = result
144142 if is_streaming (kwargs ):
145143 return ChatStreamWrapper (
146- parsed_result , handler , chat_invocation , capture_content
144+ parsed_result , chat_invocation , capture_content
147145 )
148146
149147 _set_response_properties (
150148 chat_invocation , parsed_result , capture_content
151149 )
152- handler . stop_llm ( chat_invocation )
150+ chat_invocation . stop ( )
153151 return result
154152 except Exception as error :
155- handler .fail_llm (
156- chat_invocation , Error (type = type (error ), message = str (error ))
157- )
153+ chat_invocation .fail (Error (type = type (error ), message = str (error )))
158154 raise
159155
160156 return traced_method
@@ -232,10 +228,8 @@ def async_chat_completions_create_v_new(
232228 capture_content = content_capturing_mode != ContentCapturingMode .NO_CONTENT
233229
234230 async def traced_method (wrapped , instance , args , kwargs ):
235- chat_invocation = handler .start_llm (
236- create_chat_invocation (
237- kwargs , instance , capture_content = capture_content
238- )
231+ chat_invocation = create_chat_invocation (
232+ handler , kwargs , instance , capture_content = capture_content
239233 )
240234
241235 try :
@@ -247,19 +241,17 @@ async def traced_method(wrapped, instance, args, kwargs):
247241 parsed_result = result
248242 if is_streaming (kwargs ):
249243 return ChatStreamWrapper (
250- parsed_result , handler , chat_invocation , capture_content
244+ parsed_result , chat_invocation , capture_content
251245 )
252246
253247 _set_response_properties (
254248 chat_invocation , parsed_result , capture_content
255249 )
256- handler . stop_llm ( chat_invocation )
250+ chat_invocation . stop ( )
257251 return result
258252
259253 except Exception as error :
260- handler .fail_llm (
261- chat_invocation , Error (type = type (error ), message = str (error ))
262- )
254+ chat_invocation .fail (Error (type = type (error ), message = str (error )))
263255 raise
264256
265257 return traced_method
@@ -495,8 +487,8 @@ def _set_response_attributes(span, result):
495487
496488
497489def _set_response_properties (
498- chat_invocation : LLMInvocation , result , capture_content : bool
499- ) -> LLMInvocation :
490+ chat_invocation : InferenceInvocation , result , capture_content : bool
491+ ) -> InferenceInvocation :
500492 if getattr (result , "model" , None ):
501493 chat_invocation .response_model_name = result .model
502494
@@ -868,8 +860,7 @@ def cleanup(self, error: Optional[BaseException] = None):
868860
869861
870862class ChatStreamWrapper (BaseStreamWrapper ):
871- handler : TelemetryHandler
872- invocation : LLMInvocation
863+ invocation : InferenceInvocation
873864 response_id : Optional [str ] = None
874865 response_model : Optional [str ] = None
875866 service_tier : Optional [str ] = None
@@ -880,13 +871,11 @@ class ChatStreamWrapper(BaseStreamWrapper):
880871 def __init__ (
881872 self ,
882873 stream : Stream ,
883- handler : TelemetryHandler ,
884- invocation : LLMInvocation ,
874+ invocation : InferenceInvocation ,
885875 capture_content : bool ,
886876 ):
887877 super ().__init__ (stream , capture_content = capture_content )
888878 self .stream = stream
889- self .handler = handler
890879 self .invocation = invocation
891880 self .choice_buffers = []
892881
@@ -944,9 +933,7 @@ def cleanup(self, error: Optional[BaseException] = None):
944933 self ._set_output_messages ()
945934
946935 if error :
947- self .handler .fail_llm (
948- self .invocation , Error (type = type (error ), message = str (error ))
949- )
936+ self .invocation .fail (Error (type = type (error ), message = str (error )))
950937 else :
951- self .handler . stop_llm ( self . invocation )
938+ self .invocation . stop ( )
952939 self ._started = False
0 commit comments