BaseCallbackManager#

class langchain_core.callbacks.base.BaseCallbackManager(handlers: List[BaseCallbackHandler], inheritable_handlers: List[BaseCallbackHandler] | None = None, parent_run_id: UUID | None = None, *, tags: List[str] | None = None, inheritable_tags: List[str] | None = None, metadata: Dict[str, Any] | None = None, inheritable_metadata: Dict[str, Any] | None = None)[source]#

Base callback manager for LangChain.

Initialize callback manager.

Parameters:
  • handlers (List[BaseCallbackHandler]) – The handlers.

  • inheritable_handlers (Optional[List[BaseCallbackHandler]]) – The inheritable handlers. Default is None.

  • parent_run_id (Optional[UUID]) – The parent run ID. Default is None.

  • tags (Optional[List[str]]) – The tags. Default is None.

  • inheritable_tags (Optional[List[str]]) – The inheritable tags. Default is None.

  • metadata (Optional[Dict[str, Any]]) – The metadata. Default is None.

  • inheritable_metadata (Optional[Dict[str, Any]]) –

Attributes

is_async

Whether the callback manager is async.

Methods

__init__(handlers[, inheritable_handlers, ...])

Initialize callback manager.

add_handler(handler[, inherit])

Add a handler to the callback manager.

add_metadata(metadata[, inherit])

Add metadata to the callback manager.

add_tags(tags[, inherit])

Add tags to the callback manager.

copy()

Copy the callback manager.

merge(other)

Merge the callback manager with another callback manager.

on_chain_start(serialized, inputs, *, run_id)

Run when a chain starts running.

on_chat_model_start(serialized, messages, *, ...)

Run when a chat model starts running.

on_llm_start(serialized, prompts, *, run_id)

Run when LLM starts running.

on_retriever_start(serialized, query, *, run_id)

Run when the Retriever starts running.

on_tool_start(serialized, input_str, *, run_id)

Run when the tool starts running.

remove_handler(handler)

Remove a handler from the callback manager.

remove_metadata(keys)

Remove metadata from the callback manager.

remove_tags(tags)

Remove tags from the callback manager.

set_handler(handler[, inherit])

Set handler as the only handler on the callback manager.

set_handlers(handlers[, inherit])

Set handlers as the only handlers on the callback manager.

__init__(handlers: List[BaseCallbackHandler], inheritable_handlers: List[BaseCallbackHandler] | None = None, parent_run_id: UUID | None = None, *, tags: List[str] | None = None, inheritable_tags: List[str] | None = None, metadata: Dict[str, Any] | None = None, inheritable_metadata: Dict[str, Any] | None = None) None[source]#

Initialize callback manager.

Parameters:
  • handlers (List[BaseCallbackHandler]) – The handlers.

  • inheritable_handlers (Optional[List[BaseCallbackHandler]]) – The inheritable handlers. Default is None.

  • parent_run_id (Optional[UUID]) – The parent run ID. Default is None.

  • tags (Optional[List[str]]) – The tags. Default is None.

  • inheritable_tags (Optional[List[str]]) – The inheritable tags. Default is None.

  • metadata (Optional[Dict[str, Any]]) – The metadata. Default is None.

  • inheritable_metadata (Dict[str, Any] | None) –

Return type:

None

add_handler(handler: BaseCallbackHandler, inherit: bool = True) None[source]#

Add a handler to the callback manager.

Parameters:
  • handler (BaseCallbackHandler) – The handler to add.

  • inherit (bool) – Whether to inherit the handler. Default is True.

Return type:

None

add_metadata(metadata: Dict[str, Any], inherit: bool = True) None[source]#

Add metadata to the callback manager.

Parameters:
  • metadata (Dict[str, Any]) – The metadata to add.

  • inherit (bool) – Whether to inherit the metadata. Default is True.

Return type:

None

add_tags(tags: List[str], inherit: bool = True) None[source]#

Add tags to the callback manager.

Parameters:
  • tags (List[str]) – The tags to add.

  • inherit (bool) – Whether to inherit the tags. Default is True.

Return type:

None

copy() T[source]#

Copy the callback manager.

Parameters:

self (T) –

Return type:

T

merge(other: BaseCallbackManager) T[source]#

Merge the callback manager with another callback manager.

May be overwritten in subclasses. Primarily used internally within merge_configs.

Returns:

The merged callback manager of the same type

as the current object.

Return type:

BaseCallbackManager

Parameters:

Example: Merging two callback managers.

from langchain_core.callbacks.manager import CallbackManager, trace_as_chain_group
from langchain_core.callbacks.stdout import StdOutCallbackHandler

manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
with trace_as_chain_group("My Group Name", tags=["tag1"]) as group_manager:
    merged_manager = group_manager.merge(manager)
    print(merged_manager.handlers)
    # [
    #    <langchain_core.callbacks.stdout.StdOutCallbackHandler object at ...>,
    #    <langchain_core.callbacks.streaming_stdout.StreamingStdOutCallbackHandler object at ...>,
    # ]

    print(merged_manager.tags)
    #    ['tag2', 'tag1']
on_chain_start(serialized: Dict[str, Any], inputs: Dict[str, Any], *, run_id: UUID, parent_run_id: UUID | None = None, tags: List[str] | None = None, metadata: Dict[str, Any] | None = None, **kwargs: Any) Any#

Run when a chain starts running.

Parameters:
  • serialized (Dict[str, Any]) – The serialized chain.

  • inputs (Dict[str, Any]) – The inputs.

  • run_id (UUID) – The run ID. This is the ID of the current run.

  • parent_run_id (UUID) – The parent run ID. This is the ID of the parent run.

  • tags (Optional[List[str]]) – The tags.

  • metadata (Optional[Dict[str, Any]]) – The metadata.

  • kwargs (Any) – Additional keyword arguments.

Return type:

Any

on_chat_model_start(serialized: Dict[str, Any], messages: List[List[BaseMessage]], *, run_id: UUID, parent_run_id: UUID | None = None, tags: List[str] | None = None, metadata: Dict[str, Any] | None = None, **kwargs: Any) Any#

Run when a chat model starts running.

ATTENTION: This method is called for chat models. If you’re implementing

a handler for a non-chat model, you should use on_llm_start instead.

Parameters:
  • serialized (Dict[str, Any]) – The serialized chat model.

  • messages (List[List[BaseMessage]]) – The messages.

  • run_id (UUID) – The run ID. This is the ID of the current run.

  • parent_run_id (UUID) – The parent run ID. This is the ID of the parent run.

  • tags (Optional[List[str]]) – The tags.

  • metadata (Optional[Dict[str, Any]]) – The metadata.

  • kwargs (Any) – Additional keyword arguments.

Return type:

Any

on_llm_start(serialized: Dict[str, Any], prompts: List[str], *, run_id: UUID, parent_run_id: UUID | None = None, tags: List[str] | None = None, metadata: Dict[str, Any] | None = None, **kwargs: Any) Any#

Run when LLM starts running.

ATTENTION: This method is called for non-chat models (regular LLMs). If

you’re implementing a handler for a chat model, you should use on_chat_model_start instead.

Parameters:
  • serialized (Dict[str, Any]) – The serialized LLM.

  • prompts (List[str]) – The prompts.

  • run_id (UUID) – The run ID. This is the ID of the current run.

  • parent_run_id (UUID) – The parent run ID. This is the ID of the parent run.

  • tags (Optional[List[str]]) – The tags.

  • metadata (Optional[Dict[str, Any]]) – The metadata.

  • kwargs (Any) – Additional keyword arguments.

Return type:

Any

on_retriever_start(serialized: Dict[str, Any], query: str, *, run_id: UUID, parent_run_id: UUID | None = None, tags: List[str] | None = None, metadata: Dict[str, Any] | None = None, **kwargs: Any) Any#

Run when the Retriever starts running.

Parameters:
  • serialized (Dict[str, Any]) – The serialized Retriever.

  • query (str) – The query.

  • run_id (UUID) – The run ID. This is the ID of the current run.

  • parent_run_id (UUID) – The parent run ID. This is the ID of the parent run.

  • tags (Optional[List[str]]) – The tags.

  • metadata (Optional[Dict[str, Any]]) – The metadata.

  • kwargs (Any) – Additional keyword arguments.

Return type:

Any

on_tool_start(serialized: Dict[str, Any], input_str: str, *, run_id: UUID, parent_run_id: UUID | None = None, tags: List[str] | None = None, metadata: Dict[str, Any] | None = None, inputs: Dict[str, Any] | None = None, **kwargs: Any) Any#

Run when the tool starts running.

Parameters:
  • serialized (Dict[str, Any]) – The serialized tool.

  • input_str (str) – The input string.

  • run_id (UUID) – The run ID. This is the ID of the current run.

  • parent_run_id (UUID) – The parent run ID. This is the ID of the parent run.

  • tags (Optional[List[str]]) – The tags.

  • metadata (Optional[Dict[str, Any]]) – The metadata.

  • inputs (Optional[Dict[str, Any]]) – The inputs.

  • kwargs (Any) – Additional keyword arguments.

Return type:

Any

remove_handler(handler: BaseCallbackHandler) None[source]#

Remove a handler from the callback manager.

Parameters:

handler (BaseCallbackHandler) – The handler to remove.

Return type:

None

remove_metadata(keys: List[str]) None[source]#

Remove metadata from the callback manager.

Parameters:

keys (List[str]) – The keys to remove.

Return type:

None

remove_tags(tags: List[str]) None[source]#

Remove tags from the callback manager.

Parameters:

tags (List[str]) – The tags to remove.

Return type:

None

set_handler(handler: BaseCallbackHandler, inherit: bool = True) None[source]#

Set handler as the only handler on the callback manager.

Parameters:
  • handler (BaseCallbackHandler) – The handler to set.

  • inherit (bool) – Whether to inherit the handler. Default is True.

Return type:

None

set_handlers(handlers: List[BaseCallbackHandler], inherit: bool = True) None[source]#

Set handlers as the only handlers on the callback manager.

Parameters:
  • handlers (List[BaseCallbackHandler]) – The handlers to set.

  • inherit (bool) – Whether to inherit the handlers. Default is True.

Return type:

None