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-corecallbacksmanagerdispatch_custom_event
    Function●Since v0.2

    dispatch_custom_event

    Dispatch an adhoc event.

    Copy
    dispatch_custom_event(
      name: str,
      data: Any,
      *,
      config: RunnableConfig | None = None
    ) -> None

    Example:

    from langchain_core.callbacks import BaseCallbackHandler
    from langchain_core.callbacks import dispatch_custom_event
    from langchain_core.runnable import RunnableLambda
    
    class CustomCallbackManager(BaseCallbackHandler):
        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}")
    
    def foo(inputs):
        dispatch_custom_event("my_event", {"bar": "buzz})
        return inputs
    
    foo_ = RunnableLambda(foo)
    foo_.invoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})

    Parameters

    NameTypeDescription
    name*str

    The name of the adhoc event.

    data*Any

    The data for the adhoc event.

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

    configRunnableConfig | None
    Default:None

    Optional config object.

    Mirrors the async API but not strictly needed.

    View source on GitHub