Source code for langchain_core.tracers.run_collector
"""A tracer that collects all nested runs in a list."""fromtypingimportAny,Optional,UnionfromuuidimportUUIDfromlangchain_core.tracers.baseimportBaseTracerfromlangchain_core.tracers.schemasimportRun
[docs]classRunCollectorCallbackHandler(BaseTracer):"""Tracer that collects all nested runs in a list. This tracer is useful for inspection and evaluation purposes. Parameters ---------- name : str, default="run-collector_callback_handler" example_id : Optional[Union[UUID, str]], default=None The ID of the example being traced. It can be either a UUID or a string. """name:str="run-collector_callback_handler"
[docs]def__init__(self,example_id:Optional[Union[UUID,str]]=None,**kwargs:Any)->None:"""Initialize the RunCollectorCallbackHandler. Parameters ---------- example_id : Optional[Union[UUID, str]], default=None The ID of the example being traced. It can be either a UUID or a string. **kwargs : Any Additional keyword arguments """super().__init__(**kwargs)self.example_id=(UUID(example_id)ifisinstance(example_id,str)elseexample_id)self.traced_runs:list[Run]=[]
def_persist_run(self,run:Run)->None:"""Persist a run by adding it to the traced_runs list. Parameters ---------- run : Run The run to be persisted. """run_=run.copy()run_.reference_example_id=self.example_idself.traced_runs.append(run_)