Skip to content

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

Closed
naumanAhmed3 wants to merge 1 commit into
vercel:mainfrom
naumanAhmed3:fix-usechat-transport-stale
Closed

fix(react): refresh transport across renders in useChat#15380
naumanAhmed3 wants to merge 1 commit into
vercel:mainfrom
naumanAhmed3:fix-usechat-transport-stale

Conversation

@naumanAhmed3

Copy link
Copy Markdown

What

useChat previously captured transport (and any body / headers / prepareSendMessagesRequest baked into it) on the very first render via the initial new Chat(...) call, so an inline transport closing over component state would forever send the value from that first render. The community report in #7819 shows the most common shape:

const [subagent, setSubagent] = useState('todos-agent');

useChat({
  transport: new DefaultChatTransport({
    body: { subagent },          // ← always 'todos-agent', even after setSubagent(...)
  }),
});

The other callback options (onFinish, onError, onToolCall, ...) already use a callbacksRef pattern to stay fresh across renders. This PR extends the same pattern to transport:

  1. Keep the user-supplied transport in a userTransportRef that gets refreshed on every render.
  2. Create a stable delegating ChatTransport proxy once. Its identity never changes, so Chat happily keeps it across renders, but each sendMessages / reconnectToStream call reads through to whatever the most-recent user transport is.

If the user does not supply a transport, the ref stays unset and Chat falls back to its built-in default (no behavior change for that path).

Why this approach

  • Symmetric with the existing callbacksRef pattern for onFinish / onError / onToolCall already present in this hook — least surprising for readers.
  • No Chat API change. transport is private readonly on AbstractChat and stays that way; the proxy lives on the React side only.
  • Works for both shapes users report:
    • inline new DefaultChatTransport({ body: { state } }) recreated each render
    • a custom transport instance that itself reads from React/Zustand/etc. — both go through the same userTransportRef.

Tests

Adds one regression test (describe('transport state stays fresh across renders (#7819)')) that mirrors the exact issue body:

  • Renders a component with [subagent, setSubagent] = useState('todos-agent')
  • Passes that into body: { subagent } on a DefaultChatTransport constructed inline
  • Clicks a button to set the state to 'research-agent'
  • Clicks send and asserts the request body's subagent is 'research-agent', not the stale 'todos-agent'

Before this fix the assertion would fail with 'todos-agent'.

Notes

  • I don't have a local Node toolchain to run vitest myself; relying on CI to confirm the regression test and the rest of the suite still pass. Happy to push fixups for any maintainer feedback.

Closes #7819.

useChat previously captured the transport (and any inline body / headers /
prepareSendMessagesRequest baked into it) on the very first render, so
inline transports closing over component state forever sent the
first-render value.

Apply the same pattern already used for onFinish / onError / onToolCall:
keep the user's transport in a ref, and pass Chat a stable delegating
proxy that reads the latest transport on every sendMessages /
reconnectToStream call.

Closes vercel#7819.
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