How DyUI works
Read this page first. It follows one message end to end and links to the page that covers each part in depth.
DyUI has three parts, and all of them ship in the dyui package:
- Your Python app: an
Appwith handlers (@app.on_message,@app.on_action…) and, optionally, your own components. - The DyUI server: a FastAPI app that runs your handlers, parses what they produce, stores conversations and streams events to the browser over Server-Sent Events.
- The chat app: a React app, compiled into the package. It renders the stream with DyUI’s component library.
browser ──POST /api/chat──▶ on_message(msg, chat) │ yields text, components, steps, reasoning │ (or streams a model: dyui.llm.stream(..., components=app.catalog)) ▼ fence parser ──▶ validated UI nodes ──SSE──▶ chat app renders them │ your code ◀── chat.ask() / @app.on_action / back to on_message ◀── POST /api/action (clicks, forms)1. The message arrives
Section titled “1. The message arrives”The user sends a message. The browser calls POST /api/chat. The server stores the message, opens an SSE stream and calls your on_message(msg, chat) handler.
msg.textis what the user typed, andmsg.filesthe files they attached.chat.historyis the conversation so far as{"role", "content"}dicts, ready for any chat API.chat.sessionis a dict saved with the conversation.
Details: Messages & streaming, Sessions & persistence.
2. Your handler yields
Section titled “2. Your handler yields”Everything the handler yields is appended to the reply, in order:
- Text streams in as Markdown.
- Components (
ui.Stat(...),ui.Table(...), or your own) render where they’re yielded. chat.step(...)shows a live step: running, then done or error, with its duration.dyui.Reasoning(...)fills a collapsible “thinking” block.
Details: Steps & reasoning, Components.
3. Or a model writes the reply
Section titled “3. Or a model writes the reply”To let a model answer, the handler streams it and yields what comes back:
# Inside your handler:async for part in dyui.llm.stream(MODEL, chat.history, components=app.catalog): yield partapp.catalog lists the components the model may use. components=app.catalog adds a short “UI components” section to the system prompt, with one signature line per component, generated from the pydantic models. Because the prompt is rebuilt from the live registry, registering a component or editing its docstring changes what the model knows on the next turn.
The model then writes components as JSON lines in a ```dyui fence inside its text. Any SDK works: DyUI only needs the text.
Details: Generative UI, Models & providers, API keys.
4. The parser turns text into components
Section titled “4. The parser turns text into components”Text goes through a streaming fence parser. Prose streams straight through. A dyui block becomes components while it’s still being written: a component appears as soon as its "ui" name is known, and fills in as its props arrive.
Every node is validated against its pydantic model. Invalid output turns into a quiet “Couldn’t display this component” chip, and the surrounding prose is kept. Custom components are expanded on the server through their render(), so the browser only ever renders built-ins.
When the handler finishes, the message is stored with its parts. After the first exchange, the thread gets a title from the user’s first words.
Details: Custom components.
5. The user clicks
Section titled “5. The user clicks”Interactive components (buttons, forms, choices, confirms, ratings) send an action to POST /api/action. The server first checks that the component exists in that message and declared that action, and rejects forged actions. It then routes the action in this order:
- to a
chat.ask(...)that is waiting for this component; - otherwise to an
@app.on_action(name)handler; - otherwise back to
on_message, as an LLM-friendly message the model can respond to.
An LLM bot therefore needs no action code at all; add handlers only where Python should decide.
Details: Actions & human-in-the-loop.
Safety model
Section titled “Safety model”- The model only ever sends data. HTML from the model is never rendered, and Markdown is rendered without raw HTML.
- URLs must be
http(s):,mailto:or app-relative/api/files/...; anything else is dropped. External links open in a new tab withrel="noopener noreferrer". - Actions are verified against the component that declared them.
action.datacomes from the server’s copy, and each interactive component can be answered once (409after that). - Request bodies are size-limited, failed password attempts are throttled, and uploaded files that aren’t safe to display inline are served as downloads under a sandboxing Content-Security-Policy.
- Developer HTML (
ui.Html,dyui.html_template) renders in a sandboxed iframe without same-origin access, and the model can’t produce it. - API keys stay on the server. Errors in the chat never include tracebacks; the traceback goes to the server log.
Complete example
Section titled “Complete example”One turn with every step above and no model, so it needs no key: text, a live step, a component, a button handled in Python, and an approval that pauses the handler.
"""One DyUI turn, end to end, with no model: text, a component, a live step,a button that comes back to Python, and an approval that pauses the handler.
Save as app.py, then run: dyui run app.py"""import asyncio
import dyuifrom dyui import ui
app = dyui.App(title="Order Desk", starters=["Where is order 1042?"])
@app.on_messageasync def reply(msg: dyui.Message, chat: dyui.Chat): # 1. text streams into the reply as Markdown yield f"Looking up **{msg.text}**…" # 2. a live step shows work in progress, then done with its duration async with chat.step("Fetching the order", type="tool") as step: await asyncio.sleep(0.3) # call your API here step.output = {"order": 1042, "status": "shipped"} # 3. components render inline, where they're yielded yield ui.KeyValue({"Order": "1042", "Status": "Shipped", "Carrier": "DHL"}) # 4. a button sends an action back to Python yield ui.Buttons(ui.Button("Cancel order", action="cancel_order", data={"order": 1042}, style="danger"))
@app.on_action("cancel_order")async def cancel(action: dyui.Action, chat: dyui.Chat): # 5. chat.ask() renders a component and waits for the user's answer answer = await chat.ask(ui.Confirm(f"Cancel order {action.data['order']}?", danger=True, confirm_label="Cancel it")) if answer.values.get("confirmed"): yield ui.Callout("Cancelled. The refund is on its way.", level="success") else: yield "Kept the order as it is."-
Save it as
app.pyin a new, empty file, or download app.py. -
Run it:
Terminal window pip install dyuidyui run app.py -
Open http://localhost:8000, send the starter, then click Cancel order.
See also
Section titled “See also”- Your first LLM bot: the same loop with a model in charge.
- Protocol: the HTTP and SSE events, for your own frontend.