Skip to content

Response generation and hooks#3

Draft
jcourson-bg wants to merge 3 commits intomainfrom
cursor/response-generation-and-hooks-30e9
Draft

Response generation and hooks#3
jcourson-bg wants to merge 3 commits intomainfrom
cursor/response-generation-and-hooks-30e9

Conversation

@jcourson-bg
Copy link
Copy Markdown
Owner

Background

This change addresses a user request to enable streaming MDX/JSON tree structures (and patches) to a client, particularly for React Native, to avoid client-side markdown parsing while retaining the useChat developer experience. The goal was to demonstrate how to achieve this with minimal to no changes to the AI SDK itself.

Summary

This PR introduces a new example (examples/next-openai/app/use-chat-mdx-tree-patches) that showcases how to stream structured data parts alongside regular assistant text using the existing useChat hook and DefaultChatTransport.

The example includes:

  • A server route (app/api/use-chat-mdx-tree-patches/route.ts) that:
    • Streams an initial data-mdxTree snapshot.
    • Streams incremental data-mdxPatch events as transient: true chunks.
    • Streams a final data-mdxTree snapshot.
  • A client page (app/use-chat-mdx-tree-patches/page.tsx) that:
    • Uses useChat with DefaultChatTransport.
    • Subscribes to onData to apply data-mdxPatch events to a local JSON tree cache.
    • Renders content directly from the JSON tree, bypassing client-side markdown parsing.

This demonstrates that the AI SDK's data-* UI message chunk mechanism can be leveraged for advanced streaming scenarios like MDX tree patches without requiring core SDK modifications.

Manual Verification

  1. Run the next-openai example: pnpm --filter next-openai dev
  2. Navigate to http://localhost:3000/use-chat-mdx-tree-patches
  3. Type a message and submit.
  4. Observe the "Rendered from streamed 'MDX tree'" section updating incrementally as the AI streams its response. This section is rendered from the client-side JSON tree, which is updated by data-mdxPatch chunks.
  5. Below the rendered text, observe the full data-mdxTree part in the message history, showing the final JSON tree snapshot.

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)

Future Work

  • Consider adding first-class support for data-* chunks with { patch, protocol } semantics in processUIMessageStream to automatically apply patches to existing data parts, reducing boilerplate in onData handlers.
  • Explore adding a dedicated React Native fetch adapter or transport example within the SDK.

Open in Cursor Open in Web

This commit introduces an example demonstrating how to stream MDX tree data and patches to the client for real-time rendering.

Co-authored-by: jcourson8 <jcourson8@gmail.com>
@cursor
Copy link
Copy Markdown

cursor bot commented Jan 9, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

cursoragent and others added 2 commits January 9, 2026 04:07
Co-authored-by: jcourson8 <jcourson8@gmail.com>
Co-authored-by: jcourson8 <jcourson8@gmail.com>
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