Skip to content

The Open Standard for Generative UI

3.8 KB
Raw Blame
---
title: "Vercel AI SDK"
---

<Example
  prompt="What's the current Apple stock price?"
  image="/integrations/examples/vercel-ai-sdk"
  width={1600}
  height={600}
  alt="The Vercel AI SDK example answering a question about Apple's stock price. OpenUI renders the get_stock_price tool result as cards for the price, day change, day range and volume."
  href="https://github.com/thesysdev/openui/tree/main/examples/agent-frameworks/vercel-ai-sdk"
>

The `vercel-ai-sdk` example is a Next.js app whose chat route calls `streamText()` with weather, stock-price and web-search tools that return sample data. The browser reads the AI SDK's UIMessage stream through `vercelAIAdapter()`, and OpenUI renders the answer as components.

```bash title="Terminal"
cd examples/agent-frameworks/vercel-ai-sdk
pnpm install --ignore-workspace
pnpm generate:apiKey   # writes THESYS_API_KEY to .env.local
pnpm dev
```

</Example>

<Steps>

<Step title="Stream with streamText" source="https://github.com/thesysdev/openui/blob/main/examples/agent-frameworks/vercel-ai-sdk/src/app/api/chat/route.ts">

Call `streamText` with the generated OpenUI system prompt and your AI SDK tools, and return `toUIMessageStreamResponse()` from the route. `stopWhen: stepCountIs(5)` lets the model call tools and keep going for up to five steps.

```ts title="src/app/api/chat/route.ts"
import { createOpenAI } from "@ai-sdk/openai";
import { convertToModelMessages, stepCountIs, streamText } from "ai";
// Builds the OpenUI system prompt from your component library.
import { cloudInstructions } from "@/lib/cloud-prompt";
// Your AI SDK tools, defined with tool() from "ai".
import { tools } from "@/lib/tools";

// OpenUI Gateway is OpenAI-compatible, so the AI SDK's OpenAI provider can call it.
const gateway = createOpenAI({
  baseURL: "https://api.thesys.dev/v1/embed",
  apiKey: process.env.THESYS_API_KEY,
});

export async function POST(req: Request) {
  const { messages } = await req.json(); // UIMessage[] from vercelAIMessageFormat
  const result = streamText({
    model: gateway.chat("google/gemini-3.6-flash-free"),
    system: cloudInstructions(),
    messages: await convertToModelMessages(messages),
    tools,
    stopWhen: stepCountIs(5), // Run tools and continue, up to 5 steps.
  });
  return result.toUIMessageStreamResponse();
}
```

</Step>

<Step title="Configure the adapter" source="https://github.com/thesysdev/openui/blob/main/examples/agent-frameworks/vercel-ai-sdk/src/app/page.tsx">

Create the client connection with `fetchLLM`. `vercelAIAdapter()` reads the UIMessage stream, and `vercelAIMessageFormat` sends the conversation as `UIMessage[]`, which the route passes to `convertToModelMessages()`.

```ts title="src/app/page.tsx"
import { fetchLLM, vercelAIAdapter, vercelAIMessageFormat } from "@openuidev/react-ui";

const llm = fetchLLM({
  url: "/api/chat",
  // Parses the UIMessage stream that toUIMessageStreamResponse() returns.
  streamAdapter: vercelAIAdapter(),
  // Sends UIMessage[], which the route passes to convertToModelMessages().
  messageFormat: vercelAIMessageFormat,
});
```

</Step>

<Step title="Connect your chat UI" source="https://github.com/thesysdev/openui/blob/main/examples/agent-frameworks/vercel-ai-sdk/src/app/page.tsx">

Pass that `llm` to `AgentInterface` for a ready-made chat app, or to `ChatProvider` from `@openuidev/react-headless` to build your own UI. Either way, streamed text, tool calls and multi-step runs arrive as the same chat events.

```tsx title="src/app/page.tsx"
"use client";

import { AgentInterface } from "@openuidev/react-ui";
import { openuiLibrary } from "@openuidev/react-ui/genui-lib";

// `llm` is the fetchLLM() connection from the previous step, in this same file.

export default function Page() {
  return <AgentInterface llm={llm} componentLibrary={openuiLibrary} />;
}
```

</Step>

</Steps>