LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Graphs
    • Functional API
    • Pregel
    • Checkpointing
    • Storage
    • Caching
    • Types
    • Runtime
    • Config
    • Errors
    • Constants
    • Channels
    • Agents
    LangGraph Checkpoint
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewGraphsFunctional APIPregelCheckpointingStorageCachingTypesRuntimeConfigErrorsConstantsChannelsAgents
    LangGraph Checkpoint
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    Language
    Theme
    Pythonlanggraphstream_muxStreamMux
    Class●Since v1.1

    StreamMux

    Copy
    StreamMux(
      self,
      transformers: list[StreamTransformer] | None = None,
      *,
      is_async: 

    Constructors

    Attributes

    Methods

    View source on GitHub
    bool
    =
    False
    ,
    factories
    :
    list
    [
    TransformerFactory
    ]
    |
    None
    =
    None
    ,
    scope
    :
    tuple
    [
    str
    ,
    .
    .
    .
    ]
    =
    (
    )
    ,
    _assign_seq
    :
    bool
    =
    True
    )

    Parameters

    NameTypeDescription
    transformerslist[StreamTransformer] | None
    Default:None

    Already-built transformer instances. Registered only on this mux — they are NOT cloned into child mini-muxes built by _make_child. Use factories for transformers that should propagate to nested scopes.

    is_asyncbool
    Default:False

    True for async dispatch (apush / aclose / afail), False for the sync path.

    factorieslist[TransformerFactory] | None
    Default:None
    scopetuple[str, ...]
    Default:()
    _assign_seqbool
    Default:True
    constructor
    __init__
    NameType
    transformerslist[StreamTransformer] | None
    is_asyncbool
    factorieslist[TransformerFactory] | None
    scopetuple[str, ...]
    _assign_seqbool
    attribute
    is_async: is_async
    attribute
    scope: tuple[str, ...]
    attribute
    extensions: dict[str, Any]
    attribute
    native_keys: set[str]
    method
    transformer_by_key

    Return the transformer that contributed key to the projection.

    method
    bind_pump

    Wire the sync pull callback onto every projection in this mux.

    Records the pump on the mux so child mini-muxes built by _make_child can inherit it. Propagates to:

    • the main event log (self._events)
    • every projection StreamChannel in extensions
    • any registered transformer that exposes _bind_pump (e.g. MessagesTransformer so ChatModelStream instances drive the shared pump from their cursors)
    method
    bind_apump

    Async counterpart to bind_pump.

    method
    push

    Route an event through all transformers, then append to the main log.

    Each transformer's process() is called in registration order. If any transformer returns False, the event is suppressed from the main log, but transformers that already saw it keep their side effects.

    On the root mux, seq is assigned right before an event enters the main log, not before the transformer pipeline runs. This ensures that events auto-forwarded from StreamChannels during process() get earlier seq numbers than the original event, preserving monotonic ordering in the root log. Child muxes do not assign seq, so subgraph forwarding can share event objects without mutating their envelopes.

    method
    close

    Finalize all transformers, close all projections and the main log.

    StreamChannels discovered in transformer projections are auto-closed after finalize() runs — transformers don't need to close them manually. If any transformer's finalize() raises, the remaining transformers, projections, and the main log are still closed; the first error is re-raised after cleanup completes.

    method
    fail

    Fail all transformers, projections, and the main log.

    StreamChannels discovered in transformer projections are auto-failed — transformers don't need to fail them manually. If any transformer's fail() raises, the remaining transformers, projections, and the main log are still failed.

    method
    apush

    Dispatch an event on the async lane.

    Awaits each transformer's aprocess in registration order before appending to the main log. A slow aprocess serializes the pipeline by design — that's the guarantee that lets a later transformer (or a synchronous consumer) see the result of the async work. For decoupled work, use schedule() from inside process / aprocess instead.

    The main log append is a non-blocking push — matching v1's put_nowait shape. The root mux assigns seq; child muxes do not, so forwarded subgraph events can be shared without copying. Memory is bounded by caller pace via the caller-driven pump; see StreamChannel for the full tradeoff story.

    method
    aclose

    Finalize on the async lane.

    Awaits every task started via StreamTransformer.schedule() across all transformers, then calls afinalize() on each, then auto-closes channels and the main event log.

    If any scheduled task raised under on_error="raise", or any transformer's afinalize raises, the exception propagates. The caller (the pump) handles it by routing into afail.

    method
    afail

    Fail on the async lane.

    Cancels every scheduled task across all transformers, awaits them to completion, then runs each transformer's afail hook and auto-fails channels and the main event log.

    Central event dispatcher for the streaming infrastructure.

    Owns the main event log and routes events through a transformer pipeline. StreamChannels with a name discovered in transformer projections are auto-wired so that every push() also injects a ProtocolEvent into the main log. StreamChannels without a name are local-only.

    Pass is_async=True when the mux will be consumed via async iteration (handler.astream()). All StreamChannel instances discovered during registration are automatically bound to the matching mode.

    One-argument callables (scope) -> StreamTransformer. Called once with this mux's scope here, and cloned again per child scope by _make_child so each sub-mux gets fresh instances.

    The namespace the mux operates within. The root mux is ().

    Internal flag for child muxes. Root muxes assign monotonic seq numbers when appending to their main event log; child muxes share forwarded event objects and must not mutate their envelopes.