OpenTelemetry Official Langchain instrumentation
Project description
This library traces LangChain and LangGraph applications. It hooks into LangChain’s callback manager to emit spans that mirror the structure of your application:
Workflow spans for a graph or chain run — for example an invocation of a LangGraph StateGraph — capturing the overall input and output of the run.
Agent spans for agent invocations nested inside a workflow, including the agent name, id, description, and conversation/session id when available.
Tool spans for tool calls made during a run.
The spans nest to reflect the graph, so a single graph invocation produces a workflow span with the agent, tool, and model calls it triggered as children.
Installation
pip install opentelemetry-instrumentation-genai-langchain
See the examples directory for runnable workflow, agent, tools, and zero-code scenarios.
Usage
Call LangChainInstrumentor().instrument() once during startup, then build and invoke your graph as usual. The example below traces a simple two-node LangGraph StateGraph (START → researcher → summariser → END); the graph.invoke(...) call is recorded as a workflow span with the node model calls nested underneath.
from typing import Annotated, TypedDict
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI
from langgraph.graph import END, START, StateGraph
from langgraph.graph.message import add_messages
from opentelemetry.instrumentation.genai.langchain import LangChainInstrumentor
LangChainInstrumentor().instrument()
llm = ChatOpenAI(model="<your-model>", temperature=0)
class State(TypedDict):
messages: Annotated[list, add_messages]
research: str
def researcher(state: State) -> dict:
response = llm.invoke(
[
SystemMessage(content="Provide 2-3 factual sentences."),
HumanMessage(content=state["messages"][-1].content),
]
)
return {"research": response.content, "messages": [response]}
def summariser(state: State) -> dict:
response = llm.invoke(
[
SystemMessage(content="Condense the text into one sentence."),
HumanMessage(content=state["research"]),
]
)
return {"messages": [response]}
builder = StateGraph(State)
builder.add_node("researcher", researcher)
builder.add_node("summariser", summariser)
builder.add_edge(START, "researcher")
builder.add_edge("researcher", "summariser")
builder.add_edge("summariser", END)
graph = builder.compile()
# Recorded as a workflow span with the two node LLM calls nested underneath.
graph.invoke(
{
"messages": [HumanMessage(content="What is the capital of France?")],
"research": "",
}
)
References
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file opentelemetry_instrumentation_genai_langchain-1.0b0.tar.gz.
File metadata
- Download URL: opentelemetry_instrumentation_genai_langchain-1.0b0.tar.gz
- Upload date:
- Size: 43.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b2167b7a21932b6eb54fbf6292fa3d78677eb428197b0d0924c3a26668f939b
|
|
| MD5 |
b5cbdc9d780990251d687b74e4ba5f9b
|
|
| BLAKE2b-256 |
98b04335e548718808ec0e1b49d9e41ae9c2e30f301c1cb676268f89a3346665
|
Provenance
The following attestation bundles were made for opentelemetry_instrumentation_genai_langchain-1.0b0.tar.gz:
Publisher:
release-all.yml on open-telemetry/opentelemetry-python-genai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
opentelemetry_instrumentation_genai_langchain-1.0b0.tar.gz -
Subject digest:
7b2167b7a21932b6eb54fbf6292fa3d78677eb428197b0d0924c3a26668f939b - Sigstore transparency entry: 2132312111
- Sigstore integration time:
-
Permalink:
open-telemetry/opentelemetry-python-genai@1e4aca50a0c5b5fcf8b3e2a7a81eec6ac12a1669 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/open-telemetry
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-all.yml@1e4aca50a0c5b5fcf8b3e2a7a81eec6ac12a1669 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file opentelemetry_instrumentation_genai_langchain-1.0b0-py3-none-any.whl.
File metadata
- Download URL: opentelemetry_instrumentation_genai_langchain-1.0b0-py3-none-any.whl
- Upload date:
- Size: 21.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6cb27abaed51dc58a77418a48617465a60a50f50078d573094fd39060214eedf
|
|
| MD5 |
e96d5d46f905e9a1de059559c92a44c0
|
|
| BLAKE2b-256 |
b97623c14aa4cdd82d7beb2795b7c56a011d05f9cfbf30db51d86438515ba476
|
Provenance
The following attestation bundles were made for opentelemetry_instrumentation_genai_langchain-1.0b0-py3-none-any.whl:
Publisher:
release-all.yml on open-telemetry/opentelemetry-python-genai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
opentelemetry_instrumentation_genai_langchain-1.0b0-py3-none-any.whl -
Subject digest:
6cb27abaed51dc58a77418a48617465a60a50f50078d573094fd39060214eedf - Sigstore transparency entry: 2132312192
- Sigstore integration time:
-
Permalink:
open-telemetry/opentelemetry-python-genai@1e4aca50a0c5b5fcf8b3e2a7a81eec6ac12a1669 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/open-telemetry
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-all.yml@1e4aca50a0c5b5fcf8b3e2a7a81eec6ac12a1669 -
Trigger Event:
workflow_dispatch
-
Statement type: