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-corerunnablesbaseRunnablewith_listeners
    Method●Since v0.1

    with_listeners

    Bind lifecycle listeners to a Runnable, returning a new Runnable.

    The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

    Copy
    with_listeners(
      self,
      *,
      on_start: Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None = None,
      on_end: Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None = None,
      on_error: Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None = None
    ) -> Runnable[Input, Output]

    Example:

    from langchain_core.runnables import RunnableLambda
    from langchain_core.tracers.schemas import Run
    
    import time
    
    def test_runnable(time_to_sleep: int):
        time.sleep(time_to_sleep)
    
    def fn_start(run_obj: Run):
        print("start_time:", run_obj.start_time)
    
    def fn_end(run_obj: Run):
        print("end_time:", run_obj.end_time)
    
    chain = RunnableLambda(test_runnable).with_listeners(
        on_start=fn_start, on_end=fn_end
    )
    chain.invoke(2)

    Parameters

    NameTypeDescription
    on_startCallable[[Run], None] | Callable[[Run, RunnableConfig], None] | None
    Default:None

    Called before the Runnable starts running, with the Run object.

    on_endCallable[[Run], None] | Callable[[Run, RunnableConfig], None] | None
    Default:None

    Called after the Runnable finishes running, with the Run object.

    on_errorCallable[[Run], None] | Callable[[Run, RunnableConfig], None] | None
    Default:None

    Called if the Runnable throws an error, with the Run object.

    View source on GitHub