import Link from "next/link"; import { Code } from "@/components/code"; export const metadata = { title: "AI SDK Integration | json-render", }; export default function AiSdkPage() { return (

AI SDK Integration

Use json-render with the Vercel AI SDK for seamless streaming.

Installation

npm install ai

API Route Setup

{`// app/api/generate/route.ts import { streamText } from 'ai'; import { catalog } from '@/lib/catalog'; export async function POST(req: Request) { const { prompt, currentTree } = await req.json(); // Generate system prompt from catalog const systemPrompt = catalog.prompt(); // Optionally include current UI state for context const contextPrompt = currentTree ? \`\\n\\nCurrent UI state:\\n\${JSON.stringify(currentTree, null, 2)}\` : ''; const result = streamText({ model: 'anthropic/claude-haiku-4.5', system: systemPrompt + contextPrompt, prompt, }); return result.toTextStreamResponse(); }`}

Client-Side Hook

Use useUIStream on the client:

{`'use client'; import { useUIStream, Renderer } from '@json-render/react'; function GenerativeUI() { const { spec, isStreaming, error, send } = useUIStream({ api: '/api/generate', }); return (
{error &&

{error.message}

}
); }`}

Prompt Engineering

The catalog.prompt() method creates an optimized system prompt that:

Custom System Prompts

Pass custom rules to tailor AI behavior:

{`const systemPrompt = catalog.prompt({ customRules: [ 'Always use Card components for grouping related content', 'Prefer horizontal layouts (Row) for metrics', 'Use consistent spacing with padding="md"', ], });`}

Next

Learn about{" "} progressive streaming .

); }