-
Notifications
You must be signed in to change notification settings - Fork 49
Description
Bug Description
The PostHog LangChain CallbackHandler is incompatible with LangChain 1.0+ due to deprecated import paths.
Current Behavior
When attempting to use the CallbackHandler with LangChain 1.0+:
from posthog.ai.langchain import CallbackHandler as PostHogCallbackHandlerThis raises:
ModuleNotFoundError: No module named 'langchain.callbacks'
Root Cause
The issue is in posthog/ai/langchain/callbacks.py lines 23-24:
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema.agent import AgentAction, AgentFinishLangChain 1.0 (released September 2024) restructured the codebase and moved these modules to langchain_core.
Environment
- PostHog SDK version: 6.7.11
- LangChain version: 1.0.3
- Python: 3.x
Suggested Fix
Update the imports in posthog/ai/langchain/callbacks.py lines 23-24:
# Current (incompatible with LangChain 1.0+)
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema.agent import AgentAction, AgentFinish
# Proposed fix
from langchain_core.callbacks.base import BaseCallbackHandler
from langchain_core.agents import AgentAction, AgentFinishThis change maintains backward compatibility since langchain_core exists in both LangChain 0.x and 1.0+.
Additional Context
LangChain 1.0 removed the langchain.callbacks and langchain.schema modules as part of the core refactoring. All core functionality was moved to the langchain_core package, which is the proper import path for both old and new versions.