Registry / observability / opentelemetry-instrumentation-langchain

opentelemetry-instrumentation-langchain

JSON →
library0.62.3pypypi✓ verified 23d ago

This library provides OpenTelemetry instrumentation for applications built with LangChain, enabling comprehensive tracing of LLM interactions and associated components. It is part of the OpenLLMetry project, which extends OpenTelemetry for enhanced LLM observability. The project maintains a rapid release cadence, with frequent updates often driven by advancements in OpenTelemetry's Generative AI semantic conventions.

pip install opentelemetry-instrumentation-langchain
INSTALL
IMPORT
SIG · OPENTELEMETRY-INST
O
opentelemetry-instrumentation-langchain
observabilitypythonv0.62.3
Install
3.0s avg
Import
Disk
23MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.62.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 24.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.0s · import 0.000s · 25MB
23MB installed
● package 23MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LangchainInstrumentor
from opentelemetry.instrumentation.langchain import LangchainInstrumentor
from opentelemetry.instrumentation.langchain import LangchainInstrumentor

This quickstart demonstrates how to instrument a basic LangChain application using `opentelemetry-instrumentation-langchain`. It includes the necessary OpenTelemetry SDK setup with a `ConsoleSpanExporter` to print traces to the console, followed by the `LangchainInstrumentor().instrument()` call. A simple LangChain expression language (LCEL) chain is then invoked to generate a story, and its execution will be automatically traced by OpenTelemetry.

import os from opentelemetry import trace from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor from opentelemetry.instrumentation.langchain import LangchainInstrumentor from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser # 1. Configure OpenTelemetry SDK (essential for any OTel instrumentation) # Set up a basic console exporter for demonstration resource = Resource.create({"service.name": "my-langchain-app"}) tracer_provider = TracerProvider(resource=resource) tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter())) trace.set_tracer_provider(tracer_provider) # 2. Instrument Langchain LangchainInstrumentor().instrument() # Optional: Disable sensitive content logging if needed (e.g., for production) # os.environ['TRACELOOP_TRACE_CONTENT'] = 'false' # 3. Use Langchain as usual os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY', 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') # Replace with actual key or secure handling llm = ChatOpenAI(temperature=0) prompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful AI assistant."), ("user", "Tell me a short story about {topic}.") ]) output_parser = StrOutputParser() chain = prompt | llm | output_parser if __name__ == "__main__": print("Running LangChain application...") response = chain.invoke({"topic": "a cat detective"}) print("\nLangChain Response:") print(response) print("\nCheck console output for OpenTelemetry traces.")
Debug
Known issues
breakingFrequent updates to OpenTelemetry Generative AI semantic conventions can introduce breaking changes in span attribute names and trace structures. Recent versions (e.g., v0.53.0, v0.55.0 onwards) specifically align with new semantic conventions.
fix
Review the release notes and official OpenTelemetry Generative AI semantic conventions documentation when upgrading. You may need to update dashboards, queries, and custom processors that rely on specific span attributes. Using `OTEL_SEMCONV_STABILITY_OPT_IN=latest` can help with migration during transitions.
affects: >=0.53.0
gotchaBy default, this instrumentation logs sensitive data such as prompts, completions, and embeddings directly to span attributes. This provides visibility but may expose private user data.
fix
To disable the logging of content for privacy reasons, set the environment variable `TRACELOOP_TRACE_CONTENT` to `false`. `os.environ['TRACELOOP_TRACE_CONTENT'] = 'false'`.
affects: All versions
gotchaThe `LangchainInstrumentor().instrument()` call only enables the instrumentation. For traces to be collected and exported, a complete OpenTelemetry SDK setup (TracerProvider, SpanProcessor, SpanExporter) is required.
fix
Ensure you configure the OpenTelemetry SDK before instrumenting. This involves setting up a `TracerProvider`, adding a `SpanProcessor` (e.g., `BatchSpanProcessor` for production), and configuring an appropriate `SpanExporter` (e.g., `OTLPSpanExporter` to send to a collector).
affects: All versions
Upgrade
Version history
0.62.3latest on PyPI · released Aug 10, 2026
Audit
Dependencies
langchain-corerequiredCore dependency for LangChain applications, which this package instruments.
opentelemetry-apirequiredOpenTelemetry API for basic tracing constructs.
opentelemetry-sdkrequiredOpenTelemetry SDK for configuring tracing providers, processors, and exporters.
opentelemetry-semantic-conventionsrequiredDefines standard attribute names and values for OpenTelemetry spans, especially for Generative AI.
wraptrequiredUsed for function wrapping and instrumentation.
Agent activity
28 hits · last 30 days
node
24
OpenAI (training)
1
Resources
opentelemetry-instrumentation-langchain — pip install opentelemetry-instrumentation-langchain · libregistry