Skip to content

Commit b65e042

Browse files
committed
Merge latest master and resolve test conflicts
- Kept all new tests from both branches - PR branch: 5 new cache token tests - Master: 1 new LangChain 1.0+ compatibility test
2 parents e566768 + 46589f9 commit b65e042

File tree

5 files changed

+2397
-4
lines changed

5 files changed

+2397
-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.12 - 2025-11-02
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
@@ -2083,3 +2083,46 @@ def test_cache_write_tokens_not_subtracted_from_input(mock_client):
20832083
assert generation_props["$ai_output_tokens"] == 20
20842084
assert generation_props["$ai_cache_creation_input_tokens"] == 800
20852085
assert generation_props["$ai_cache_read_input_tokens"] == 0
2086+
2087+
2088+
def test_agent_action_and_finish_imports():
2089+
"""
2090+
Regression test for LangChain 1.0+ compatibility (Issue #362).
2091+
Verifies that AgentAction and AgentFinish can be imported and used.
2092+
This test ensures the imports work with both LangChain 0.x and 1.0+.
2093+
"""
2094+
# Import the types that caused the compatibility issue
2095+
try:
2096+
from langchain_core.agents import AgentAction, AgentFinish
2097+
except (ImportError, ModuleNotFoundError):
2098+
from langchain.schema.agent import AgentAction, AgentFinish # type: ignore
2099+
2100+
# Verify they're available in the callbacks module
2101+
from posthog.ai.langchain.callbacks import CallbackHandler
2102+
2103+
# Test on_agent_action with mock data
2104+
mock_client = MagicMock()
2105+
callbacks = CallbackHandler(mock_client)
2106+
run_id = uuid.uuid4()
2107+
parent_run_id = uuid.uuid4()
2108+
2109+
# Create mock AgentAction
2110+
action = AgentAction(tool="test_tool", tool_input="test_input", log="test_log")
2111+
2112+
# Should not raise an exception
2113+
callbacks.on_agent_action(action, run_id=run_id, parent_run_id=parent_run_id)
2114+
2115+
# Verify parent was set
2116+
assert run_id in callbacks._parent_tree
2117+
assert callbacks._parent_tree[run_id] == parent_run_id
2118+
2119+
# Test on_agent_finish with mock data
2120+
finish = AgentFinish(return_values={"output": "test_output"}, log="finish_log")
2121+
2122+
# Should not raise an exception
2123+
callbacks.on_agent_finish(finish, run_id=run_id, parent_run_id=parent_run_id)
2124+
2125+
# Verify capture was called
2126+
assert mock_client.capture.call_count == 1
2127+
call_args = mock_client.capture.call_args[1]
2128+
assert call_args["event"] == "$ai_span"

0 commit comments

Comments
 (0)