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_typesStreamTransformer
    Class●Since v1.1

    StreamTransformer

    Copy
    StreamTransformer(
        self,
        scope: tuple[str, ...] = (),
    )

    Bases

    ABC

    Constructors

    Attributes

    Methods

    View source on GitHub

    Parameters

    NameTypeDescription
    scopetuple[str, ...]
    Default:()

    The namespace tuple the owning mux is scoped to. () for the root. Factories receive this at construction time (factory(scope) in StreamMux).

    constructor
    __init__
    NameType
    scopetuple[str, ...]
    attribute
    requires_async: bool
    attribute
    supports_sync: bool
    attribute
    required_stream_modes: tuple[str, ...]
    attribute
    scope: tuple[str, ...]
    method
    init

    Return the projection dict.

    Keys become entries in run.extensions. If the transformer has _native = True, keys are also set as direct attributes on the run stream.

    StreamChannel instances in the return value are automatically wired by the StreamMux for protocol event auto-forwarding.

    method
    process

    Handle an event on the sync lane.

    Called for every event before it is appended to the main event log. Subclasses must override either process or aprocess. The default raises so a missing override fails loudly rather than silently passing every event through.

    method
    aprocess

    Handle an event on the async lane.

    The mux awaits this before dispatching to the next transformer, so a slow aprocess serializes the pipeline. Use it only when a later transformer — or a consumer reading the event synchronously — must see the result of the async work (e.g. PII redaction that mutates event in place).

    The default delegates to process, so purely-sync transformers run unchanged under astream().

    method
    finalize

    Called when the run ends normally (sync lane).

    Override to close StreamChannels, resolve promises, or perform other teardown. StreamChannel instances in the projection dict are auto-closed by the mux.

    method
    afinalize

    Called when the run ends normally (async lane).

    By the time this runs, the mux has already awaited every task started via schedule(), so StreamChannels can be closed here without a last-task-wins race.

    The default delegates to finalize.

    method
    fail

    Called when the run ends with an error (sync lane).

    Override to fail StreamChannels, reject promises, or perform other teardown. StreamChannel instances in the projection dict are auto-failed by the mux.

    method
    afail

    Called when the run ends with an error (async lane).

    The mux cancels and awaits every task started via schedule() before calling this, so cleanup doesn't race with in-flight work.

    The default delegates to fail.

    method
    schedule

    Schedule a coroutine tied to this transformer's lifecycle.

    The mux holds the task reference, awaits all scheduled tasks during aclose() before calling afinalize(), and cancels them on afail(). Authors don't need to track tasks or implement the last-task-closes-the-log dance.

    Requires a running event loop — call only under astream(). Set requires_async = True on the class so registration under sync stream() fails fast with a clear message.

    Extension point for custom stream projections.

    Transformers observe protocol events flowing through the StreamMux and build typed derived projections (StreamChannels, promises, etc.).

    Set _native = True on a transformer to have its projection keys exposed as direct attributes on the run stream (in addition to appearing in run.extensions).

    Subclasses must implement init and override at least one of process / aprocess. The finalize / afinalize and fail / afail hooks are optional — the default implementations are no-ops. StreamChannel instances in the projection dict are auto-closed / auto-failed by the mux, so most transformers don't need finalize or fail at all.

    Transformers that need async work pick the async lane by:

    1. Overriding aprocess (and optionally afinalize / afail), or
    2. Calling self.schedule(coro) from inside a sync process, or
    3. Setting requires_async = True explicitly.

    The mux detects these cases at registration and raises if they're used under sync stream() — they only work under astream().

    Use aprocess when the pump must wait for async work before the next transformer sees the event (e.g. PII redaction that mutates event in place). Use schedule() for decoupled async work whose result lands on an independent projection (e.g. async moderation scoring, cost lookup, external tracing).