diff --git a/langfuse/langchain/CallbackHandler.py b/langfuse/langchain/CallbackHandler.py index 1349f6ae0..d33c9e58c 100644 --- a/langfuse/langchain/CallbackHandler.py +++ b/langfuse/langchain/CallbackHandler.py @@ -38,9 +38,15 @@ from langfuse.types import TraceContext try: - import langchain - - if langchain.__version__.startswith("1"): + # Use langchain_core as the version sentinel instead of the langchain + # umbrella package: langchain_core is the common runtime dependency of + # langchain, langgraph, and every langchain-* provider package, so it + # is present on every install that needs this handler. Importing the + # umbrella here previously forced langchain itself to be installed + # even for the v1 branch, which never uses any langchain.* import. + import langchain_core + + if langchain_core.__version__.startswith("1"): # Langchain v1 from langchain_core.agents import AgentAction, AgentFinish from langchain_core.callbacks import ( @@ -59,7 +65,7 @@ from langchain_core.outputs import ChatGeneration, LLMResult else: - # Langchain v0 + # Langchain v0 — these symbols only exist in the umbrella package from langchain.callbacks.base import ( # type: ignore BaseCallbackHandler as LangchainBaseCallbackHandler, ) @@ -81,7 +87,7 @@ except ImportError: raise ModuleNotFoundError( - "Please install langchain to use the Langfuse langchain integration: 'pip install langchain'" + "Please install langchain_core to use the Langfuse langchain integration: 'pip install langchain-core'" ) LANGSMITH_TAG_HIDDEN: str = "langsmith:hidden"