Skip to content

Components & catalog

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.component
class 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.

def render() -> Any

Return 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).

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}.
def signature() -> str

Return the compact TS-like prompt line, e.g. stat(label: string, value: string|number, ...) — One headline number.

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 Schema object dict (no "ui" key — the tool name
  • dict[str, Any] — identifies the component).
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).

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

Component names in prompt order.

Component classes in prompt order.

def get(name: str) -> Optional[type[Component]]

Return the component class for name (or None).

def only(*refs: Ref) -> 'Catalog'

Return a new catalog containing only the given components (names or classes).

def without(*refs: Ref) -> 'Catalog'

Return a new catalog without the given components.

def prompt(mode: Literal['fence', 'tools'] = 'fence') -> str

Return 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.
def token_estimate(mode: Literal['fence', 'tools'] = 'fence') -> int

Rough token count of prompt (characters / 4).

def json_schema(strict: bool = True) -> dict[str, dict[str, Any]]

Return {name: props JSON Schema} for every component.

def tool_name(name: str) -> str

Return the tool name for a component (render_<name>).

def component_for_tool(tool_name: str) -> Optional[str]

Map a render_<name> tool name back to its component name.

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.
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] — The UINode; an invalid node (status error) on failure.
def catalog(*groups: str) -> Catalog

Return 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()

def html_template(
path: Union[str, Path],
*,
root: Union[str, Path, None] = None,
height: Optional[int] = None,
**props: Any,
) -> Html

Render 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 — If path is unsafe, escapes root or doesn’t exist.

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.