Add streaming callbacks: @callback(..., stream=True)#3888
Open
T4rk1n wants to merge 2 commits into
Open
Conversation
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.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



A callback registered with
stream=Trueis 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; yieldingdash.Patchgives 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 soset_props/ctxwork 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-yieldno_update/PreventUpdatehandling, 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.pyframe 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:
appends to the output while the callback keeps running.
bar width/label, and set_props pushes a side update along the way.