Skip to content

Commit b5b11ee

Browse files
committed
fix: Add LangChain 1.0+ compatibility for CallbackHandler imports
- Use try/except to import from langchain_core first (LangChain 1.0+) - Fall back to legacy langchain imports for older versions - Maintains backward compatibility with LangChain 0.x - All existing tests pass (45 passed) Fixes #362
1 parent 50b0c71 commit b5b11ee

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

posthog/ai/langchain/callbacks.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@
2020
)
2121
from uuid import UUID
2222

23-
from langchain.callbacks.base import BaseCallbackHandler
24-
from langchain.schema.agent import AgentAction, AgentFinish
23+
try:
24+
# LangChain 1.0+ and modern 0.x with langchain-core
25+
from langchain_core.callbacks.base import BaseCallbackHandler
26+
from langchain_core.agents import AgentAction, AgentFinish
27+
except (ImportError, ModuleNotFoundError):
28+
# Fallback for older LangChain versions
29+
from langchain.callbacks.base import BaseCallbackHandler # type: ignore
30+
from langchain.schema.agent import AgentAction, AgentFinish # type: ignore
2531
from langchain_core.documents import Document
2632
from langchain_core.messages import (
2733
AIMessage,

0 commit comments

Comments
 (0)