Skip to content

Generative UI

Generative UI is how a model renders components: DyUI describes them in the system prompt, and turns what the model writes back into validated components while it streams. Read this page when a model writes your replies.

Code on this page is fragments; the complete example is a whole file.

The model renders UI by writing data, never markup. DyUI tells the model which components exist, parses what it writes as it streams, and validates every component against the same pydantic model that produced its description.

The model writes ordinary Markdown. A component goes in a fenced block tagged dyui, with one JSON object per line:

Here are two options for this weekend.
```dyui
{"ui":"flight","airline":"TAP","depart":"Fri 18:05","price":189}
{"ui":"quick_replies","options":["Check hotels too","Book the cheaper one"]}
```
Want me to hold a seat?
  • "ui" names the component; the other keys are its props.
  • Layout components take "children": [ ...component objects... ].
  • A whole JSON array on one line also works. A block tagged json is accepted when its objects have a "ui" key.
  • The fence lines are never shown to the user.

app.catalog is the set of components this app’s model may use: the enabled built-ins plus your registered components. app.catalog.prompt() renders it. For an app with components=["table", "stat", "quick_replies"] and a custom Flight, the prompt is exactly:

## UI components
You can show UI components inside your reply. To do so, write a fenced code block
tagged `dyui` containing one JSON object per line: {"ui": "<name>", ...props}.
Components support your written answer; they do not replace it. Always write prose
outside the block as well: a sentence that introduces what you are showing, and a
short takeaway or explanation after it. Never reply with components alone. Use plain
text only when a component isn't clearly better.
Components:
table(columns: string[], rows: (string|number)[][], caption?: string) — Tabular data; use for 3+ rows.
stat(label: string, value: string|number, unit?: string, delta?: string, trend?: "up"|"down"|"flat", help?: string) — One headline number.
quick_replies(options: string[]) — Suggested follow-up replies the user can tap.
flight(airline: string, depart: string, price: number) — A bookable flight option. Use when listing specific flights.
Example:
<a sentence introducing what you are showing>
```dyui
{"ui":"stat","label":"Revenue","value":"$48.2k","delta":"+12%","trend":"up"}
```
<one or two sentences on what it means>

The prompt is rebuilt from the live registry on every access. Its order is deterministic (built-ins in catalog order, then your components in registration order), so provider prompt caching keeps hitting. The full built-in catalog is about 1,250 tokens. DyUI logs a warning when a catalog’s estimate passes 1,800.

dyui.llm.stream(..., components=catalog) appends the section to your system prompt. If your prompt contains a {dyui_components} placeholder, the section is placed there instead. With any other SDK, add app.catalog.prompt() to the system prompt yourself; see Bring your own SDK.

Terminal window
dyui catalog app.py # print the exact prompt and a token estimate
dyui catalog app.py --mode tools # the structured-mode variant
dyui catalog app.py --json # names + JSON schemas
app = dyui.App(components="all") # default: every built-in
app = dyui.App(components=["table", "chart", ui.Stat]) # names or classes
app = dyui.App(components=None) # only your own components
app.catalog.only("flight", "confirm", "quick_replies") # a subset, e.g. at checkout
app.catalog.without("chart")
dyui.catalog("data", "interactive") # built-ins by group
app.catalog.names, app.catalog.token_estimate()

The catalog only controls what the model is told about. Model output (fences, and {"ui": ...} dicts) is validated against every model-usable built-in plus your components, so a table still renders even if it isn’t in the catalog. The developer-only html and invalid are never accepted from model output. Component instances your code yields (ui.Html(...) included) always render.

The server-side FenceParser is an incremental state machine that handles chunk boundaries anywhere, even in the middle of a JSON string. For the line being written:

  1. Once "ui" is known, an empty component appears as a soft skeleton.
  2. As props arrive, a tolerant partial-JSON parser closes open strings, arrays and objects. The component updates in place, at most every ~60 ms of input.
  3. At the end of the line, the JSON is parsed and validated.

Validation is lax: values are coerced where it’s safe ("189" → 189), unknown props are dropped, and common aliases are accepted ("type": "dropdown" → select). An unknown component, invalid props or an unterminated block become an invalid component, a quiet, collapsible “Couldn’t display this component” chip. The text around it is never lost.

The invalid chip only covers what the model wrote. If your own code builds an invalid component, pydantic raises in your handler as usual.

For OpenAI and Anthropic you can opt into schema-guaranteed components:

async for part in dyui.llm.stream("openai/gpt-5", chat.history, components=app.catalog, ui_mode="tools"):
yield part

DyUI registers one strict tool per component, render_<name>, whose parameters are the component’s JSON schema. The provider’s constrained decoding then guarantees valid props. DyUI runs the tool loop itself, up to max_tool_rounds=8 requests: each completed call is rendered where it happened, a tool result is sent back that tells the model the component is on screen and asks it to carry on in text, and generation continues. Prose and components still interleave. The catalog prompt in this mode tells the model to call render_* tools instead of writing fences.

Use fence mode (the default) for every other provider and for local models.

chat.history gives the model back what it rendered. Each component in an assistant message is re-serialized as a one-line dyui block containing its props, so a follow-up like “book the cheaper one” has the data it refers to. Action messages appear as [action:name] {...}.

A model that answers with a chosen set of built-ins and a custom Ticker component, placed in the system prompt with {dyui_components}.

app.py
"""A model that answers with a chosen set of components, placed in the prompt with
{dyui_components}, plus a custom component of your own.
Needs: pip install "dyui[openai]" and NVIDIA_API_KEY in a .env file next to this one.
Save as app.py, then run: dyui run app.py (dyui catalog app.py prints the prompt)
"""
import os
import dyui
from dyui import ui
MODEL = os.getenv("DYUI_MODEL", "nvidia/moonshotai/kimi-k2.6")
UI_MODE = os.getenv("DYUI_UI_MODE", "fence") # "tools" = schema-guaranteed (OpenAI, Anthropic)
app = dyui.App(
title="Market Brief",
components=["stat", "stats", "table", "chart", "callout", "quick_replies"],
starters=["How did the big tech stocks do this quarter?"],
)
@app.component
class Ticker(dyui.Component):
"""One stock with its price and daily move. Use when the user asks about a single stock."""
symbol: str
price: float
change_pct: float
examples = [{"symbol": "ACME", "price": 182.4, "change_pct": 1.8}]
def render(self):
trend = "up" if self.change_pct >= 0 else "down"
return ui.Stat(self.symbol, f"${self.price:,.2f}", delta=f"{self.change_pct:+.1f}%", trend=trend)
SYSTEM = """You are a markets analyst. Use made-up but plausible numbers and say so.
{dyui_components}
Prefer a `ticker` for one stock, `stats` for a few headline numbers and a `chart` for trends."""
@app.on_message
async def reply(msg: dyui.Message, chat: dyui.Chat):
async for part in dyui.llm.stream(MODEL, chat.history, system=SYSTEM, components=app.catalog, ui_mode=UI_MODE):
yield part
  1. Save it as app.py in a new, empty file, or download app.py.

  2. Put your key in a .env file next to it: NVIDIA_API_KEY=nvapi-... (free at build.nvidia.com; other providers on API keys & environment).

  3. Run it:

    Terminal window
    pip install "dyui[openai]"
    dyui run app.py
  4. Open http://localhost:8000 and send the starter. Run dyui catalog app.py to see the exact prompt section the model receives, and DYUI_UI_MODE=tools (in .env) to try structured mode with an OpenAI or Anthropic model.

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.