@@ -163,7 +163,7 @@ def on_chain_error(
163163 ** kwargs : Any ,
164164 ):
165165 self ._log_debug_event ("on_chain_error" , run_id , parent_run_id , error = error )
166- self ._pop_run_and_capture_trace_or_span (run_id , parent_run_id , str ( error ) )
166+ self ._pop_run_and_capture_trace_or_span (run_id , parent_run_id , error )
167167
168168 def on_chat_model_start (
169169 self ,
@@ -262,7 +262,7 @@ def on_tool_error(
262262 ** kwargs : Any ,
263263 ) -> Any :
264264 self ._log_debug_event ("on_tool_error" , run_id , parent_run_id , error = error )
265- self ._pop_run_and_capture_trace_or_span (run_id , parent_run_id , str ( error ) )
265+ self ._pop_run_and_capture_trace_or_span (run_id , parent_run_id , error )
266266
267267 def on_retriever_start (
268268 self ,
@@ -299,7 +299,7 @@ def on_retriever_error(
299299 ) -> Any :
300300 """Run when Retriever errors."""
301301 self ._log_debug_event ("on_retriever_error" , run_id , parent_run_id , error = error )
302- self ._pop_run_and_capture_trace_or_span (run_id , parent_run_id , str ( error ) )
302+ self ._pop_run_and_capture_trace_or_span (run_id , parent_run_id , error )
303303
304304 def on_agent_action (
305305 self ,
@@ -448,8 +448,13 @@ def _capture_trace_or_span(
448448 event_properties ["$ai_trace_name" ] = run .name
449449 if self ._properties :
450450 event_properties .update (self ._properties )
451- if outputs is not None :
451+
452+ if isinstance (outputs , BaseException ):
453+ event_properties ["$ai_error" ] = _stringify_exception (outputs )
454+ event_properties ["$ai_is_error" ] = True
455+ elif outputs is not None :
452456 event_properties ["$ai_output_state" ] = with_privacy_mode (self ._client , self ._privacy_mode , outputs )
457+
453458 if self ._distinct_id is None :
454459 event_properties ["$process_person_profile" ] = False
455460
@@ -498,7 +503,7 @@ def _capture_generation(
498503
499504 if isinstance (output , BaseException ):
500505 event_properties ["$ai_http_status" ] = _get_http_status (output )
501- event_properties ["$ai_error" ] = str (output )
506+ event_properties ["$ai_error" ] = _stringify_exception (output )
502507 event_properties ["$ai_is_error" ] = True
503508 else :
504509 # Add usage
@@ -687,3 +692,10 @@ def _get_langchain_run_name(serialized: Optional[Dict[str, Any]], **kwargs: Any)
687692 except (KeyError , TypeError ):
688693 pass
689694 return None
695+
696+
697+ def _stringify_exception (exception : BaseException ) -> str :
698+ description = str (exception )
699+ if description :
700+ return f"{ exception .__class__ .__name__ } : { description } "
701+ return exception .__class__ .__name__
0 commit comments