Skip to content

fix(react): prevent Chat recreation when id is undefined - #16403

Closed
zhsj0089944 wants to merge 1 commit into
vercel:mainfrom
zhsj0089944:fix/usechat-id-undefined
Closed

fix(react): prevent Chat recreation when id is undefined#16403
zhsj0089944 wants to merge 1 commit into
vercel:mainfrom
zhsj0089944:fix/usechat-id-undefined

Conversation

@zhsj0089944

Copy link
Copy Markdown

Problem

Passing id: undefined to useChat causes the internal Chat instance to be recreated on every render. Sent messages and in-flight streams disappear immediately.

This is easy to hit because id={conversationId ?? undefined} is a common pattern.

Root cause

In useChat:

const shouldRecreateChat =
    ('chat' in options && options.chat !== chatRef.current) ||
    ('id' in options && chatRef.current.id !== options.id);

When id: undefined is passed:

  1. 'id' in options is true (key exists even though value is undefined)
  2. chatRef.current.id is an auto-generated UUID
  3. uuid !== undefined is always true

So shouldRecreateChat stays true and a fresh Chat is created every render.

Fix

Add options.id !== undefined check so that id: undefined behaves the same as omitting id:

  const shouldRecreateChat =
    ('chat' in options && options.chat !== chatRef.current) ||
-   ('id' in options && chatRef.current.id !== options.id);
+   ('id' in options && options.id !== undefined && chatRef.current.id !== options.id);

Fixes #16226

Passing id: undefined to useChat caused the Chat instance to be
recreated on every render because 'id' in options is true (key
exists) but chatRef.current.id (a UUID) !== undefined.

Add options.id !== undefined check so that id: undefined behaves
the same as omitting id entirely.

Fixes vercel#16226
@gr2m gr2m closed this in #16484 Jun 29, 2026
pull Bot pushed a commit to Abaso007/ai that referenced this pull request Jun 29, 2026
## Background

Passing `id: undefined` to `useChat` currently recreates the internal
`Chat` instance on every render because the `id` key exists in options
while the auto-generated chat ID never equals `undefined`. That clears
messages and stream state immediately after updates, which makes the
common `id={conversationId ?? undefined}` pattern behave differently
from omitting `id`.

I confirmed the reproduction from vercel#16226 with a focused regression test:
with the old guard, calling `setMessages` while `id` is explicitly
`undefined` immediately rerenders into a fresh chat with a new generated
ID and `messages: []`.

I compared the two open PRs before choosing this implementation:

- vercel#16228 by @Aayush-engineer uses the nullish guard and adds a
changeset, but does not include a regression test.
- vercel#16403 by @zhsj0089944 uses an `undefined` guard, but does not include
a regression test or changeset.

This PR uses the nullish guard from vercel#16228 because it makes explicit
`undefined` behave like an omitted ID and also treats a JavaScript
`null` value defensively as “no provided ID”. It adds the missing
hook-level regression test and a patch changeset.

Credit to @Aayush-engineer and @zhsj0089944 for the prior PRs and
diagnosis.

## Summary

- Only recreate the internal `Chat` from `useChat` when a provided `id`
is non-nullish and different from the current chat ID.
- Add a regression test that verifies `useChat({ id: undefined })`
preserves the generated ID and messages across rerenders.
- Add a patch changeset for `@ai-sdk/react`.

## Manual Verification

Confirmed the reproduction by temporarily restoring the old guard and
running:

```sh
pnpm --filter @ai-sdk/react exec vitest --config vitest.config.js --run src/use-chat.ui.test.tsx -t "should not recreate chat when id is explicitly undefined"
```

With the old guard, the test failed because messages reset to `[]` and
the generated chat ID changed. With this fix, the same test passes.

## Checklist

- [x] All commits are signed (PRs with unsigned commits cannot be
merged)
- [x] Tests have been added / updated (for bug fixes / features)
- [ ] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] I have reviewed this pull request (self-review)

## Related Issues

Fixes vercel#16226.
Supersedes and closes vercel#16228.
Supersedes and closes vercel#16403.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useChat({ id: undefined }) recreates the Chat instance on every render, wiping messages

1 participant