Connect your backend
Give your model your components as tools, and stream an answer your app can draw. Your model, your loop, your keys.
Set up useval (generative UI) in this app.
useval lets this app's AI assistant answer with interactive components that our team designs. The model never writes UI: it picks a published component and fills in its values with a tool call; the frontend draws it natively.
Work in this order and keep the app's existing behaviour intact:
1. Explore first. Find (a) the backend code that calls the LLM — provider (OpenAI, Anthropic, Vercel AI SDK or other), the chat route and the tool-calling loop — and (b) the frontend that renders assistant replies — React or Flutter. Tell me what you found and your plan before editing anything.
2. Install the SDKs. They ship as archives in the useval release download; ask me for the extracted sdk/ directory if it isn't in the repo.Install
Section titled “Install”npm install ./vendor/val-genui/val-genui-protocol-2.0.0.tgz ./vendor/val-genui/val-genui-server-1.0.0.tgzFour places it plugs in
Section titled “Four places it plugs in”-
Create a client and a reply — once per request.
app/api/chat.ts import { GenUi } from '@val-genui/server'const genui = new GenUi({ apiKey: process.env.GEN_UI_SECRET_KEY! })const ui = genui.reply({ client: request.headers.get('gen-ui-client') })The
gen-ui-clientheader comes from your app’s SDK. It tells the server what this app can draw, so an older app never receives a component it can’t.baseUrldefaults tohttp://localhost:8080; when you deploy, pass your Val service’s address asnew GenUi({ apiKey, baseUrl }). -
Add your components as tools, beside your own, in your provider’s shape.
const tools = [...myTools, ...(await ui.tools('openai'))] -
Append the guidance to your system prompt.
const system = `${mySystem}\n\n${await ui.guidance()}` -
Feed the model’s output through, then return the stream.
ui.text(delta) // prose, as it streamsif (genui.owns(call.name)) {const { forModel } = await ui.call(call) // drawn into its placemessages.push({ role: 'tool', tool_call_id: call.id, content: forModel })}void ui.end()return ui.response()
With your SDK
Section titled “With your SDK”import { openai } from '@val-genui/server/openai'
// Chat Completionsconst { messages, rest } = await openai.chatToolCalls(ui, message.tool_calls)// Responses APIconst { outputs } = await openai.responsesCalls(ui, response.output)messages are Val’s tool results; rest are your own calls, untouched.
import { anthropic } from '@val-genui/server/anthropic'
const tools = [...myTools, ...(await ui.tools('anthropic'))]const { results, rest } = await anthropic.toolUses(ui, message.content)import { jsonSchema, streamText } from 'ai'import { aiSdk } from '@val-genui/server/ai-sdk'
streamText({ model, tools: { ...myTools, ...(await aiSdk.tools(genui, ui, { jsonSchema })) }, onChunk: ({ chunk }) => chunk.type === 'text-delta' && ui.text(chunk.text), onFinish: () => ui.end(),})What the model gets back
Section titled “What the model gets back”Not the component. forModel is a short note — {"shown": "order-summary", "version": 3, …} — or {"error": "<the reason>"} when a call is refused, which the model fixes on its next turn. The markup never enters your model’s context.
ui.call never throws: a refusal removes the placeholder and returns ok: false.
Sending the answer
Section titled “Sending the answer”ui.response() |
Server-sent events as a standard Response — Next.js, Hono, Bun, Deno, Workers. { format: 'ndjson' } for NDJSON. |
ui.pipe(res) |
Node http, Express, Fastify (reply.raw). |
ui.events() |
The events themselves, for a stream you already have. |
await ui.end() |
Waits for every component, closes the stream, returns the finished reply — the thing to store. |
When someone presses a button
Section titled “When someone presses a button”messages.push({ role: 'user', content: genui.actionMessage(action) })How often to show components
Section titled “How often to show components”const genui = new GenUi({ apiKey, genUiLevel: 'medium' })low uses components selectively, medium (default) whenever one clearly fits, high throughout substantive answers. Every level respects text-only requests.