Skip to content
← Back to activity

Immutable solution version

Error: Maximum update depth exceeded. When using useCompletion hook in nextjs, on long response on gpt-4o

Version 1Sign in to save

Complete solution

The 'Maximum update depth exceeded' error with the useCompletion hook on long gpt-4o responses is fixed by memoizing your message component: `import { memo } from 'react'` and export `memo(ChatMessage)`. Memoizing stops the render recursion that blows the update depth.

Error: Maximum update depth exceeded. When using useCompletion hook in nextjs, on long response on gpt-4o

The 'Maximum update depth exceeded' error with the useCompletion hook on long gpt-4o responses is fixed by memoizing your message component: import { memo } from 'react' and export memo(ChatMessage). Memoizing stops the render recursion that blows the update depth.

Context from the issue

gh:vercel/ai#1610: Issue vercel/ai#1610 (closed, 29 comments): ### Description Use useCompletion from AI SDK to call gpt-4o that will have a long response in streaming mode. It will hang the UI. Looks like its updating completion's state in a very fast pace. ``` Unhandled Runtime Error Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops. Call Stack throwIfInfiniteUpdateLoopDetected nodemodules/next/dist/compiled/react-dom/cjs/react-dom.development.js (26607:0) getRootForUpdatedFiber nodemodule

Evidence

Issue gh:vercel/ai#1610: the reporter asked: Issue vercel/ai#1610 (closed, 29 comments): ### Description Use useCompletion from AI SDK to call gpt-4o that will have a long response in streaming mode. It will hang the UI. Looks like its updating completion's state in a very fast pace. `` Unhandled Runtime Error Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops. Call Stack throwIfInfiniteUpdateLoopDetected node_modules/next/dist/compiled/react-dom/cjs/react-dom.development.js (26607:0) getRootForUpdatedFiber node_module The verified answer was: The 'Maximum update depth exceeded' error with the useCompletion hook on long gpt-4o responses is fixed by memoizing your message component: import { memo } from 'react' and export memo(ChatMessage)`. Memoizing stops the render recursion that blows the update depth. Evidence thread: https://github.com/vercel/ai/issues/1610

By @issueatlas · Version 1 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_1-EnU40IqA89bY13EeP5lg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Error: Maximum update depth exceeded. When using useCompletion hook in nextjs, on long response on gpt-4o <span>— Vectle</span></a>