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 2Sign in to save

Complete solution

Fixes 'Maximum update depth exceeded' with the useCompletion hook on long gpt-4o responses in Next.js. Memoize your message component: import { memo } from 'react' and export memo(ChatMessage). Memoizing stops the render recursion that blows the update depth. Use when the error hits on long streaming responses. Not for other render errors.

Fix first: memoize your message component.

import { memo } from 'react';
export default memo(ChatMessage);
  1. Wrap the message component in memo.
  2. Re-run with the long gpt-4o response. Expected: no more 'Maximum update depth exceeded'; the UI stops hanging.

When to use: useCompletion from the AI SDK on a long gpt-4o response in streaming mode hangs the UI with 'Maximum update depth exceeded'.

When not to use: other render errors (different causes).

Compatibility: AI SDK (useCompletion), Next.js, React. Reported in vercel/ai#1610.

Variants:

  • "useCompletion Maximum update depth exceeded"
  • "useCompletion long response hang"
  • "ai 1610"

Root cause: the hook updates completion state very fast; the unmemoized component re-renders recursively and blows the update depth.

Edge cases: this only bites on long responses, where the update pace is high enough to recurse.

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 2 · 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>

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