Skip to content

fix(ui): prevent TypeError in onFinish when activeResponse is undefined#12207

Open
codewithkenzo wants to merge 1 commit intovercel:mainfrom
codewithkenzo:fix/active-response-finally-block
Open

fix(ui): prevent TypeError in onFinish when activeResponse is undefined#12207
codewithkenzo wants to merge 1 commit intovercel:mainfrom
codewithkenzo:fix/active-response-finally-block

Conversation

@codewithkenzo
Copy link

Background

When using useChat with onFinish callback and resume enabled, users encounter:

TypeError: Cannot read properties of undefined (reading 'state')

This happens because this.activeResponse in the finally block can be:

  1. Undefined - if an error is thrown before assignment in the try block
  2. Overwritten - by concurrent makeRequest calls

Summary

Use local variable capture pattern to safely reference activeResponse in the finally block:

  • Declare let activeResponse before try block
  • Assign to local variable first, then to this.activeResponse
  • Check local variable in finally block before calling onFinish

This is a well-established pattern for try-finally resource management (see TypeScript #28153).

Manual Verification

Build passes locally. The fix is minimal and follows the exact pattern suggested by @codybrouwers in the issue comments, which multiple users confirmed works via patch-package.

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Related Issues

Fixes #8477

Thanks to @codybrouwers for identifying the root cause and providing the solution.

Use local variable capture pattern in makeRequest() to safely access
activeResponse in the finally block. This prevents 'Cannot read properties
of undefined' errors when:
- An error is thrown before activeResponse is assigned
- Concurrent makeRequest calls overwrite this.activeResponse

Fixes vercel#8477

Thanks to @codybrouwers for identifying the root cause and solution.
@vercel-ai-sdk vercel-ai-sdk bot added the ai/ui label Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError when using useChat onFinish with resume enabled

1 participant