1717from typing import Any
1818from uuid import UUID
1919
20- from langchain_core .callbacks import BaseCallbackHandler # type: ignore
21- from langchain_core .messages import BaseMessage # type: ignore
22- from langchain_core .outputs import LLMResult # type: ignore
20+ from langchain_core .callbacks import BaseCallbackHandler
21+ from langchain_core .messages import BaseMessage
22+ from langchain_core .outputs import LLMResult
2323
2424from opentelemetry .instrumentation .langchain .invocation_manager import (
2525 _InvocationManager ,
3434)
3535
3636
37- class OpenTelemetryLangChainCallbackHandler (BaseCallbackHandler ): # type: ignore[misc]
37+ class OpenTelemetryLangChainCallbackHandler (BaseCallbackHandler ):
3838 """
3939 A callback handler for LangChain that uses OpenTelemetry to create spans for LLM calls and chains, tools etc,. in future.
4040 """
4141
4242 def __init__ (self , telemetry_handler : TelemetryHandler ) -> None :
43- super ().__init__ () # type: ignore
43+ super ().__init__ ()
4444 self ._telemetry_handler = telemetry_handler
4545 self ._invocation_manager = _InvocationManager ()
4646
4747 def on_chat_model_start (
4848 self ,
4949 serialized : dict [str , Any ],
50- messages : list [list [BaseMessage ]], # type: ignore
50+ messages : list [list [BaseMessage ]],
5151 * ,
5252 run_id : UUID ,
5353 tags : list [str ] | None ,
@@ -107,19 +107,19 @@ def on_chat_model_start(
107107 max_tokens = metadata .get ("ls_max_tokens" )
108108
109109 input_messages : list [InputMessage ] = []
110- for sub_messages in messages : # type: ignore[reportUnknownVariableType]
111- for message in sub_messages : # type: ignore[reportUnknownVariableType]
112- content = message .content # type : ignore[reportUnknownVariableType ]
113- role = message .type # type: ignore[reportUnknownVariableType]
114- parts = [Text (content = content , type = "text" )] # type: ignore[reportUnknownVariableType]
115- input_messages .append (InputMessage (parts = parts , role = role )) # type: ignore[reportUnknownVariableType]
110+ for sub_messages in messages :
111+ for message in sub_messages :
112+ content = message .content # pyright : ignore[reportUnknownMemberType ]
113+ role = message .type
114+ parts = [Text (content = content , type = "text" )]
115+ input_messages .append (InputMessage (parts = parts , role = role ))
116116
117117 llm_invocation = LLMInvocation (
118118 request_model = request_model ,
119119 input_messages = input_messages ,
120120 provider = provider ,
121- top_p = top_p , # type: ignore[reportPossiblyUnboundVariable]
122- frequency_penalty = frequency_penalty , # type: ignore[reportPossiblyUnboundVariable]
121+ top_p = top_p ,
122+ frequency_penalty = frequency_penalty ,
123123 presence_penalty = presence_penalty , # type: ignore[reportPossiblyUnboundVariable]
124124 stop_sequences = stop_sequences , # type: ignore[reportPossiblyUnboundVariable]
125125 seed = seed , # type: ignore[reportPossiblyUnboundVariable]
@@ -137,7 +137,7 @@ def on_chat_model_start(
137137
138138 def on_llm_end (
139139 self ,
140- response : LLMResult , # type: ignore [reportUnknownParameterType]
140+ response : LLMResult ,
141141 * ,
142142 run_id : UUID ,
143143 parent_run_id : UUID | None ,
@@ -151,7 +151,7 @@ def on_llm_end(
151151 return
152152
153153 output_messages : list [OutputMessage ] = []
154- for generation in getattr (response , "generations" , []): # type: ignore
154+ for generation in getattr (response , "generations" , []):
155155 for chat_generation in generation :
156156 # Get finish reason
157157 generation_info = getattr (
@@ -185,7 +185,7 @@ def on_llm_end(
185185 output_message = OutputMessage (
186186 role = role ,
187187 parts = parts ,
188- finish_reason = finish_reason , # type: ignore[reportPossiblyUnboundVariable, reportArgumentType]
188+ finish_reason = finish_reason ,
189189 )
190190 output_messages .append (output_message )
191191
@@ -207,7 +207,7 @@ def on_llm_end(
207207
208208 llm_invocation .output_messages = output_messages
209209
210- llm_output = getattr (response , "llm_output" , None ) # type: ignore
210+ llm_output = getattr (response , "llm_output" , None )
211211 if llm_output is not None :
212212 response_model = llm_output .get ("model_name" ) or llm_output .get (
213213 "model"
@@ -222,7 +222,7 @@ def on_llm_end(
222222 llm_invocation = self ._telemetry_handler .stop_llm (
223223 invocation = llm_invocation
224224 )
225- if not llm_invocation .span .is_recording (): # type: ignore[reportOptionalMemberAccess]
225+ if llm_invocation . span and not llm_invocation .span .is_recording ():
226226 self ._invocation_manager .delete_invocation_state (run_id = run_id )
227227
228228 def on_llm_error (
@@ -240,10 +240,9 @@ def on_llm_error(
240240 # If the invocation does not exist, we cannot set attributes or end it
241241 return
242242
243- error = Error (message = str (error ), type = type (error )) # type: ignore[reportAssignmentType]
243+ error_otel = Error (message = str (error ), type = type (error ))
244244 llm_invocation = self ._telemetry_handler .fail_llm (
245- invocation = llm_invocation ,
246- error = error , # type: ignore[reportArgumentType]
245+ invocation = llm_invocation , error = error_otel
247246 )
248- if not llm_invocation .span .is_recording (): # type: ignore[reportOptionalMemberAccess]
247+ if llm_invocation . span and not llm_invocation .span .is_recording ():
249248 self ._invocation_manager .delete_invocation_state (run_id = run_id )
0 commit comments