ai icon indicating copy to clipboard operation
ai copied to clipboard

Error is thrown while using `useChat` in client component in 3.3

Open defless opened this issue 2 years ago • 7 comments

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.

defless avatar Aug 22 '24 09:08 defless

Which Next.js / React versions are you using?

lgrammel avatar Aug 22 '24 15:08 lgrammel

I'm using next 14.1.0 and react 18.2.0

defless avatar Aug 23 '24 12:08 defless

Hi !

Any update on this issue ?

maximedasilva avatar Sep 10 '24 17:09 maximedasilva

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 ?

Screenshot 2024-10-28 at 16 44 20

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

defless avatar Oct 28 '24 15:10 defless

any update on this?

shivankkunwar avatar Jan 03 '25 14:01 shivankkunwar

@shivankkunwar migrating to ai v4.0 and swr 2.3 did the work for me

defless avatar Feb 04 '25 10:02 defless

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 useChat in a Client Component to throw an async/await unsupported error; downgrading restores working behavior.
  • The reporter narrowed the regression to @ai-sdk/react 0.0.25 and an @ai-sdk/ui-utils/SWR dependency update.
  • Adding onToolCall or changing SWR versions worked around the error, and upgrading to AI SDK v4 with SWR 2.3 resolved it.

lgrammel avatar Jul 22 '26 17:07 lgrammel