AsyncCallbackManager#

class langchain_core.callbacks.manager.AsyncCallbackManager(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]#

Async callback manager that handles callbacks from 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

Return whether the handler 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.

configure([inheritable_callbacks, ...])

Configure the async callback manager.

copy()

Copy the callback manager.

merge(other)

Merge the callback manager with another callback manager.

on_chain_start(serialized, inputs[, run_id])

Async run when chain starts running.

on_chat_model_start(serialized, messages[, ...])

Async run when LLM starts running.

on_custom_event(name, data[, run_id])

Dispatch an adhoc event to the handlers (async version).

on_llm_start(serialized, prompts[, run_id])

Run when LLM starts running.

on_retriever_start(serialized, query[, ...])

Run when the retriever starts running.

on_tool_start(serialized, input_str[, ...])

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#

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#

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#

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#

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

classmethod configure(inheritable_callbacks: List[BaseCallbackHandler] | BaseCallbackManager | None = None, local_callbacks: List[BaseCallbackHandler] | BaseCallbackManager | None = None, verbose: bool = False, inheritable_tags: List[str] | None = None, local_tags: List[str] | None = None, inheritable_metadata: Dict[str, Any] | None = None, local_metadata: Dict[str, Any] | None = None) AsyncCallbackManager[source]#

Configure the async callback manager.

Parameters:
  • inheritable_callbacks (Optional[Callbacks], optional) – The inheritable callbacks. Defaults to None.

  • local_callbacks (Optional[Callbacks], optional) – The local callbacks. Defaults to None.

  • verbose (bool, optional) – Whether to enable verbose mode. Defaults to False.

  • inheritable_tags (Optional[List[str]], optional) – The inheritable tags. Defaults to None.

  • local_tags (Optional[List[str]], optional) – The local tags. Defaults to None.

  • inheritable_metadata (Optional[Dict[str, Any]], optional) – The inheritable metadata. Defaults to None.

  • local_metadata (Optional[Dict[str, Any]], optional) – The local metadata. Defaults to None.

Returns:

The configured async callback manager.

Return type:

AsyncCallbackManager

copy() T#

Copy the callback manager.

Parameters:

self (T) –

Return type:

T

merge(other: BaseCallbackManager) T#

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']
async on_chain_start(serialized: Dict[str, Any] | None, inputs: Dict[str, Any] | Any, run_id: UUID | None = None, **kwargs: Any) AsyncCallbackManagerForChainRun[source]#

Async run when chain starts running.

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

  • inputs (Union[Dict[str, Any], Any]) – The inputs to the chain.

  • run_id (UUID, optional) – The ID of the run. Defaults to None.

  • **kwargs (Any) – Additional keyword arguments.

Returns:

The async callback manager

for the chain run.

Return type:

AsyncCallbackManagerForChainRun

async on_chat_model_start(serialized: Dict[str, Any], messages: List[List[BaseMessage]], run_id: UUID | None = None, **kwargs: Any) List[AsyncCallbackManagerForLLMRun][source]#

Async run when LLM starts running.

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

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

  • run_id (UUID, optional) – The ID of the run. Defaults to None.

  • **kwargs (Any) – Additional keyword arguments.

Returns:

The list of

async callback managers, one for each LLM Run corresponding to each inner message list.

Return type:

List[AsyncCallbackManagerForLLMRun]

async on_custom_event(name: str, data: Any, run_id: UUID | None = None, **kwargs: Any) None[source]#

Dispatch an adhoc event to the handlers (async version).

This event should NOT be used in any internal LangChain code. The event is meant specifically for users of the library to dispatch custom events that are tailored to their application.

Parameters:
  • name (str) – The name of the adhoc event.

  • data (Any) – The data for the adhoc event.

  • run_id (UUID | None) – The ID of the run. Defaults to None.

  • kwargs (Any) –

Return type:

None

New in version 0.2.14.

async on_llm_start(serialized: Dict[str, Any], prompts: List[str], run_id: UUID | None = None, **kwargs: Any) List[AsyncCallbackManagerForLLMRun][source]#

Run when LLM starts running.

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

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

  • run_id (UUID, optional) – The ID of the run. Defaults to None.

  • **kwargs (Any) – Additional keyword arguments.

Returns:

The list of async

callback managers, one for each LLM Run corresponding to each prompt.

Return type:

List[AsyncCallbackManagerForLLMRun]

async on_retriever_start(serialized: Dict[str, Any] | None, query: str, run_id: UUID | None = None, parent_run_id: UUID | None = None, **kwargs: Any) AsyncCallbackManagerForRetrieverRun[source]#

Run when the retriever starts running.

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

  • query (str) – The query.

  • run_id (UUID, optional) – The ID of the run. Defaults to None.

  • parent_run_id (UUID, optional) – The ID of the parent run. Defaults to None.

  • **kwargs (Any) – Additional keyword arguments.

Returns:

The async callback manager

for the retriever run.

Return type:

AsyncCallbackManagerForRetrieverRun

async on_tool_start(serialized: Dict[str, Any] | None, input_str: str, run_id: UUID | None = None, parent_run_id: UUID | None = None, **kwargs: Any) AsyncCallbackManagerForToolRun[source]#

Run when the tool starts running.

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

  • input_str (str) – The input to the tool.

  • run_id (UUID, optional) – The ID of the run. Defaults to None.

  • parent_run_id (UUID, optional) – The ID of the parent run. Defaults to None.

  • **kwargs (Any) – Additional keyword arguments.

Returns:

The async callback manager

for the tool run.

Return type:

AsyncCallbackManagerForToolRun

remove_handler(handler: BaseCallbackHandler) None#

Remove a handler from the callback manager.

Parameters:

handler (BaseCallbackHandler) – The handler to remove.

Return type:

None

remove_metadata(keys: List[str]) None#

Remove metadata from the callback manager.

Parameters:

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

Return type:

None

remove_tags(tags: List[str]) None#

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#

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#

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