Skip to content

Add AI SDK compatibility via v0/ai-sdk and v0/react#162

Draft
nandorojo wants to merge 2 commits into
v2from
f/ai-sdk
Draft

Add AI SDK compatibility via v0/ai-sdk and v0/react#162
nandorojo wants to merge 2 commits into
v2from
f/ai-sdk

Conversation

@nandorojo

Copy link
Copy Markdown
Collaborator

Additive AI SDK v7 compatibility for the v0 SDK: two new entry points, v0/ai-sdk and v0/react, with optional peer deps on ai, @ai-sdk/react, and react. No changes to the existing API.

Server

// app/api/chat/route.ts
import { v0 } from 'v0'
import { toUIMessageStreamResponse } from 'v0/ai-sdk'

export async function POST(request: Request) {
  const { chatId, message, attachments } = await request.json()

  return toUIMessageStreamResponse(
    chatId
      ? await v0.messages.sendStream({ chatId, message, attachments })
      : await v0.chats.createStream({ message, attachments }),
  )
}

// stream resumption
export async function GET(request: Request) {
  const chatId = new URL(request.url).searchParams.get('chatId')
  if (!chatId) return new Response(null, { status: 204 })

  try {
    return toUIMessageStreamResponse(await v0.chats.resume({ chatId }))
  } catch {
    return new Response(null, { status: 204 })
  }
}

Client

'use client'

import { useV0Chat } from 'v0/react'

const { messages, sendMessage, status, chatId } = useV0Chat()

// or, with an existing chat + resumption of in-flight generations
useV0Chat({ chatId, messages, resume: true })

Prefer the plain hook? Same route handler:

import { useChat } from '@ai-sdk/react'
import type { V0UIMessage } from 'v0/ai-sdk'

useChat<V0UIMessage>() // just works

Fully typed messages, derived from the SDK

V0UIMessage derives entirely from the existing Message type. text/thinking map to the AI SDK's native text/reasoning parts; every other v0 part becomes a typed data-* part; the remaining Message fields are the metadata.

{message.parts.map((part, index) => {
  switch (part.type) {
    case 'text':
      return <p key={index}>{part.text}</p>
    case 'reasoning':
      return <em key={index}>{part.text}</em>
    case 'data-file-edit':
      // part.data.operation: 'create' | 'update' | 'delete' | 'rename' | 'patch'
      return <code key={index}>{`${part.data.operation} ${part.data.path}`}</code>
    case 'data-bash':
      return <pre key={index}>{`$ ${part.data.command}`}</pre>
    default:
      return null
  }
})}

message.metadata?.chatId
message.metadata?.usage?.creditsCost.total

Loading history

import { toUIMessages } from 'v0/ai-sdk'

const response = await v0.messages.list({ chatId, limit: 100 })
const messages = toUIMessages(response.data.messages) // pass to useV0Chat({ chatId, messages })

Also exports toUIMessageStream (raw chunk stream), toUIMessage, and fromUIMessage for custom setups. 55 tests cover the converters, the stream protocol adapter (driven through the SDK's real jsondiffpatch deltas), and useV0Chat rendered against a recorded fetch.

@vercel

vercel Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
v0-sdk-clone-demo Error Error Open in v0 Jul 8, 2026 8:45pm
v0-sdk-playground Error Error Open in v0 Jul 8, 2026 8:45pm
v0-sdk-simple-demo Error Error Open in v0 Jul 8, 2026 8:45pm

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestion:

Named function expression in useEffect shadows the memoized generateNextChatId, causing infinite recursion and a stack overflow on mount.

Fix on Vercel

@nandorojo nandorojo changed the base branch from main to v2 July 8, 2026 20:02
@socket-security

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addednpm/​@​ai-sdk/​react@​4.0.16981007598100
Addednpm/​legid@​0.1.49710010082100
Addednpm/​ai@​7.0.158610010099100
Addednpm/​@​happy-dom/​global-registrator@​20.10.61001008795100

View full report

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.

1 participant