Home / Class/ _TracerCore Class — langchain Architecture

_TracerCore Class — langchain Architecture

Architecture documentation for the _TracerCore class in core.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa["_TracerCore"]
  f4021fab_f34e_b0c4_272d_54fcccd4097b["core.py"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|defined in| f4021fab_f34e_b0c4_272d_54fcccd4097b
  52630f4b_8b65_8eb9_14f8_791f76adb374["__init__()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| 52630f4b_8b65_8eb9_14f8_791f76adb374
  aa1f5e08_4d58_d36d_4c3a_a0ff53142297["_persist_run()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| aa1f5e08_4d58_d36d_4c3a_a0ff53142297
  54900518_8055_919d_2bbb_96df7b268f0c["_add_child_run()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| 54900518_8055_919d_2bbb_96df7b268f0c
  84e4047e_7cf9_98bb_d281_e64bb894a439["_get_stacktrace()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| 84e4047e_7cf9_98bb_d281_e64bb894a439
  b78d6151_8db8_8c1f_994c_f8a309156406["_start_trace()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| b78d6151_8db8_8c1f_994c_f8a309156406
  60a1c951_2591_b7a1_1501_c0c8becf68b6["_get_run()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| 60a1c951_2591_b7a1_1501_c0c8becf68b6
  12828f30_a6f6_b6b8_865e_eb4a145286a0["_create_chat_model_run()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| 12828f30_a6f6_b6b8_865e_eb4a145286a0
  d9088b27_9532_7e47_7d9b_5fdf40717153["_create_llm_run()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| d9088b27_9532_7e47_7d9b_5fdf40717153
  73d8076a_af1f_99fa_81f2_4431544ca37d["_llm_run_with_token_event()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| 73d8076a_af1f_99fa_81f2_4431544ca37d
  ae4ec01b_2322_17e1_9f7d_eef9ef8fb3b9["_llm_run_with_retry_event()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| ae4ec01b_2322_17e1_9f7d_eef9ef8fb3b9
  c3e7478e_4d35_2efc_8193_2d0711655c88["_complete_llm_run()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| c3e7478e_4d35_2efc_8193_2d0711655c88
  bf78d867_b750_cc31_ebbf_ef3ef8a48168["_errored_llm_run()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| bf78d867_b750_cc31_ebbf_ef3ef8a48168
  e9de3c89_7492_97bb_ea3f_bca3f960b4a4["_create_chain_run()"]
  4fc6201d_2217_7ee6_6ae9_980ec7e172fa -->|method| e9de3c89_7492_97bb_ea3f_bca3f960b4a4

Relationship Graph

Source Code

libs/core/langchain_core/tracers/core.py lines 40–705

class _TracerCore(ABC):
    """Abstract base class for tracers.

    This class provides common methods, and reusable methods for tracers.
    """

    log_missing_parent: bool = True

    def __init__(
        self,
        *,
        _schema_format: Literal[
            "original", "streaming_events", "original+chat"
        ] = "original",
        **kwargs: Any,
    ) -> None:
        """Initialize the tracer.

        Args:
            _schema_format: Primarily changes how the inputs and outputs are handled.

                For internal use only. This API will change.

                - `'original'` is the format used by all current tracers.

                    This format is slightly inconsistent with respect to inputs and
                    outputs.
                - `'streaming_events'` is used for supporting streaming events, for
                    internal usage. It will likely change in the future, or be
                    deprecated entirely in favor of a dedicated async tracer for
                    streaming events.
                - `'original+chat'` is a format that is the same as `'original'` except
                    it does NOT raise an attribute error `on_chat_model_start`
            **kwargs: Additional keyword arguments that will be passed to the
                superclass.
        """
        super().__init__(**kwargs)

        self._schema_format = _schema_format  # For internal use only API will change.

        self.run_map: dict[str, Run] = {}
        """Map of run ID to run. Cleared on run end."""

        self.order_map: dict[UUID, tuple[UUID, str]] = {}
        """Map of run ID to (trace_id, dotted_order). Cleared when tracer GCed."""

    @abstractmethod
    def _persist_run(self, run: Run) -> Coroutine[Any, Any, None] | None:
        """Persist a run."""

    @staticmethod
    def _add_child_run(
        parent_run: Run,
        child_run: Run,
    ) -> None:
        """Add child run to a chain run or tool run."""
        parent_run.child_runs.append(child_run)

    @staticmethod
    def _get_stacktrace(error: BaseException) -> str:
        """Get the stacktrace of the parent error."""
        msg = repr(error)
        try:
            tb = traceback.format_exception(error)
            return (msg + "\n\n".join(tb)).strip()
        except Exception:
            return msg

    def _start_trace(self, run: Run) -> Coroutine[Any, Any, None] | None:  # type: ignore[return]
        current_dotted_order = run.start_time.strftime("%Y%m%dT%H%M%S%fZ") + str(run.id)
        if run.parent_run_id:
            if parent := self.order_map.get(run.parent_run_id):
                run.trace_id, run.dotted_order = parent
                run.dotted_order += "." + current_dotted_order
                if parent_run := self.run_map.get(str(run.parent_run_id)):
                    self._add_child_run(parent_run, run)
            else:
                if self.log_missing_parent:
                    logger.debug(
                        "Parent run %s not found for run %s. Treating as a root run.",
                        run.parent_run_id,

Frequently Asked Questions

What is the _TracerCore class?
_TracerCore is a class in the langchain codebase, defined in libs/core/langchain_core/tracers/core.py.
Where is _TracerCore defined?
_TracerCore is defined in libs/core/langchain_core/tracers/core.py at line 40.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free