Skip to content

fix(react): refresh transport across renders in useChat#16326

Closed
fayonation wants to merge 3 commits into
vercel:mainfrom
fayonation:fix/usechat-stale-transport-body-7819
Closed

fix(react): refresh transport across renders in useChat#16326
fayonation wants to merge 3 commits into
vercel:mainfrom
fayonation:fix/usechat-stale-transport-body-7819

Conversation

@fayonation

Copy link
Copy Markdown

Summary

Fixes #7819

Test plan

  • cd packages/react && pnpm build (DTS passes)
  • cd packages/react && pnpm test -- src/use-chat.ui.test.tsx (64 tests)
  • Verified new tests fail without the fix (2/2 fail on main use-chat.ts)

fayonation and others added 3 commits June 23, 2026 11:06
Delegate sendMessages and reconnectToStream through a stable transport
ref so inline DefaultChatTransport body/headers stay in sync with React
state across re-renders and async initialization.

Fixes vercel#7819

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
pull Bot pushed a commit to ben-vargas/ai that referenced this pull request Jul 1, 2026
…one (vercel#16546)

## Background

`useChat()` creates its `Chat` instance once and stores it in a
`useRef()`, so as a result, a `transport` passed to `useChat()` was
captured and then frozen at its value in the first render. If react
state later changes, the `transport` becomes stale.

A very similar problem was addressed in
vercel#8985 but with callbacks.

## Summary

Rather than passing the transport directly to `Chat`, this passes a
proxy object that always reads the latest transport (or falls back to a
default one).

This uses the same solution as in vercel#8985
for callbacks.

## Manual Verification

Added a route to the ai-e2e-next example at
`/chat/stale-transport-demo`. It reproduces the bug without the changes
made, and then is fixed with these changes.

## Related Issues

Fixes vercel#7819

Closes vercel#12330
Closes vercel#13464
Closes vercel#15380
Closes vercel#16326
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.

In useChat in @ai-sdk/react, state sent via transport#body is always stale

1 participant