Skip to content

Actions & human-in-the-loop

An action is what an interactive component sends back when the user clicks, submits or chooses. Read this page to decide where each one goes: a waiting chat.ask(), an @app.on_action handler, or the model.

The snippets below are fragments that assume an app; the complete example is a whole file.

Interactive components (buttons, choice, form, confirm, rating, card actions, and list rows with an action) send an action when the user responds. An action carries:

Field Meaning
name The action name declared by the component, e.g. "book_flight", "form_submit", "confirm"
data Static data declared next to the action (a button’s data)
values What the user entered or chose. See below.
label What the user clicked, e.g. "Book" or "Approve"
message_id, component_id Where it came from
Component Default action values
buttons, card actions, list rows the button’s / row’s own action {}
choice choice {"selected": [...], "other": "..."}
form form_submit {"<field name>": value, ...}
confirm confirm {"confirmed": true | false}
rating rating {"rating": n} (thumbs: 1 or -1)

quick_replies are different: tapping one sends its text as a normal user message.

When an action arrives, DyUI verifies it before anything runs:

  • The component must exist in that assistant message, in the user’s own conversation, and must have declared that action name. Forged actions get a 400.
  • action.data always comes from the server’s copy of the component, never from the browser, so a button’s data (prices, ids) can be trusted. The browser’s copy is only used to tell apart several buttons that share an action name; if it matches none of them, the request is rejected as forged.
  • Each interactive component can be answered once. A second answer, for example from another tab, gets a 409 (“This has already been answered”).

It then tries three routes, in order:

await chat.ask(component) renders an interactive component and pauses your handler until the user responds, then returns the Action. This is the human-in-the-loop primitive:

@app.on_message
async def reply(msg, chat):
answer = await chat.ask(ui.Confirm("Delete 14 conversations?", danger=True, confirm_label="Delete"))
if answer.values.get("confirmed"):
yield ui.Callout("Deleted.", level="success")
else:
yield "Kept everything."

Pass timeout= (seconds) to stop waiting; asyncio.TimeoutError is raised when it runs out. ask() also works with forms, choices, buttons and ratings:

answer = await chat.ask(ui.Form(
ui.Field("email", type="email", required=True),
ui.Field("plan", type="select", options=["Starter", "Team"]),
title="Create your account",
))
email = answer.values["email"]

2. @app.on_action(name) — handle it in Python

Section titled “2. @app.on_action(name) — handle it in Python”
@app.on_action("book_flight")
async def book(action: dyui.Action, chat: dyui.Chat):
yield f"Booking {action.data['airline']}…"

An action handler has the same shapes and yield rules as on_message. If it yields, its output becomes a new assistant reply.

With no waiting ask() and no handler, the action becomes a user turn. The user sees the label (for example “Book”), and on_message receives an LLM-friendly text with msg.action set:

[action:book_flight] {"data":{"airline":"TAP","depart":"Fri 18:05","price":189}}

An LLM bot therefore needs no action code at all: the model reads what was clicked and carries on.

After an interactive component is used, it becomes resolved: the client shows the choice inline (for example a check and “Approved”) and disables the control. The resolution is stored in the message as props.__resolved__, so it survives a reload. Only components in the latest assistant message are interactive.

All three routes in one app: a form answered through chat.ask(), a button with an @app.on_action handler that asks for confirmation, and a button with no handler that comes back to on_message. It needs no API key.

app.py
"""Every way an action reaches your code: chat.ask() waits for an answer,
@app.on_action handles a click, and anything unhandled goes back to on_message.
Save as app.py, then run: dyui run app.py
"""
import dyui
from dyui import ui
app = dyui.App(title="Account Desk", starters=["Create an account"])
@app.on_message
async def reply(msg: dyui.Message, chat: dyui.Chat):
if msg.action is not None: # route 3: an action with no handler arrives as a message
yield f"You clicked **{msg.action.label}** (`{msg.action.name}`). An LLM bot would see: `{msg.text}`"
return
# route 1: render a form and wait for the answer
answer = await chat.ask(ui.Form(
ui.Field("email", type="email", required=True),
ui.Field("plan", type="select", options=["Starter", "Team"]),
title="Create your account",
))
email, plan = answer.values["email"], answer.values["plan"]
yield f"Creating a **{plan}** account for `{email}`."
yield ui.Buttons(
ui.Button("Delete it again", action="delete_account", data={"email": email}, style="danger"),
ui.Button("Tell me more", action="more_info"), # no handler: becomes a message
)
@app.on_action("delete_account") # route 2: a Python handler for one action name
async def delete(action: dyui.Action, chat: dyui.Chat):
answer = await chat.ask(ui.Confirm(f"Delete {action.data['email']}?", danger=True, confirm_label="Delete"))
if answer.values.get("confirmed"):
yield ui.Callout("Deleted.", level="success")
else:
yield "Kept everything."
  1. Save it as app.py in a new, empty file, or download app.py.

  2. Run it:

    Terminal window
    pip install dyui
    dyui run app.py
  3. Open http://localhost:8000, send the starter, fill in the form, then try both buttons.

DyUI Docs assistant

Answers come from the DyUI docs. gpt-oss-120b via Groq. Press Escape or the close button to return to the page.