Skip to content

Commit fe69498

Browse files
committed
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.
1 parent b5b11ee commit fe69498

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

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"

0 commit comments

Comments
 (0)