Skip to content

Commit 57546d2

Browse files
authored
fix(llma): LangChain 1.0+ compatibility for CallbackHandler (#363)
* 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 * test: Add regression test for AgentAction/AgentFinish imports - Tests that AgentAction and AgentFinish can be imported - Tests on_agent_action and on_agent_finish callbacks with mock data - Ensures compatibility with both LangChain 0.x and 1.0+ - Catches the import issue that was previously only tested with API keys This addresses a test coverage gap identified during code review. * chore: Add CHANGELOG entry for LangChain 1.0+ compatibility fix * fix: Remove unused type: ignore comments for mypy The type: ignore comments were only needed when the except block executes, but CI runs with LangChain 1.0+ so the try block succeeds. Mypy flags these as unused-ignore errors. * chore: bump version to 6.7.12 for langchain 1.0 compatibility
1 parent 50b0c71 commit 57546d2

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# Unreleased
1+
# 6.7.12 - 2025-11-02
22

33
- fix(django): Restore process_exception method to capture view and downstream middleware exceptions (fixes #329)
4+
- fix(ai/langchain): Add LangChain 1.0+ compatibility for CallbackHandler imports (fixes #362)
45

56
# 6.7.11 - 2025-10-28
67

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
30+
from langchain.schema.agent import AgentAction, AgentFinish
2531
from langchain_core.documents import Document
2632
from langchain_core.messages import (
2733
AIMessage,

posthog/test/ai/langchain/test_callbacks.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,3 +1877,46 @@ def test_tool_definition(mock_client):
18771877
assert props["$ai_latency"] == 1.0
18781878
# Verify that tools are captured in the $ai_tools property
18791879
assert props["$ai_tools"] == tools
1880+
1881+
1882+
def test_agent_action_and_finish_imports():
1883+
"""
1884+
Regression test for LangChain 1.0+ compatibility (Issue #362).
1885+
Verifies that AgentAction and AgentFinish can be imported and used.
1886+
This test ensures the imports work with both LangChain 0.x and 1.0+.
1887+
"""
1888+
# Import the types that caused the compatibility issue
1889+
try:
1890+
from langchain_core.agents import AgentAction, AgentFinish
1891+
except (ImportError, ModuleNotFoundError):
1892+
from langchain.schema.agent import AgentAction, AgentFinish # type: ignore
1893+
1894+
# Verify they're available in the callbacks module
1895+
from posthog.ai.langchain.callbacks import CallbackHandler
1896+
1897+
# Test on_agent_action with mock data
1898+
mock_client = MagicMock()
1899+
callbacks = CallbackHandler(mock_client)
1900+
run_id = uuid.uuid4()
1901+
parent_run_id = uuid.uuid4()
1902+
1903+
# Create mock AgentAction
1904+
action = AgentAction(tool="test_tool", tool_input="test_input", log="test_log")
1905+
1906+
# Should not raise an exception
1907+
callbacks.on_agent_action(action, run_id=run_id, parent_run_id=parent_run_id)
1908+
1909+
# Verify parent was set
1910+
assert run_id in callbacks._parent_tree
1911+
assert callbacks._parent_tree[run_id] == parent_run_id
1912+
1913+
# Test on_agent_finish with mock data
1914+
finish = AgentFinish(return_values={"output": "test_output"}, log="finish_log")
1915+
1916+
# Should not raise an exception
1917+
callbacks.on_agent_finish(finish, run_id=run_id, parent_run_id=parent_run_id)
1918+
1919+
# Verify capture was called
1920+
assert mock_client.capture.call_count == 1
1921+
call_args = mock_client.capture.call_args[1]
1922+
assert call_args["event"] == "$ai_span"

posthog/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "6.7.11"
1+
VERSION = "6.7.12"
22

33
if __name__ == "__main__":
44
print(VERSION, end="") # noqa: T201

0 commit comments

Comments
 (0)