Dynamic Schema A2UI
LLM-generated A2UI. A secondary LLM creates both the schema and data from any prompt.
Not available for agent-spec yet
declarative-gen-ui demo, and agent-spec doesn't have it yet. See this page for CopilotKit's Built-in Agent, LangGraph (Python), LangGraph (TypeScript).Use a dynamic schema when you cannot know the UI shape ahead of time, for example a dashboard whose layout changes per question. Your agent composes the surface (schema, data, and layout) from your components on each turn.
How it works
- The agent calls the A2UI tool to draw a surface, made available
when
injectA2UITool: true. - The runtime serializes your client-side catalog (component names +
Zod prop schemas) into the agent's
copilotkit.contextso the LLM knows which components it may emit. - The tool call streams from the agent as
TOOL_CALL_ARGSevents. - The A2UI middleware intercepts the stream and renders cards progressively as data items arrive.
The 3-file split
The canonical Bring-Your-Own-Catalog (BYOC) layout keeps three files
side-by-side under frontend/src/app/a2ui/:
| File | What lives there |
|---|---|
definitions.ts | Zod props schema + human-readable descriptions for each custom component. Platform-agnostic, so the runtime can serialise it to the LLM. |
renderers.tsx | React implementations keyed by the same names. TypeScript enforces that every definition has a renderer. |
catalog.ts | createCatalog(definitions, renderers, { includeBasicCatalog: true }): merges your custom components with CopilotKit's built-in primitives. |
Declare your custom component definitions
Each entry pairs a Zod prop schema with a description. The description
matters, because the LLM reads it to decide which component to emit. The
example below is a dashboard catalog with cards (Card, StatusBadge,
Metric, InfoRow, DataTable, PrimaryButton) and charts
(PieChart, BarChart):
Implement the React renderers
Every key in myDefinitions must have a matching renderer. Props are
statically typed against the Zod schema, so refactors stay safe.
The demo renderers use the demo's own Badge and Button components,
its theme tokens, and recharts for the charts. Use your own
components in their place:
Wire definitions and renderers into a catalog
createCatalog is what you hand to the provider. Flip
includeBasicCatalog: true to merge CopilotKit's built-ins
(Column, Row, Text, Image, Card, Button, List, Tabs, …), so the LLM
can compose custom + basic components interchangeably:
Pass the catalog to the provider
A single prop (a2ui={{ catalog }}) is all the frontend needs; the
provider registers the catalog and wires up the built-in A2UI
activity-message renderer:
In your app, use your own runtime URL and your own chat in place of the
demo's Chat component.
That is all the default path needs. The catalog auto-enables A2UI and
injects the generate_a2ui tool, so the runtime needs no a2ui block,
unless your agent brings its own A2UI tool (see Agent setup).
(No catalog? Turn it on from the runtime instead with
a2ui: { injectA2UITool: true }.)
Ask for a dashboard
Ask the agent for a dashboard. For example, send Show me my sales dashboard for this quarter. The dashboard renders in the chat with the
components from your catalog.
Agent setup
Tell the model in its instructions when to call generate_a2ui, for example
for any answer that needs a dashboard, a card layout, or a chart. A model with
no instruction about the tool answers in prose and never calls it.
I opted out of auto-inject, now what?
When the runtime does not inject the generate_a2ui tool, what you add
depends on your setup:
- The runtime has an
a2uiblock, or the provider passes a catalog. The runtime still attaches theA2UIMiddleware. Add only the A2UI agent tool below. - No catalog and no
a2uiblock. A2UI is off. Add both theA2UIMiddlewareand the A2UI agent tool below.
The A2UIMiddleware
The A2UIMiddleware is what turns the agent's a2ui_operations into
rendered surfaces. Without it, the agent's output never becomes UI; it
falls through as a plain tool result. The middleware can also add a
render_a2ui tool to the agent. To do this, set injectA2UITool: true
and skip the next step. Attach it to the AG-UI agent:
import { A2UIMiddleware } from "@ag-ui/a2ui-middleware";
agent.use(new A2UIMiddleware({ injectA2UITool: false }));The A2UI agent tool
The generate_a2ui tool runs a secondary LLM (a subagent) that designs
the surface, which is why you hand it a model. With LangGraph Python,
build it with the AG-UI factory and add it to your agent's tools:
from ag_ui_langgraph import get_a2ui_tools
from langchain_openai import ChatOpenAI
generate_a2ui = get_a2ui_tools({
"model": ChatOpenAI(model="gpt-6-luna", reasoning_effort="none"),
"default_catalog_id": "declarative-gen-ui-catalog", # the catalogId you pass to createCatalog
})
tools = [my_other_tool, generate_a2ui]With other agents, define a generate_a2ui tool that calls a second
model and returns an a2ui_operations container. The
Built-in Agent example
shows this in TypeScript.
Progressive streaming
The secondary LLM's render_a2ui tool call streams from the agent
as TOOL_CALL_ARGS events. The A2UI middleware:
- Waits for the full
componentsarray before emitting anything; the schema must be complete before rendering starts. - Extracts
surfaceId+rootfrom the partial JSON. - Emits
createSurface+updateComponentsonce the schema is complete. - Extracts complete
itemsobjects progressively and emits anupdateDataModelfor each, so cards appear one by one as data streams in.
A built-in progress indicator shows while the schema is still generating and hides automatically once data items start arriving.
When should I use dynamic schemas?
- You don't know the UI shape ahead of time; the agent decides what to show based on the user's request.
- You want to prototype A2UI without committing to a schema file yet.
- You're building a conversational dashboard where the layout varies per turn.
If the surface is well-known (e.g. a product card, a flight result), prefer a fixed schema; it's faster, cheaper, and the UI is deterministic.
How is this guide?