Skip to content

convert internal AI SDK stream events to UIMessageStreamPart format#678

Merged
whoiskatrin merged 2 commits intomainfrom
ai-sdk-finish-mismatch
Nov 26, 2025
Merged

convert internal AI SDK stream events to UIMessageStreamPart format#678
whoiskatrin merged 2 commits intomainfrom
ai-sdk-finish-mismatch

Conversation

@whoiskatrin
Copy link
Copy Markdown
Contributor

No description provided.

…fix error handling in useAgentChat by wrapping controller.close() in try-catch
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Nov 26, 2025

🦋 Changeset detected

Latest commit: 447088b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Convert internal AI SDK stream events to UIMessageStreamPart format.
@claude
Copy link
Copy Markdown

claude bot commented Nov 26, 2025

Claude Code Review

Summary: This PR properly fixes the AI SDK v5 stream event incompatibility described in issue #677. The implementation is clean and addresses the root cause.

Issues Found

1. Type safety issue in event transformation (ai-chat-agent.ts:1232-1236)

The destructuring assumes finishReason is a string, but the AI SDK defines it as a union type. While this works at runtime, the cast is misleading. Consider using 'finishReason: unknown' for accuracy.

2. Missing test coverage

The existing test file (react-tests/use-agent-chat.test.tsx) does not cover:

  • The finish event transformation logic
  • The try-catch around controller.close()
  • Error cases in stream processing

This is a critical bug fix that should have tests to prevent regression.

What Works Well

  • Root cause fix: Correctly converts internal AI SDK events to UI stream format
  • Defensive error handling: The try-catch blocks around controller.close() properly handle race conditions
  • Clear documentation: Comment explains the issue and references the GitHub issue
  • Preserved stream storage: The transformation happens before both storage and broadcast

The PR is solid overall. The type safety issue is minor, but missing test coverage is a real gap for a bug fix.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Nov 26, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@678

commit: 447088b

@claude
Copy link
Copy Markdown

claude bot commented Nov 26, 2025

Claude Code Review

Fix for AI SDK v5 stream event compatibility

This PR correctly addresses the type mismatch between AI SDK internal LanguageModelV3StreamPart and the UIMessageStreamPart format expected by the frontend.

Issues Found

1. Potential data loss in event transformation (ai-chat-agent.ts:1233)

The destructuring with type assertion may drop properties if data has additional fields beyond the assertion. Consider being more explicit about which properties to preserve.

2. Missing test coverage

No tests added for the stream event transformation logic. Given this fixes issue 677, tests would prevent regression:

  • Unit test for finish event transformation
  • Integration test validating complete flow
  • Test for try-catch blocks in ai-react.tsx

3. Silent error suppression (ai-react.tsx:260, 290)

Empty catch blocks suppress all controller.close errors. Could hide unexpected issues worth logging for debugging.

Summary

Core fix is sound. Main concerns: test coverage and error suppression breadth.

@whoiskatrin whoiskatrin merged commit cccbd0f into main Nov 26, 2025
6 checks passed
@whoiskatrin whoiskatrin deleted the ai-sdk-finish-mismatch branch November 26, 2025 10:17
@github-actions github-actions bot mentioned this pull request Nov 25, 2025
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.

2 participants