Repository navigation
Description
Which component is this bug for?
Langchain Instrumentation
📜 Description
When using traceloop-sdk with langgraph (which depends only on langchain-core), no traces are emitted unless the langchain package is also installed. This breaks tracing for LangGraph-only projects.
👟 Reproduction steps
- Create a fresh virtual environment and install the following packages:
pip install traceloop-sdk==0.40.7 langgraph==0.4.8
- Run this minimal LangGraph script:
from typing import TypedDict
from langgraph.graph import StateGraph, END, START
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from traceloop.sdk import Traceloop
exporter = InMemorySpanExporter()
Traceloop.init(
exporter=exporter,
disable_batch=True,
)
class DummyGraphState(TypedDict):
result: str
def mynode_func(state: DummyGraphState) -> DummyGraphState:
return state
def build_graph():
workflow = StateGraph(DummyGraphState)
workflow.add_node("mynode", mynode_func)
workflow.add_edge(START, "mynode")
workflow.add_edge("mynode", END)
langgraph = workflow.compile()
return langgraph
graph = build_graph()
graph.invoke({"result": "init"})
spans = exporter.get_finished_spans()
assert ['mynode.task', 'LangGraph.workflow'] == [s.name for s in spans], f"Actual: {[s.name for s in spans]=}"- This will fail with:
Traceback (most recent call last):
File "/home/ronen/traceloop/openllmetry/packages/opentelemetry-instrumentation-langchain/tests/bug.py", line 35, in <module>
assert ['mynode.task', 'LangGraph.workflow'] == [s.name for s in spans], f"Actual: {[s.name for s in spans]=}"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Actual: [s.name for s in spans]=[]
- Installing these additional packages resolves the issue:
pip install langchain langchain_community langchain_openai
👍 Expected behavior
LangGraph spans should be emitted without requiring unrelated LangChain packages like langchain, langchain_community, or langchain_openai.
👎 Actual Behavior with Screenshots
No spans are emitted unless the langchain package (and others) are installed, even though langgraph does not depend on them.
🤖 Python Version
No response
📃 Provide any additional context for the Bug.
- The
langgraphpackage depends onlangchain-core, not onlangchain(Source) - However, the current instrumentor depends on the presence of the
langchainpackage, which causesinstrument()to exit early iflangchainis not installed:
Line 29 in 6d13035
_instruments = ("langchain >= 0.0.346", "langchain-core > 0.1.0") - Additionally, the instrumentor attempts to instrument
langchain_communityandlangchain_openaiunconditionally:
Lines 99 to 110 in 6d13035
wrap_function_wrapper( module="langchain_community.llms.openai", name="BaseOpenAI._astream", wrapper=openai_tracing_wrapper, ) # Wrap langchain_openai.llms.base.BaseOpenAI wrap_function_wrapper( module="langchain_openai.llms.base", name="BaseOpenAI._generate", wrapper=openai_tracing_wrapper, ) - As a result, when using LangGraph in isolation (without the
langchainpackage), no spans are emitted.
Suggestion:
Remove the hard dependency on the langchain package and adjust the logic to instrument langchain_community, and langchain_openai only if they are installed.
👀 Have you spent some time to check if this bug has been raised before?
- I checked and didn't find similar issue
Are you willing to submit PR?
Yes I am willing to submit a PR!
Activity
nirga commentedon Jun 9, 2025
Interesting @ronensc - luckily seems like an easy fix! Are you creating a PR?
ronensc commentedon Jun 9, 2025
@nirga Yes I am willing to submit a PR