Skip to content

Add streaming callbacks: @callback(..., stream=True)#3888

Open
T4rk1n wants to merge 2 commits into
devfrom
feat/stream-callbacks
Open

Add streaming callbacks: @callback(..., stream=True)#3888
T4rk1n wants to merge 2 commits into
devfrom
feat/stream-callbacks

Conversation

@T4rk1n

@T4rk1n T4rk1n commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

A callback registered with stream=True is a generator (or async generator) whose yields are pushed to the browser as they are produced. Each yielded value has the same shape as a regular return value and replaces the outputs; yielding dash.Patch gives incremental updates (e.g. LLM token streaming). The last yield is the final value.

Transport follows the callback's normal selection: over the WebSocket callback transport, frames ride the open connection as callback_response messages with stream: true; otherwise the HTTP response streams NDJSON (application/x-ndjson), one frame per line with a terminal {"done": true} frame. Works on Flask, Quart, and FastAPI; sync generators warn at registration since they occupy a server worker for the whole stream.

  • dash/_streaming.py: StreamedCallbackResponse marker, context-safe iteration helpers (callback context travels in a contextvars snapshot so set_props/ctx work after dispatch returns), NDJSON serialization, and a thread bridge for async generators on Flask.
  • dash/_callback.py: stream wrappers building one frame per yield via _prepare_response; per-yield no_update/PreventUpdate handling, on_error support, error frames; registration validation (mutually exclusive with background/clientside/mcp_enabled/api_endpoint).
  • backends: streaming response branches in each serve_callback; ws.py frame emitter and stream consumers for both the event-loop and threadpool dispatch paths.
  • renderer: applyStreamFrame applies frames on arrival through the sideUpdate path (Patch applies exactly once); NDJSON reader in handleServerside; stream-aware callback_response handling keeps the request pending until the terminal frame; loading states span the whole stream.

Demo

Two examples:

  1. Fake LLM token streaming — each token is yielded as a dash.Patch that
    appends to the output while the callback keeps running.
  2. A long computation with a live progress bar — each yield replaces the
    bar width/label, and set_props pushes a side update along the way.
import asyncio
import random

from dash import Dash, Input, Output, Patch, callback, html, set_props

LOREM = (
    "Streaming callbacks let a generator push every yield straight to the "
    "browser while the callback is still running which is exactly what you "
    "want for token streaming progress feeds and long computations"
).split()

app = Dash(__name__)

app.layout = html.Div(
    style={"maxWidth": "640px", "margin": "40px auto", "fontFamily": "sans-serif"},
    children=[
        html.H2("stream=True demo"),
        html.H4("1. Token streaming (Patch appends)"),
        html.Button("Generate", id="generate", n_clicks=0),
        html.Div(
            id="answer",
            children="Click Generate…",
            style={
                "whiteSpace": "pre-wrap",
                "border": "1px solid #ccc",
                "borderRadius": "6px",
                "padding": "12px",
                "margin": "12px 0 32px",
                "minHeight": "60px",
            },
        ),
        html.H4("2. Long computation with progress"),
        html.Button("Run job", id="run-job", n_clicks=0),
        html.Div(
            style={
                "background": "#eee",
                "borderRadius": "6px",
                "margin": "12px 0",
                "height": "22px",
                "overflow": "hidden",
            },
            children=html.Div(
                id="bar",
                style={
                    "background": "#119dff",
                    "height": "100%",
                    "width": "0%",
                    "transition": "width 0.2s",
                },
            ),
        ),
        html.Div(id="status", children=""),
    ],
)


@callback(
    Output("answer", "children"),
    Input("generate", "n_clicks"),
    stream=True,
    prevent_initial_call=True,
)
async def generate(n):
    yield ""  # clear the output immediately
    for word in LOREM:
        await asyncio.sleep(random.uniform(0.03, 0.15))  # pretend to think
        patch = Patch()
        patch += word + " "
        yield patch  # append one token


@callback(
    Output("bar", "style"),
    Output("status", "children"),
    Input("run-job", "n_clicks"),
    stream=True,
    prevent_initial_call=True,
)
async def run_job(n):
    style = Patch()
    for i in range(1, 101):
        await asyncio.sleep(0.04)
        style["width"] = f"{i}%"
        yield style, f"Processing step {i}/100…"
        if i == 50:
            # set_props between yields rides along with the next frame
            set_props("answer", {"children": "Halfway there — set_props says hi."})
    yield style, "Done ✅"


if __name__ == "__main__":
    app.run(debug=True)

A callback registered with stream=True is a generator (or async
generator) whose yields are pushed to the browser as they are produced.
Each yielded value has the same shape as a regular return value and
replaces the outputs; yielding dash.Patch gives incremental updates
(e.g. LLM token streaming). The last yield is the final value.

Transport follows the callback's normal selection: over the WebSocket
callback transport, frames ride the open connection as callback_response
messages with stream: true; otherwise the HTTP response streams NDJSON
(application/x-ndjson), one frame per line with a terminal {"done": true}
frame. Works on Flask, Quart, and FastAPI; sync generators warn at
registration since they occupy a server worker for the whole stream.

- dash/_streaming.py: StreamedCallbackResponse marker, context-safe
  iteration helpers (callback context travels in a contextvars snapshot
  so set_props/ctx work after dispatch returns), NDJSON serialization,
  and a thread bridge for async generators on Flask.
- dash/_callback.py: stream wrappers building one frame per yield via
  _prepare_response; per-yield no_update/PreventUpdate handling,
  on_error support, error frames; registration validation (mutually
  exclusive with background/clientside/mcp_enabled/api_endpoint).
- backends: streaming response branches in each serve_callback;
  ws.py frame emitter and stream consumers for both the event-loop and
  threadpool dispatch paths.
- renderer: applyStreamFrame applies frames on arrival through the
  sideUpdate path (Patch applies exactly once); NDJSON reader in
  handleServerside; stream-aware callback_response handling keeps the
  request pending until the terminal frame; loading states span the
  whole stream.
fastapi.testclient imports starlette.testclient, which requires httpx.
Skip the protocol-level stream tests when httpx is not installed and add
httpx to the CI requirements so the websocket job actually runs them.
@sonarqubecloud

sonarqubecloud Bot commented Jul 9, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant