LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-corecallbacksmanageradispatch_custom_event
    Function●Since v0.2

    adispatch_custom_event

    Copy
    adispatch_custom_event(
      name: str,
      data: Any,
      *,
      config: RunnableConfig | None 
    View source on GitHub
    =
    None
    )
    ->
    None

    Parameters

    NameTypeDescription
    name*str
    data*Any
    configRunnableConfig | None
    Default:None

    Dispatch an adhoc event to the handlers.

    Example:

    from langchain_core.callbacks import (
        AsyncCallbackHandler,
        adispatch_custom_event
    )
    from langchain_core.runnable import RunnableLambda
    
    class CustomCallbackManager(AsyncCallbackHandler):
        async def on_custom_event(
            self,
            name: str,
            data: Any,
            *,
            run_id: UUID,
            tags: list[str] | None = None,
            metadata: dict[str, Any] | None = None,
            **kwargs: Any,
        ) -> None:
            print(f"Received custom event: {name} with data: {data}")
    
    callback = CustomCallbackManager()
    
    async def foo(inputs):
        await adispatch_custom_event("my_event", {"bar": "buzz})
        return inputs
    
    foo_ = RunnableLambda(foo)
    await foo_.ainvoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})

    Example: Use with astream events

    from langchain_core.callbacks import (
        AsyncCallbackHandler,
        adispatch_custom_event
    )
    from langchain_core.runnable import RunnableLambda
    
    class CustomCallbackManager(AsyncCallbackHandler):
        async def on_custom_event(
            self,
            name: str,
            data: Any,
            *,
            run_id: UUID,
            tags: list[str] | None = None,
            metadata: dict[str, Any] | None = None,
            **kwargs: Any,
        ) -> None:
            print(f"Received custom event: {name} with data: {data}")
    
    callback = CustomCallbackManager()
    
    async def foo(inputs):
        await adispatch_custom_event("event_type_1", {"bar": "buzz})
        await adispatch_custom_event("event_type_2", 5)
        return inputs
    
    foo_ = RunnableLambda(foo)
    
    async for event in foo_.ainvoke_stream(
        {"a": "1"},
        version="v2",
        config={"callbacks": [CustomCallbackManager()]}
    ):
        print(event)
    Warning

    If using python 3.10 and async, you MUST specify the config parameter or the function will raise an error. This is due to a limitation in asyncio for python 3.10 that prevents LangChain from automatically propagating the config object on the user's behalf.

    The name of the adhoc event.

    The data for the adhoc event.

    Free form data. Ideally should be JSON serializable to avoid serialization issues downstream, but this is not enforced.

    Optional config object.

    Mirrors the async API but not strictly needed.