Ai Sdk Ui
by @veeramanikandanr48
Build React chat interfaces with Vercel AI SDK v6. Covers useChat/useCompletion/useObject hooks, message parts structure, tool approval workflows, and 18 UI error solutions. Prevents documented issues with React Strict Mode, concurrent requests, stale closures, and tool approval edge cases. Use when: implementing AI chat UIs, migrating v5βv6, troubleshooting "useChat failed to parse stream", "stale body values", "React maximum update depth", "Cannot read properties of undefined (reading 'state'
clawhub install ai-sdk-uiπ About This Skill
name: ai-sdk-ui description: | Build React chat interfaces with Vercel AI SDK v6. Covers useChat/useCompletion/useObject hooks, message parts structure, tool approval workflows, and 18 UI error solutions. Prevents documented issues with React Strict Mode, concurrent requests, stale closures, and tool approval edge cases.
Use when: implementing AI chat UIs, migrating v5βv6, troubleshooting "useChat failed to parse stream", "stale body values", "React maximum update depth", "Cannot read properties of undefined (reading 'state')", or tool approval workflow errors. user-invocable: true
AI SDK UI - Frontend React Hooks
Frontend React hooks for AI-powered user interfaces with Vercel AI SDK v6.
Version: AI SDK v6.0.42 (Stable) Framework: React 18+/19, Next.js 14+/15+ Last Updated: 2026-01-20
AI SDK v6 Stable (January 2026)
Status: Stable Release Latest: ai@6.0.42, @ai-sdk/react@3.0.44, @ai-sdk/openai@3.0.7 Migration: Minimal breaking changes from v5 β v6
New UI Features in v6
1. Message Parts Structure (Breaking Change)
In v6, message content is now accessed via .parts array instead of .content:
// β v5 (OLD)
{messages.map(m => (
{m.content}
))}// β
v6 (NEW)
{messages.map(m => (
{m.parts.map((part, i) => {
if (part.type === 'text') return {part.text};
if (part.type === 'tool-invocation') return ;
if (part.type === 'file') return ;
return null;
})}
))}
Part Types:
text - Text content with .text propertytool-invocation - Tool calls with .toolName, .args, .resultfile - File attachments with .mimeType, .datareasoning - Model reasoning (when available)source - Source citations3. Agent Integration
Type-safe messaging with agents using InferAgentUIMessage:
import { useChat } from '@ai-sdk/react';
import type { InferAgentUIMessage } from 'ai';
import { myAgent } from './agent';export default function AgentChat() {
const { messages, sendMessage } = useChat>({
api: '/api/chat',
});
// messages are now type-checked against agent schema
}
4. Tool Approval Workflows (Human-in-the-Loop) Request user confirmation before executing tools:
import { useChat } from '@ai-sdk/react';
import { useState } from 'react';export default function ChatWithApproval() {
const { messages, sendMessage, addToolApprovalResponse } = useChat({
api: '/api/chat',
});
const handleApprove = (toolCallId: string) => {
addToolApprovalResponse({
toolCallId,
approved: true, // or false to deny
});
};
return (
{messages.map(message => (
{message.toolInvocations?.map(tool => (
tool.state === 'awaiting-approval' && (
Approve tool call: {tool.toolName}?
)
))}
))}
);
}
5. Auto-Submit Capability Automatically continue conversation after handling approvals:
import { useChat, lastAssistantMessageIsCompleteWithApprovalResponses } from '@ai-sdk/react';export default function AutoSubmitChat() {
const { messages, sendMessage } = useChat({
api: '/api/chat',
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
// Automatically resubmit after all approval responses provided
});
}
6. Structured Output in Chat
Generate structured data alongside tool calling (previously only available in useObject):
import { useChat } from '@ai-sdk/react';
import { z } from 'zod';const schema = z.object({
summary: z.string(),
sentiment: z.enum(['positive', 'neutral', 'negative']),
});
export default function StructuredChat() {
const { messages, sendMessage } = useChat({
api: '/api/chat',
// Server can now stream structured output with chat messages
});
}
useChat Hook - v4 β v5 Breaking Changes
CRITICAL: useChat no longer manages input state in v5!
v4 (OLD - DON'T USE):
const { messages, input, handleInputChange, handleSubmit, append } = useChat();
v5 (NEW - CORRECT):
const { messages, sendMessage } = useChat();
const [input, setInput] = useState('');
Summary of v5 Changes:
1. Input management removed: input, handleInputChange, handleSubmit no longer exist
2. append() β sendMessage(): New method for sending messages
3. onResponse removed: Use onFinish instead
4. initialMessages β controlled mode: Use messages prop for full control
5. maxSteps removed: Handle on server-side only
See references/use-chat-migration.md for complete migration guide.
useAssistant Hook (Deprecated)
> β οΈ Deprecation Notice: useAssistant is deprecated as of AI SDK v5. OpenAI Assistants API v2
> will sunset on August 26, 2026. For new projects, use useChat with custom backend logic instead.
> See the openai-assistants skill for migration guidance.
Interact with OpenAI-compatible assistant APIs with automatic UI state management.
Import:
import { useAssistant } from '@ai-sdk/react';
Basic Usage:
'use client';
import { useAssistant } from '@ai-sdk/react';
import { useState, FormEvent } from 'react';export default function AssistantChat() {
const { messages, sendMessage, isLoading, error } = useAssistant({
api: '/api/assistant',
});
const [input, setInput] = useState('');
const handleSubmit = (e: FormEvent) => {
e.preventDefault();
sendMessage({ content: input });
setInput('');
};
return (
{messages.map(m => (
{m.role}: {m.content}
))}
{error && {error.message}}
);
}
Use Cases:
See official docs for complete API reference: https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-assistant
Top UI Errors & Solutions
See references/top-ui-errors.md for complete documentation. Quick reference:
1. useChat Failed to Parse Stream
Error: SyntaxError: Unexpected token in JSON at position X
Cause: API route not returning proper stream format.
Solution:
// β
CORRECT
return result.toDataStreamResponse();// β WRONG
return new Response(result.textStream);
2. useChat No Response
Cause: API route not streaming correctly.
Solution:
// App Router - use toDataStreamResponse()
export async function POST(req: Request) {
const result = streamText({ /* ... */ });
return result.toDataStreamResponse(); // β
}// Pages Router - use pipeDataStreamToResponse()
export default async function handler(req, res) {
const result = streamText({ /* ... */ });
return result.pipeDataStreamToResponse(res); // β
}
3. Streaming Not Working When Deployed
Cause: Deployment platform buffering responses.
Solution: Vercel auto-detects streaming. Other platforms may need configuration.
4. Stale Body Values with useChat
Cause: body option captured at first render only.
Solution:
// β WRONG - body captured once
const { userId } = useUser();
const { messages } = useChat({
body: { userId }, // Stale!
});// β
CORRECT - use controlled mode
const { userId } = useUser();
const { messages, sendMessage } = useChat();
sendMessage({
content: input,
data: { userId }, // Fresh on each send
});
5. React Maximum Update Depth
Cause: Infinite loop in useEffect.
Solution:
// β WRONG
useEffect(() => {
saveMessages(messages);
}, [messages, saveMessages]); // saveMessages triggers re-render!// β
CORRECT
useEffect(() => {
saveMessages(messages);
}, [messages]); // Only depend on messages
See references/top-ui-errors.md for 13 more common errors (18 total documented).
Streaming Best Practices
Performance
Always use streaming for better UX:
// β
GOOD - Streaming (shows tokens as they arrive)
const { messages } = useChat({ api: '/api/chat' });// β BAD - Non-streaming (user waits for full response)
const response = await fetch('/api/chat', { method: 'POST' });
UX Patterns
Show loading states:
{isLoading && AI is typing...}
Provide stop button:
{isLoading && }
Auto-scroll to latest message:
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [messages]);
Disable input while loading:
See references/streaming-patterns.md for comprehensive best practices.
React Strict Mode Considerations
React Strict Mode intentionally double-invokes effects to catch bugs. When using useChat or useCompletion in effects (auto-resume, initial messages), guard against double execution to prevent duplicate API calls and token waste.
Problem:
'use client';
import { useChat } from '@ai-sdk/react';
import { useEffect } from 'react';export default function Chat() {
const { messages, sendMessage, resumeStream } = useChat({
api: '/api/chat',
resume: true,
});
useEffect(() => {
// β Triggers twice in strict mode β two concurrent streams
sendMessage({ content: 'Hello' });
// or
resumeStream();
}, []);
}
Solution:
// β
Use ref to track execution
import { useRef } from 'react';const hasSentRef = useRef(false);
useEffect(() => {
if (hasSentRef.current) return;
hasSentRef.current = true;
sendMessage({ content: 'Hello' });
}, []);
// For resumeStream specifically:
const hasResumedRef = useRef(false);
useEffect(() => {
if (!autoResume || hasResumedRef.current || status === 'streaming') return;
hasResumedRef.current = true;
resumeStream();
}, [autoResume, resumeStream, status]);
Why It Happens: React Strict Mode double-invokes effects to surface side effects. The SDK doesn't guard against concurrent requests, so both invocations create separate streams that fight for state updates.
Impact: Duplicate messages, doubled token usage, race conditions causing TypeError: "Cannot read properties of undefined (reading 'state')".
Source: GitHub Issue #7891, Issue #6166
When to Use This Skill
Use ai-sdk-ui When:
Don't Use When:
Related Skills:
Package Versions
Stable (v6 - Recommended):
{
"dependencies": {
"ai": "^6.0.8",
"@ai-sdk/react": "^3.0.6",
"@ai-sdk/openai": "^3.0.2",
"react": "^18.3.0",
"zod": "^3.24.2"
}
}
Legacy (v5):
{
"dependencies": {
"ai": "^5.0.99",
"@ai-sdk/react": "^1.0.0",
"@ai-sdk/openai": "^2.0.68"
}
}
Version Notes:
Links to Official Documentation
Core UI Hooks:
Advanced Topics (Link Only):
Next.js Integration:
Migration & Troubleshooting:
Vercel Deployment:
Templates
This skill includes the following templates in templates/:
1. use-chat-basic.tsx - Basic chat with manual input (v5 pattern) 2. use-chat-tools.tsx - Chat with tool calling UI rendering 3. use-chat-attachments.tsx - File attachments support 4. use-completion-basic.tsx - Basic text completion 5. use-object-streaming.tsx - Streaming structured data 6. nextjs-chat-app-router.tsx - Next.js App Router complete example 7. nextjs-chat-pages-router.tsx - Next.js Pages Router complete example 8. nextjs-api-route.ts - API route for both App and Pages Router 9. message-persistence.tsx - Save/load chat history 10. custom-message-renderer.tsx - Custom message components with markdown 11. package.json - Dependencies template
Reference Documents
See references/ for:
Production Tested: WordPress Auditor (https://wordpress-auditor.webfonts.workers.dev) Last verified: 2026-01-20 | Skill version: 3.1.0 | Changes: Updated to AI SDK v6.0.42 (+19 patches). Added React Strict Mode section. Expanded Issue #7 (stale body) with 3 workarounds. Added 6 new issues: TypeError with resume+onFinish (#13), concurrent sendMessage state corruption (#14), tool approval callback edge case (#15), ZodError on early stop (#16), convertToModelMessages tool approval bug (#17), undefined id infinite loop (#18). Error count: 12β18.