You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: content/docs/05-ai-sdk-ui/01-overview.mdx
+5-3
Original file line number
Diff line number
Diff line change
@@ -9,9 +9,10 @@ Vercel AI SDK UI is designed to help you build interactive chat, completion, and
9
9
10
10
Vercel AI SDK UI provides robust abstractions that simplify the complex tasks of managing chat streams and UI updates on the frontend, enabling you to develop dynamic AI-driven interfaces more efficiently. With three main hooks — **`useChat`**, **`useCompletion`**, and **`useAssistant`** — you can incorporate real-time chat capabilities, text completions, and interactive assistant features into your app.
11
11
12
-
-**`useChat`** offers real-time streaming of chat messages, abstracting state management for inputs, messages, loading, and errors, allowing for seamless integration into any UI design.
13
-
-**`useCompletion`** enables you to handle text completions in your applications, managing chat input state and automatically updating the UI as new completions are streamed from your AI provider.
14
-
-**`useAssistant`** is designed to facilitate interaction with OpenAI-compatible assistant APIs, managing UI state and updating it automatically as responses are streamed.
12
+
-**[`useChat`](/docs/reference/ai-sdk-ui/use-chat)** offers real-time streaming of chat messages, abstracting state management for inputs, messages, loading, and errors, allowing for seamless integration into any UI design.
13
+
-**[`useCompletion`](/docs/reference/ai-sdk-ui/use-completion)** enables you to handle text completions in your applications, managing chat input state and automatically updating the UI as new completions are streamed from your AI provider.
14
+
-**[`useObject`](/docs/reference/ai-sdk-ui/use-object)** is a hook that allows you to consume streamed JSON objects, providing a simple way to handle and display structured data in your application.
15
+
-**[`useAssistant`](/docs/reference/ai-sdk-ui/use-assistant)** is designed to facilitate interaction with OpenAI-compatible assistant APIs, managing UI state and updating it automatically as responses are streamed.
15
16
16
17
These hooks are designed to reduce the complexity and time required to implement AI interactions, letting you focus on creating exceptional user experiences.
17
18
@@ -25,6 +26,7 @@ Here is a comparison of the supported functions across these frameworks:
<Note>`useObject` is an experimental feature and only available in React.</Note>
9
+
10
+
The [`useObject`](/docs/reference/ai-sdk-ui/use-object) hook allows you to create interfaces that represent a structured JSON object that is being streamed.
11
+
12
+
In this guide, you will learn how to use the `useObject` hook in your application to generate UIs for structured data on the fly.
13
+
14
+
## Example
15
+
16
+
The example shows a small notfications demo app that generates fake notifications in real-time.
17
+
18
+
### Schema
19
+
20
+
It is helpful to set up the schema in a separate file that is imported on both the client and server.
21
+
22
+
```ts filename='app/api/use-object/schema.ts'
23
+
import { DeepPartial } from'ai';
24
+
import { z } from'zod';
25
+
26
+
// define a schema for the notifications
27
+
exportconst notificationSchema =z.object({
28
+
notifications: z.array(
29
+
z.object({
30
+
name: z.string().describe('Name of a fictional person.'),
31
+
message: z.string().describe('Message. Do not use emojis or links.'),
32
+
minutesAgo: z.number(),
33
+
}),
34
+
),
35
+
});
36
+
37
+
// define a type for the partial notifications during generation
Copy file name to clipboardexpand all lines: content/docs/07-reference/ai-sdk-core/04-stream-object.mdx
+40-3
Original file line number
Diff line number
Diff line change
@@ -429,12 +429,19 @@ for await (const partialObject of partialObjectStream) {
429
429
name: 'partialObjectStream',
430
430
type: 'AsyncIterableStream<DeepPartial<T>>',
431
431
description:
432
-
'Note that the partial object is not validated. If you want to be certain that the actual content matches your schema, you need to implement your own validation for partial results.',
432
+
'Stream of partial objects. It gets more complete as the stream progresses. Note that the partial object is not validated. If you want to be certain that the actual content matches your schema, you need to implement your own validation for partial results.',
433
+
},
434
+
{
435
+
name: 'textStream',
436
+
type: 'AsyncIterableStream<string>',
437
+
description:
438
+
'Text stream of the JSON representation of the generated object. It contains text chunks. When the stream is finished, the object is valid JSON that can be parsed.',
433
439
},
434
440
{
435
441
name: 'fullStream',
436
442
type: 'AsyncIterableStream<ObjectStreamPart<T>>',
437
-
description: 'The full stream of the object.',
443
+
description:
444
+
'Stream of different types of events, including partial objects, errors, and finish events.',
438
445
properties: [
439
446
{
440
447
type: 'ObjectPart',
@@ -450,6 +457,20 @@ for await (const partialObject of partialObjectStream) {
450
457
},
451
458
],
452
459
},
460
+
{
461
+
type: 'TextDeltaPart',
462
+
parameters: [
463
+
{
464
+
name: 'type',
465
+
type: "'text-delta'",
466
+
},
467
+
{
468
+
name: 'textDelta',
469
+
type: 'string',
470
+
description: 'The text delta for the underlying raw JSON text.',
471
+
},
472
+
],
473
+
},
453
474
{
454
475
type: 'ErrorPart',
455
476
parameters: [
@@ -514,6 +535,18 @@ for await (const partialObject of partialObjectStream) {
514
535
description:
515
536
'Warnings from the model provider (e.g. unsupported settings).',
'Writes text delta output to a Node.js response-like object. It sets a `Content-Type` header to `text/plain; charset=utf-8` and writes each text delta as a separate chunk.',
543
+
},
544
+
{
545
+
name: 'toTextStreamResponse',
546
+
type: '(init?: ResponseInit) => Response',
547
+
description:
548
+
'Creates a simple text stream response. Each text delta is encoded as UTF-8 and sent as a separate chunk. Non-text-delta events are ignored.',
549
+
},
517
550
]}
518
551
/>
519
552
@@ -522,9 +555,13 @@ for await (const partialObject of partialObjectStream) {
Copy file name to clipboardexpand all lines: content/docs/07-reference/ai-sdk-ui/02-use-completion.mdx
+1-1
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ description: API reference for the useCompletion hook.
5
5
6
6
# `useCompletion()`
7
7
8
-
Allows you to create text completion based capibilities for your application. It enables the streaming of text completions from your AI provider, manages the state for chat input, and updates the UI automatically as new messages are received.
8
+
Allows you to create text completion based capabilities for your application. It enables the streaming of text completions from your AI provider, manages the state for chat input, and updates the UI automatically as new messages are received.
Copy file name to clipboardexpand all lines: content/docs/07-reference/ai-sdk-ui/index.mdx
+7-1
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,11 @@ AI SDK UI contains the following hooks:
24
24
'Use a hook to interact with language models in a completion interface.',
25
25
href: '/docs/reference/ai-sdk-ui/use-completion',
26
26
},
27
+
{
28
+
title: 'useObject',
29
+
description: 'Use a hook for consuming a streamed JSON objects.',
30
+
href: '/docs/reference/ai-sdk-ui/use-object',
31
+
},
27
32
{
28
33
title: 'useAssistant',
29
34
description: 'Use a hook to interact with OpenAI assistants.',
@@ -46,14 +51,15 @@ It also contains the following helper functions:
46
51
47
52
## UI Framework Support
48
53
49
-
AI SDK UI supports several frameworks: [React](https://react.dev/), [Svelte](https://svelte.dev/), [Vue.js](https://vuejs.org/), and [SolidJS](https://www.solidjs.com/).
54
+
AI SDK UI supports the following frameworks: [React](https://react.dev/), [Svelte](https://svelte.dev/), [Vue.js](https://vuejs.org/), and [SolidJS](https://www.solidjs.com/).
50
55
Here is a comparison of the supported functions across these frameworks:
0 commit comments