Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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": "",
    }
)

Configuration

By default, prompts and completions are not captured. To capture message content, set the environment variable OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to one of NO_CONTENT, SPAN_ONLY, EVENT_ONLY, or SPAN_AND_EVENT.

Prompts and completions can instead be uploaded to external storage via a completion hook: set OTEL_INSTRUMENTATION_GENAI_COMPLETION_HOOK=upload with OTEL_INSTRUMENTATION_GENAI_UPLOAD_BASE_PATH (install the upload extra: pip install opentelemetry-util-genai[upload]), or pass a custom CompletionHook programmatically, which takes precedence over the environment variable:

LangChainInstrumentor().instrument(completion_hook=my_hook)

References

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.1b1.tar.gz.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_genai_langchain-1.1b1.tar.gz
Algorithm Hash digest
SHA256 bee2dc1ed80c879626f0a35d1dd45efe4decb225acf75ee9a077baec10bbdd5a
MD5 c88edfd7258c8c28c4213910a411c08c
BLAKE2b-256 7449e82bbf8633ef88ec9824ca172b45e1baae33bfbce7eeee6cc056512c182b

See more details on using hashes here.

Provenance

The following attestation bundles were made for opentelemetry_instrumentation_genai_langchain-1.1b1.tar.gz:

Publisher: release-package.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.1b1-py3-none-any.whl.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_genai_langchain-1.1b1-py3-none-any.whl
Algorithm Hash digest
SHA256 17cfb31ba13978a2c5da2d818bcef915ab67ce26f6535bd852c5ed3e81f94a5e
MD5 6732df871b8b73aba362fe48a4569cb1
BLAKE2b-256 b6b3ce07110e539492464f24c0208c5c109b4d5e89e0aa3c0b64e7b8e20cdd40

See more details on using hashes here.

Provenance

The following attestation bundles were made for opentelemetry_instrumentation_genai_langchain-1.1b1-py3-none-any.whl:

Publisher: release-package.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.

Release history Release notifications | RSS feed

This release

1.1b1 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page