[docs]classRootListenersTracer(BaseTracer):"""Tracer that calls listeners on run start, end, and error. Parameters: log_missing_parent: Whether to log a warning if the parent is missing. Default is False. config: The runnable config. on_start: The listener to call on run start. on_end: The listener to call on run end. on_error: The listener to call on run error. """log_missing_parent=False
[docs]def__init__(self,*,config:RunnableConfig,on_start:Optional[Listener],on_end:Optional[Listener],on_error:Optional[Listener],)->None:"""Initialize the tracer. Args: config: The runnable config. on_start: The listener to call on run start. on_end: The listener to call on run end. on_error: The listener to call on run error """super().__init__(_schema_format="original+chat")self.config=configself._arg_on_start=on_startself._arg_on_end=on_endself._arg_on_error=on_errorself.root_id:Optional[UUID]=None
def_persist_run(self,run:Run)->None:# This is a legacy method only called once for an entire run tree# therefore not useful herepassdef_on_run_create(self,run:Run)->None:ifself.root_idisnotNone:returnself.root_id=run.idifself._arg_on_startisnotNone:call_func_with_variable_args(self._arg_on_start,run,self.config)def_on_run_update(self,run:Run)->None:ifrun.id!=self.root_id:returnifrun.errorisNone:ifself._arg_on_endisnotNone:call_func_with_variable_args(self._arg_on_end,run,self.config)else:ifself._arg_on_errorisnotNone:call_func_with_variable_args(self._arg_on_error,run,self.config)
[docs]classAsyncRootListenersTracer(AsyncBaseTracer):"""Async Tracer that calls listeners on run start, end, and error. Parameters: log_missing_parent: Whether to log a warning if the parent is missing. Default is False. config: The runnable config. on_start: The listener to call on run start. on_end: The listener to call on run end. on_error: The listener to call on run error. """log_missing_parent=False
[docs]def__init__(self,*,config:RunnableConfig,on_start:Optional[AsyncListener],on_end:Optional[AsyncListener],on_error:Optional[AsyncListener],)->None:"""Initialize the tracer. Args: config: The runnable config. on_start: The listener to call on run start. on_end: The listener to call on run end. on_error: The listener to call on run error """super().__init__(_schema_format="original+chat")self.config=configself._arg_on_start=on_startself._arg_on_end=on_endself._arg_on_error=on_errorself.root_id:Optional[UUID]=None
asyncdef_persist_run(self,run:Run)->None:# This is a legacy method only called once for an entire run tree# therefore not useful herepassasyncdef_on_run_create(self,run:Run)->None:ifself.root_idisnotNone:returnself.root_id=run.idifself._arg_on_startisnotNone:awaitacall_func_with_variable_args(self._arg_on_start,run,self.config)asyncdef_on_run_update(self,run:Run)->None:ifrun.id!=self.root_id:returnifrun.errorisNone:ifself._arg_on_endisnotNone:awaitacall_func_with_variable_args(self._arg_on_end,run,self.config)else:ifself._arg_on_errorisnotNone:awaitacall_func_with_variable_args(self._arg_on_error,run,self.config)