Skip to content

Commit e381a36

Browse files
wrisaaabmass
andauthored
Switched langchain llm callbacks to use genai utils handler (#3889)
* Removed telemetry from inference callbacks and added calls to genai utils apis instead. * Fixed errors * Fixed typecheck errors * Fixed typecheck errors * Fixed precommit errors * added util dependancy * updated invocation manager * removed unnecessary dependancies * fixed precommit * fixed typecheck * fixed precommit * fixed test * removed unnecessary line * addressed comments * fixed errors * fixed precommit * removed get_property_value method * fixed format * 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. * Please fix reportPossiblyUnboundVariable * fixed typecheck * added and updated tests * Fixed conflicts and removed unnecessary changes * Fixed precommit --------- Co-authored-by: aaronabbott <aaronabbott@google.com>
1 parent e01a4c1 commit e381a36

File tree

17 files changed

+4461
-3770
lines changed

17 files changed

+4461
-3770
lines changed

instrumentation-genai/opentelemetry-instrumentation-langchain/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Added support to call genai utils handler for langchain LLM invocations.
11+
([https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3889](#3889))
12+
1013
- Added span support for genAI langchain llm invocation.
1114
([#3665](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3665))

instrumentation-genai/opentelemetry-instrumentation-langchain/examples/manual/README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Next, set up a virtual environment like this:
2626
source .venv/bin/activate
2727
pip install "python-dotenv[cli]"
2828
pip install -r requirements.txt
29+
pip install opentelemetry-instrumentation-langchain
30+
pip install util/opentelemetry-util-genai (once opentelemetry-util-genai package is released remove it from here and add dependency in opentelemetry-instrumentation-langchain)
2931

3032
Run
3133
---

instrumentation-genai/opentelemetry-instrumentation-langchain/pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ classifiers = [
2525
"Programming Language :: Python :: 3.13",
2626
]
2727
dependencies = [
28-
"opentelemetry-api >= 1.31.0",
2928
"opentelemetry-instrumentation ~= 0.57b0",
30-
"opentelemetry-semantic-conventions ~= 0.57b0"
3129
]
3230

3331
[project.optional-dependencies]

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,16 @@
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
4545
from opentelemetry.instrumentation.langchain.callback_handler import (
4646
OpenTelemetryLangChainCallbackHandler,
4747
)
4848
from opentelemetry.instrumentation.langchain.package import _instruments
49-
from opentelemetry.instrumentation.langchain.version import __version__
5049
from opentelemetry.instrumentation.utils import unwrap
51-
from opentelemetry.semconv.schemas import Schemas
52-
from opentelemetry.trace import get_tracer
50+
from opentelemetry.util.genai.handler import get_telemetry_handler
5351

5452

5553
class LangChainInstrumentor(BaseInstrumentor):
@@ -72,15 +70,12 @@ def _instrument(self, **kwargs: Any):
7270
Enable Langchain instrumentation.
7371
"""
7472
tracer_provider = kwargs.get("tracer_provider")
75-
tracer = get_tracer(
76-
__name__,
77-
__version__,
78-
tracer_provider,
79-
schema_url=Schemas.V1_37_0.value,
80-
)
8173

74+
telemetry_handler = get_telemetry_handler(
75+
tracer_provider=tracer_provider
76+
)
8277
otel_callback_handler = OpenTelemetryLangChainCallbackHandler(
83-
tracer=tracer,
78+
telemetry_handler=telemetry_handler
8479
)
8580

8681
wrap_function_wrapper(
@@ -109,7 +104,7 @@ def __init__(
109104
def __call__(
110105
self,
111106
wrapped: Callable[..., None],
112-
instance: BaseCallbackHandler, # type: ignore
107+
instance: BaseCallbackHandler,
113108
args: tuple[Any, ...],
114109
kwargs: dict[str, Any],
115110
):

0 commit comments

Comments
 (0)