@@ -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