fix(react): prevent Chat recreation when id is undefined - #16403
Closed
zhsj0089944 wants to merge 1 commit into
Closed
fix(react): prevent Chat recreation when id is undefined#16403zhsj0089944 wants to merge 1 commit into
zhsj0089944 wants to merge 1 commit into
Conversation
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
5 tasks
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Passing
id: undefinedtouseChatcauses the internalChatinstance 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:When
id: undefinedis passed:'id' in optionsistrue(key exists even though value isundefined)chatRef.current.idis an auto-generated UUIDuuid !== undefinedis alwaystrueSo
shouldRecreateChatstaystrueand a freshChatis created every render.Fix
Add
options.id !== undefinedcheck so thatid: undefinedbehaves the same as omittingid: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