Skip to content

Commit fa4a8b4

Browse files
committed
Make tox -e typecheck install langchain, and fixed some of the type
errors. The remaining ones seem like real issues that need to be fixed.
1 parent 8ec8dfc commit fa4a8b4

4 files changed

Lines changed: 272 additions & 278 deletions

File tree

instrumentation-genai/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
from typing import Any, Callable, Collection
4040

41-
from langchain_core.callbacks import BaseCallbackHandler # type: ignore
41+
from langchain_core.callbacks import BaseCallbackHandler
4242
from wrapt import wrap_function_wrapper # type: ignore
4343

4444
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
@@ -104,7 +104,7 @@ def __init__(
104104
def __call__(
105105
self,
106106
wrapped: Callable[..., None],
107-
instance: BaseCallbackHandler, # type: ignore
107+
instance: BaseCallbackHandler,
108108
args: tuple[Any, ...],
109109
kwargs: dict[str, Any],
110110
):

instrumentation-genai/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/callback_handler.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
from typing import Any
1818
from 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

2424
from opentelemetry.instrumentation.langchain.invocation_manager import (
2525
_InvocationManager,
@@ -34,20 +34,20 @@
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)

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,7 @@ deps =
11061106
{toxinidir}/instrumentation-genai/opentelemetry-instrumentation-vertexai[instruments]
11071107
{toxinidir}/instrumentation-genai/opentelemetry-instrumentation-google-genai[instruments]
11081108
{toxinidir}/instrumentation-genai/opentelemetry-instrumentation-anthropic[instruments]
1109+
{toxinidir}/instrumentation-genai/opentelemetry-instrumentation-langchain[instruments]
11091110
{toxinidir}/instrumentation/opentelemetry-instrumentation-aiokafka[instruments]
11101111
{toxinidir}/instrumentation/opentelemetry-instrumentation-asyncclick[instruments]
11111112
{toxinidir}/exporter/opentelemetry-exporter-credential-provider-gcp

0 commit comments

Comments
 (0)