Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Langchain ai Langchain Streaming Events

From Leeroopedia
Knowledge Sources
Domains Streaming, Observability
Last Updated 2026-02-11 00:00 GMT

Overview

An event-driven streaming API that provides structured lifecycle events from all Runnables in a chain, enabling fine-grained observability of complex pipelines.

Description

While stream() provides chunks from a single model, astream_events() provides structured events from every Runnable in a composed chain. Events include start, stream (per-chunk), and end for each component. This enables monitoring retrieval, formatting, model generation, and parsing stages independently.

Events follow the format: on_{runnable_type}_{start|stream|end} with metadata including name, run_id, parent_ids, tags, and data.

Usage

Use astream_events() when building complex chains and you need visibility into intermediate steps, or when building UIs that display progress from multiple chain components.

Theoretical Basis

# Abstract event stream (not real code)
async for event in chain.astream_events(input, version="v2"):
    if event["event"] == "on_chat_model_stream":
        display(event["data"]["chunk"].content)
    elif event["event"] == "on_retriever_end":
        show_sources(event["data"]["output"])

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment