@@ -610,6 +610,28 @@ async def on_chain_end(
610
610
run_type ,
611
611
)
612
612
613
+ def _get_tool_run_info_with_inputs (self , run_id : UUID ) -> tuple [RunInfo , Any ]:
614
+ """Get run info for a tool and extract inputs, with validation.
615
+
616
+ Args:
617
+ run_id: The run ID of the tool.
618
+
619
+ Returns:
620
+ A tuple of (run_info, inputs).
621
+
622
+ Raises:
623
+ AssertionError: If the run ID is a tool call and does not have inputs.
624
+ """
625
+ run_info = self .run_map .pop (run_id )
626
+ if "inputs" not in run_info :
627
+ msg = (
628
+ f"Run ID { run_id } is a tool call and is expected to have "
629
+ f"inputs associated with it."
630
+ )
631
+ raise AssertionError (msg )
632
+ inputs = run_info ["inputs" ]
633
+ return run_info , inputs
634
+
613
635
@override
614
636
async def on_tool_start (
615
637
self ,
@@ -652,21 +674,43 @@ async def on_tool_start(
652
674
"tool" ,
653
675
)
654
676
677
+ @override
678
+ async def on_tool_error (
679
+ self ,
680
+ error : BaseException ,
681
+ * ,
682
+ run_id : UUID ,
683
+ parent_run_id : Optional [UUID ] = None ,
684
+ tags : Optional [list [str ]] = None ,
685
+ ** kwargs : Any ,
686
+ ) -> None :
687
+ """Run when tool errors."""
688
+ run_info , inputs = self ._get_tool_run_info_with_inputs (run_id )
689
+
690
+ self ._send (
691
+ {
692
+ "event" : "on_tool_error" ,
693
+ "data" : {
694
+ "error" : error ,
695
+ "input" : inputs ,
696
+ },
697
+ "run_id" : str (run_id ),
698
+ "name" : run_info ["name" ],
699
+ "tags" : run_info ["tags" ],
700
+ "metadata" : run_info ["metadata" ],
701
+ "parent_ids" : self ._get_parent_ids (run_id ),
702
+ },
703
+ "tool" ,
704
+ )
705
+
655
706
@override
656
707
async def on_tool_end (self , output : Any , * , run_id : UUID , ** kwargs : Any ) -> None :
657
708
"""End a trace for a tool run.
658
709
659
710
Raises:
660
711
AssertionError: If the run ID is a tool call and does not have inputs
661
712
"""
662
- run_info = self .run_map .pop (run_id )
663
- if "inputs" not in run_info :
664
- msg = (
665
- f"Run ID { run_id } is a tool call and is expected to have "
666
- f"inputs associated with it."
667
- )
668
- raise AssertionError (msg )
669
- inputs = run_info ["inputs" ]
713
+ run_info , inputs = self ._get_tool_run_info_with_inputs (run_id )
670
714
671
715
self ._send (
672
716
{
0 commit comments