# Vercel AI SDK accessibility

Add paced screen-reader updates to AI SDK through documented state, finish callbacks, and error callbacks.

Canonical: https://generativea11y.com/docs/integrations/ai-sdk
Markdown: https://generativea11y.com/markdown/docs/integrations/ai-sdk
Documentation channel: source checkout (may include unreleased APIs).
Repository: https://github.com/bhaveshchow20/generative-a11y
Build base revision: f7bcd7cee3f4381b23b5660942666a3dbe12f3e5 (modified checkout: false).
Source package manifest versions (not release parity): @generative-a11y/ag-ui@0.3.0, @generative-a11y/ai-sdk@0.2.0, @generative-a11y/assistant-ui@0.2.0, @generative-a11y/core@0.4.0, @generative-a11y/devtools@0.2.1, @generative-a11y/dom@0.3.0, @generative-a11y/react@0.2.0.
Compare your installed package version and its release tag before using these examples. Package manifest versions alone do not identify unreleased changes. See https://generativea11y.com/docs/stability.

## Install for your existing app

<Tabs groupId="package-manager" items={["npm","pnpm","yarn","bun"]}>
<Tab value="npm">
```shell title="Terminal"
npm install @generative-a11y/react @generative-a11y/ai-sdk
```
</Tab>

<Tab value="pnpm">
```shell title="Terminal"
pnpm add @generative-a11y/react @generative-a11y/ai-sdk
```
</Tab>

<Tab value="yarn">
```shell title="Terminal"
yarn add @generative-a11y/react @generative-a11y/ai-sdk
```
</Tab>

<Tab value="bun">
```shell title="Terminal"
bun add @generative-a11y/react @generative-a11y/ai-sdk
```
</Tab>
</Tabs>

Requires AI SDK 7 / `@ai-sdk/react` 4 and React 18.2+ or 19.
See [Compatibility](#compatibility) for the supported minor versions and peers.

## React integration

### Import the hooks

Add these imports to your chat module. In a React Server Components app, keep
it behind a `"use client"` boundary.

```tsx title="components/chat.tsx"
"use client";

import { DefaultChatTransport } from "ai";
import { useChat } from "@ai-sdk/react";
import { A11yProvider, useRuntime } from "@generative-a11y/react";
import {
  useChatAccessibility,
  useObserveChatAccessibility,
} from "@generative-a11y/ai-sdk/react";
```

### Connect your chat

Inside your existing `Chat()`, create accessibility before `useChat`, then
observe the returned state. Keep your transport and other chat options; the
example below uses `/api/chat`.

```tsx title="components/chat.tsx"
const runtime = useRuntime();
const accessibility = useChatAccessibility({
  runtime,
  scopeId: "support",
});
const chat = useChat({
  id: "support",
  transport: new DefaultChatTransport({ api: "/api/chat" }),
  ...accessibility.chatCallbacks,
});
useObserveChatAccessibility({ integration: accessibility, snapshot: chat });
```

### Add the provider

Wrap your chat once:

```tsx title="components/chat.tsx"
export function App() {
  return (
    <A11yProvider>
      <Chat />
    </A11yProvider>
  );
}
```

Keep using `chat` in your existing UI. See the
[complete copyable component](/api/ai-sdk/use-chat-accessibility) for a minimal
message list and composer.

### Existing callbacks

Already use `onFinish` or `onError`? Pass your existing functions to
`useChatAccessibility` so they are composed rather than replaced. Keep the rest
of your options, including transport, in `existingOptions`, and spread the
composed callbacks last. Omit either callback if your app does not use it.

```tsx title="components/chat.tsx"
const accessibility = useChatAccessibility({
  runtime,
  scopeId: "support",
  onFinish,
  onError,
});
const chat = useChat({ ...existingOptions, ...accessibility.chatCallbacks });
```

### Ownership and behavior

**One provider per chat surface**

`A11yProvider` owns runtime and browser delivery. The adapter owns observer
cleanup. Both clean up on unmount; keep the provider mounted across responses
and `scopeId` stable for that session.

This adds paced live-region updates without changing your visible UI or moving
focus during ordinary streaming. Your app still owns semantic structure,
keyboard controls, and accessible content. DOM checks do not prove screen-reader
output.

## Advanced: preconstructed Chat

With `useChat({ chat: existingChat })`, other initialization options, including
callbacks, are ignored. Compose callbacks when constructing that chat using the
[observer API](/api/ai-sdk), and manage its subscription and cleanup at that
boundary.

## Compatibility

| Dependency            | Supported range                                   | Checked version |
| --------------------- | ------------------------------------------------- | --------------- |
| Node.js               | 22+                                               | 22.16.0         |
| `ai`                  | `>=7.0.0 <7.1.0`                                  | 7.0.77          |
| `@ai-sdk/react`       | `>=4.0.0 <4.1.0`                                  | 4.0.80          |
| `react` / `react-dom` | 18.2+ within major 18, or 19.x                    | 19.2.8          |
| `zod` (host SDK peer) | 3.25.76+ within major 3, or 4.1.8+ within major 4 | 4.4.3           |

Your existing AI SDK app supplies these dependencies and a working backend.
Core and DOM install transitively; declare them directly only if your own code
imports them. The React hooks live at `@generative-a11y/ai-sdk/react`; the root
observer entry remains usable without React.

## Lifecycle mapping

The adapter translates confirmed public SDK evidence:

| Evidence                                  | Reports                                                |
| ----------------------------------------- | ------------------------------------------------------ |
| Append-only assistant text                | Response text updates                                  |
| Tool parts, approval IDs, and source IDs  | Tools, approvals, and citations                        |
| Composed `onFinish` / `onError` callbacks | Completion, cancellation, failure, and connection loss |

Retry and regenerate detection are unavailable. A successful finish after a
disconnection reports recovery. See the [observer reference](/api/ai-sdk) for
exact mappings and limits.

## Troubleshooting

* **Missing completion updates:** create accessibility before `useChat` and spread
  its composed callbacks last.
* **Repeated output:** keep `scopeId` stable and attach only one observer per chat.
* **Existing messages are silent:** the first snapshot establishes a history
  baseline. The adapter observes subsequent updates without replaying history.
