Home / Function/ merge() — langchain Function Reference

merge() — langchain Function Reference

Architecture documentation for the merge() function in manager.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  40baea86_dfe8_0c6b_cff8_57b3fde6c2ee["merge()"]
  c39f1e6c_ff8b_15f7_1623_83b1b6b328ac["AsyncCallbackManagerForChainGroup"]
  40baea86_dfe8_0c6b_cff8_57b3fde6c2ee -->|defined in| c39f1e6c_ff8b_15f7_1623_83b1b6b328ac
  9afe8ec3_6265_0bfd_b9aa_231ee6084fa3["merge()"]
  9afe8ec3_6265_0bfd_b9aa_231ee6084fa3 -->|calls| 40baea86_dfe8_0c6b_cff8_57b3fde6c2ee
  9afe8ec3_6265_0bfd_b9aa_231ee6084fa3["merge()"]
  40baea86_dfe8_0c6b_cff8_57b3fde6c2ee -->|calls| 9afe8ec3_6265_0bfd_b9aa_231ee6084fa3
  57f655ed_89b3_c486_de7e_73ebe360134a["atrace_as_chain_group()"]
  40baea86_dfe8_0c6b_cff8_57b3fde6c2ee -->|calls| 57f655ed_89b3_c486_de7e_73ebe360134a
  style 40baea86_dfe8_0c6b_cff8_57b3fde6c2ee fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/callbacks/manager.py lines 2209–2269

    def merge(
        self: AsyncCallbackManagerForChainGroup, other: BaseCallbackManager
    ) -> AsyncCallbackManagerForChainGroup:
        """Merge the group callback manager with another callback manager.

        Overwrites the merge method in the base class to ensure that the parent run
        manager is preserved. Keeps the `parent_run_manager` from the current object.

        Returns:
            A copy of the current `AsyncCallbackManagerForChainGroup` with the handlers,
                tags, etc. of the other callback manager merged in.

        Example:
            ```python
            # Merging two callback managers
            from langchain_core.callbacks.manager import (
                CallbackManager,
                atrace_as_chain_group,
            )
            from langchain_core.callbacks.stdout import StdOutCallbackHandler

            manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
            async with atrace_as_chain_group(
                "My Group Name", tags=["tag1"]
            ) as group_manager:
                merged_manager = group_manager.merge(manager)
                print(type(merged_manager))
                # <class 'langchain_core.callbacks.manager.AsyncCallbackManagerForChainGroup'>

                print(merged_manager.handlers)
                # [
                #    <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
                #    <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
                # ]

                print(merged_manager.tags)
                #    ['tag2', 'tag1']
            ```
        """  # noqa: E501
        manager = self.__class__(
            parent_run_id=self.parent_run_id or other.parent_run_id,
            handlers=[],
            inheritable_handlers=[],
            tags=list(set(self.tags + other.tags)),
            inheritable_tags=list(set(self.inheritable_tags + other.inheritable_tags)),
            metadata={
                **self.metadata,
                **other.metadata,
            },
            parent_run_manager=self.parent_run_manager,
        )

        handlers = self.handlers + other.handlers
        inheritable_handlers = self.inheritable_handlers + other.inheritable_handlers

        for handler in handlers:
            manager.add_handler(handler)

        for handler in inheritable_handlers:
            manager.add_handler(handler, inherit=True)
        return manager

Domain

Subdomains

Called By

Frequently Asked Questions

What does merge() do?
merge() is a function in the langchain codebase, defined in libs/core/langchain_core/callbacks/manager.py.
Where is merge() defined?
merge() is defined in libs/core/langchain_core/callbacks/manager.py at line 2209.
What does merge() call?
merge() calls 2 function(s): atrace_as_chain_group, merge.
What calls merge()?
merge() is called by 1 function(s): merge.

Analyze Your Own Codebase

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

Try Supermodel Free