Error is thrown while using `useChat` in client component in 3.3
Description
Since migrating from 3.2 to 3.3, I've been getting the following error from using useChat in a client component:
Error: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding `'use client'` to a module that was originally written for the server.
at trackUsedThenable (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js:9157:19)
at useThenable (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js:11483:16)
at Object.use (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js:11504:14)
at use (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/compiled/react/cjs/react.development.js:2572:21)
at useSWRHandler (webpack-internal:///(app-pages-browser)/./node_modules/swr/dist/core/index.mjs:511:13)
at eval (webpack-internal:///(app-pages-browser)/./node_modules/swr/dist/_internal/index.mjs:662:16)
at useSWRArgs (webpack-internal:///(app-pages-browser)/./node_modules/swr/dist/_internal/index.mjs:684:16)
at useChat (webpack-internal:///(app-pages-browser)/./node_modules/@ai-sdk/react/dist/index.mjs:306:109)
at AiAssistantForm (webpack-internal:///(app-pages-browser)/./components/AiAssistantForm/index.tsx:40:133)
Code example
'use client';
import { useChat } from 'ai/react';
// other imports
const AiAssistantForm = () => {
const {
messages,
input,
handleInputChange,
handleSubmit,
} = useChat({
keepLastMessageOnError: true,
maxToolRoundtrips: 2,
initialMessages: [{
id: 'initialMessage',
role: 'assistant',
content: 'Hello, how can I help you today ?',
}],
});
// The rest of my component
};
Additional context
Switching back to 3.2 makes it work fine.
Which Next.js / React versions are you using?
I'm using next 14.1.0 and react 18.2.0
Hi !
Any update on this issue ?
It seems like the problem appeared in 5b7b3bbe. (@ai-sdk/react 0.0.25) and more precisely the update of @ai-sdk/ui-utils from 0.0.15 to 0.0.16
Potentially with the change of the swr version. As today I still have this issue with ai 3.4.
I saw some swr related issues like https://github.com/vercel/ai/issues/1656.
Do you have any feedback on this @lgrammel ?
EDIT: adding the onToolCall parameter to the useChat() hook and using 2.2.0 or 2.2.6-beta.4 swr version seems to fix the issue.
But this is not a viable solution
any update on this?
@shivankkunwar migrating to ai v4.0 and swr 2.3 did the work for me
Identify issue type
Identified as Bug
Confidence: High
Rationale
This is a concrete regression in existing useChat behavior that unexpectedly breaks valid Client Component usage.
Evidence
- Upgrading AI SDK from 3.2 to 3.3 causes
useChatin a Client Component to throw an async/await unsupported error; downgrading restores working behavior. - The reporter narrowed the regression to
@ai-sdk/react0.0.25 and an@ai-sdk/ui-utils/SWR dependency update. - Adding
onToolCallor changing SWR versions worked around the error, and upgrading to AI SDK v4 with SWR 2.3 resolved it.