Components & catalog
dyui.Component
Section titled “dyui.Component”class Component(*args: Any, **data: Any)Base class for every DyUI component.
Subclass it (and register with @app.component) to define a custom
component. The class docstring becomes the prompt description, the fields
become props, and render returns a tree of built-in components::
@app.componentclass Flight(dyui.Component): """A bookable flight option. Use when listing specific flights.""" airline: str depart: str price: float
examples = [{"airline": "TAP", "depart": "Fri 18:05", "price": 189}]
def render(self): return ui.Card(self.airline, ui.Stat("Price", f"${self.price:.0f}"))Class-level options (plain class attributes): name (LLM name; default
snake_case of the class name), description (default: first docstring
paragraph), examples, group, llm (False hides it from the LLM),
positional (prop names accepted positionally).
Pass id="..." to the constructor to give the rendered node a stable id;
yielding another component with the same id replaces it in place.
Component.render()
Section titled “Component.render()”def render() -> AnyReturn the built-in component tree for a custom component.
Built-ins don’t override this. Custom components must, returning a component (or a list of components, wrapped in a card, or a string, rendered as markdown).
Component.to_node()
Section titled “Component.to_node()”def to_node(id: str | None = None) -> dict[str, Any]Serialize to a UINode dict (SPEC §4).
Custom components are expanded via render; the outer node keeps
the custom name in props.__component__. Child ids derive from the
parent id (<id>-0, <id>-tabs-1-0…), so they are stable.
Parameters
| Name | Type | Description |
|---|---|---|
id |
`str | None` |
Returns
dict[str, Any]— The node dict{id, ui, props, children?, status}.
Component.signature()
Section titled “Component.signature()”def signature() -> strReturn the compact TS-like prompt line, e.g.
stat(label: string, value: string|number, ...) — One headline number.
Component.json_schema()
Section titled “Component.json_schema()”def json_schema(strict: bool = True) -> dict[str, Any]Return the JSON Schema of this component’s props (SPEC §5.0).
Parameters
| Name | Type | Description |
|---|---|---|
strict |
bool |
Produce a provider strict-mode compatible schema (additionalProperties: false everywhere, every property required, optionals nullable, free-form JSON and child components encoded as JSON strings, no default). |
Returns
dict[str, Any]— A JSON Schemaobjectdict (no"ui"key — the tool namedict[str, Any]— identifies the component).
Component.declared_actions()
Section titled “Component.declared_actions()”def declared_actions( props: Mapping[str, Any],) -> list[tuple[str, Optional[dict[str, Any]]]]Actions a rendered node of this class may send, as (name, data).
Used by the server to reject forged actions. data is the static data
declared alongside the action (None when not applicable).
dyui.Catalog
Section titled “dyui.Catalog”class Catalog(components: Iterable[type[Component]] = ())An ordered set of component classes offered to the LLM.
Built-ins come first in spec order, then custom components in registration order, so the generated prompt is deterministic (prompt caching friendly).
Example >>> import dyui >>> cat = dyui.catalog(“data”, “interactive”) >>> “stat” in cat.names True
Catalog.names list[str]
Section titled “Catalog.names list[str]”Component names in prompt order.
Catalog.components list[type[Component]]
Section titled “Catalog.components list[type[Component]]”Component classes in prompt order.
Catalog.get()
Section titled “Catalog.get()”def get(name: str) -> Optional[type[Component]]Return the component class for name (or None).
Catalog.only()
Section titled “Catalog.only()”def only(*refs: Ref) -> 'Catalog'Return a new catalog containing only the given components (names or classes).
Catalog.without()
Section titled “Catalog.without()”def without(*refs: Ref) -> 'Catalog'Return a new catalog without the given components.
Catalog.prompt()
Section titled “Catalog.prompt()”def prompt(mode: Literal['fence', 'tools'] = 'fence') -> strReturn the “UI components” section to add to a system prompt.
Parameters
| Name | Type | Description |
|---|---|---|
mode |
Literal['fence', 'tools'] |
"fence" (default) teaches the fenced dyui block format; `"tools"` tells the model to call `render_* tools instead. |
Catalog.token_estimate()
Section titled “Catalog.token_estimate()”def token_estimate(mode: Literal['fence', 'tools'] = 'fence') -> intRough token count of prompt (characters / 4).
Catalog.json_schema()
Section titled “Catalog.json_schema()”def json_schema(strict: bool = True) -> dict[str, dict[str, Any]]Return {name: props JSON Schema} for every component.
Catalog.tool_name()
Section titled “Catalog.tool_name()”def tool_name(name: str) -> strReturn the tool name for a component (render_<name>).
Catalog.component_for_tool()
Section titled “Catalog.component_for_tool()”def component_for_tool(tool_name: str) -> Optional[str]Map a render_<name> tool name back to its component name.
Catalog.tool_schemas()
Section titled “Catalog.tool_schemas()”def tool_schemas( provider: Literal['openai', 'anthropic'] = 'openai',) -> list[dict[str, Any]]Return ready-to-send tool definitions, one render_<name> per component.
Parameters
| Name | Type | Description |
|---|---|---|
provider |
Literal['openai', 'anthropic'] |
"openai" → {"type": "function", "function": {..., "strict": true}}; "anthropic" → {"name", "description", "input_schema", "strict": true}. |
Raises
ValueError— For an unknown provider.
Catalog.validate()
Section titled “Catalog.validate()”def validate(obj: Any, *, id: Optional[str] = None) -> dict[str, Any]Validate an LLM component object (or tool-call args with "ui").
Explicit null for optional props counts as “not provided”.
Parameters
| Name | Type | Description |
|---|---|---|
obj |
Any |
{"ui": name, ...props}. |
id |
Optional[str] |
Node id (default: fresh c_<8hex>). |
Returns
dict[str, Any]— TheUINode; aninvalidnode (statuserror) on failure.
dyui.catalog()
Section titled “dyui.catalog()”def catalog(*groups: str) -> CatalogReturn a catalog of built-in components, optionally limited to groups.
Parameters
| Name | Type | Description |
|---|---|---|
*groups |
str |
Any of content, data, media, interactive, layout, agent. None → every LLM built-in. |
Example
dyui.catalog("data", "interactive").prompt()
dyui.html_template()
Section titled “dyui.html_template()”def html_template( path: Union[str, Path], *, root: Union[str, Path, None] = None, height: Optional[int] = None, **props: Any,) -> HtmlRender an HTML template file into a sandboxed ui.Html component.
Parameters
| Name | Type | Description |
|---|---|---|
path |
Union[str, Path] |
Template path (see load_template for where it is looked up). |
root |
Union[str, Path, None] |
Directory templates must live in (default: your app’s directory, then the current directory). |
height |
Optional[int] |
Optional fixed iframe height in px (auto-sized otherwise). |
**props |
Any |
Values for the placeholders. |
Raises
ValueError— Ifpathis unsafe, escapesrootor doesn’t exist.