Skip to main content

OpenTelemetry Official Langchain instrumentation

Project description

pypi

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


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

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file opentelemetry_instrumentation_genai_langchain-1.0b0.tar.gz.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_genai_langchain-1.0b0.tar.gz
Algorithm Hash digest
SHA256 7b2167b7a21932b6eb54fbf6292fa3d78677eb428197b0d0924c3a26668f939b
MD5 b5cbdc9d780990251d687b74e4ba5f9b
BLAKE2b-256 98b04335e548718808ec0e1b49d9e41ae9c2e30f301c1cb676268f89a3346665

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file opentelemetry_instrumentation_genai_langchain-1.0b0-py3-none-any.whl.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_genai_langchain-1.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 6cb27abaed51dc58a77418a48617465a60a50f50078d573094fd39060214eedf
MD5 e96d5d46f905e9a1de059559c92a44c0
BLAKE2b-256 b97623c14aa4cdd82d7beb2795b7c56a011d05f9cfbf30db51d86438515ba476

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page