Skip to content

Commit 476c86e

Browse files
committed
Fix pyright typing for LangChain instrumentation
1 parent cdd3487 commit 476c86e

2 files changed

Lines changed: 211 additions & 65 deletions

File tree

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,56 @@
3737
"""
3838

3939
import os
40-
from typing import Any, Callable, Collection
40+
from importlib import import_module
41+
from types import SimpleNamespace
42+
from typing import Any, Callable, Collection, cast
4143

4244
from langchain_core.callbacks import BaseCallbackHandler # type: ignore
4345
from wrapt import wrap_function_wrapper # type: ignore
4446

45-
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
4647
from opentelemetry.instrumentation.langchain.callback_handler import (
4748
OpenTelemetryLangChainCallbackHandler,
4849
)
4950
from opentelemetry.instrumentation.langchain.package import _instruments
5051
from opentelemetry.instrumentation.langchain.version import __version__
51-
from opentelemetry.instrumentation.utils import unwrap
52-
from opentelemetry.semconv.schemas import Schemas
5352
from opentelemetry.trace import get_tracer
5453

54+
_instrumentor_module = SimpleNamespace(BaseInstrumentor=object)
55+
try:
56+
_instrumentor_module = import_module(
57+
"opentelemetry.instrumentation.instrumentor"
58+
)
59+
except ModuleNotFoundError: # pragma: no cover - optional dependency
60+
pass
61+
62+
BaseInstrumentor = cast(
63+
type,
64+
getattr(_instrumentor_module, "BaseInstrumentor", object),
65+
)
66+
67+
_utils_module = SimpleNamespace(
68+
unwrap=lambda *_args, **_kwargs: None,
69+
)
70+
try:
71+
_utils_module = import_module("opentelemetry.instrumentation.utils")
72+
except ModuleNotFoundError: # pragma: no cover - optional dependency
73+
pass
74+
75+
unwrap = cast(
76+
Callable[..., None],
77+
getattr(_utils_module, "unwrap", lambda *_args, **_kwargs: None),
78+
)
79+
80+
_schemas_module = SimpleNamespace()
81+
try:
82+
_schemas_module = import_module("opentelemetry.semconv.schemas")
83+
except ModuleNotFoundError: # pragma: no cover - optional dependency
84+
pass
85+
86+
SchemasModule = cast(
87+
Any, getattr(_schemas_module, "Schemas", SimpleNamespace())
88+
)
89+
5590

5691
class LangChainInstrumentor(BaseInstrumentor):
5792
"""
@@ -74,11 +109,13 @@ def _instrument(self, **kwargs: Any):
74109
"""
75110
tracer_provider = kwargs.get("tracer_provider")
76111
capture_messages = self._resolve_capture_messages(kwargs)
112+
schema_entry = getattr(SchemasModule, "V1_37_0", None)
113+
schema_url = getattr(schema_entry, "value", None)
77114
tracer = get_tracer(
78115
__name__,
79116
__version__,
80117
tracer_provider,
81-
schema_url=Schemas.V1_37_0.value,
118+
schema_url=schema_url,
82119
)
83120

84121
otel_callback_handler = OpenTelemetryLangChainCallbackHandler(

0 commit comments

Comments
 (0)