ai 6.0.31 → 6.0.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -2
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/docs/00-introduction/index.mdx +76 -0
- package/docs/02-foundations/01-overview.mdx +43 -0
- package/docs/02-foundations/02-providers-and-models.mdx +163 -0
- package/docs/02-foundations/03-prompts.mdx +620 -0
- package/docs/02-foundations/04-tools.mdx +160 -0
- package/docs/02-foundations/05-streaming.mdx +62 -0
- package/docs/02-foundations/index.mdx +43 -0
- package/docs/02-getting-started/00-choosing-a-provider.mdx +110 -0
- package/docs/02-getting-started/01-navigating-the-library.mdx +85 -0
- package/docs/02-getting-started/02-nextjs-app-router.mdx +556 -0
- package/docs/02-getting-started/03-nextjs-pages-router.mdx +542 -0
- package/docs/02-getting-started/04-svelte.mdx +627 -0
- package/docs/02-getting-started/05-nuxt.mdx +566 -0
- package/docs/02-getting-started/06-nodejs.mdx +512 -0
- package/docs/02-getting-started/07-expo.mdx +766 -0
- package/docs/02-getting-started/08-tanstack-start.mdx +583 -0
- package/docs/02-getting-started/index.mdx +44 -0
- package/docs/03-agents/01-overview.mdx +96 -0
- package/docs/03-agents/02-building-agents.mdx +367 -0
- package/docs/03-agents/03-workflows.mdx +370 -0
- package/docs/03-agents/04-loop-control.mdx +350 -0
- package/docs/03-agents/05-configuring-call-options.mdx +286 -0
- package/docs/03-agents/index.mdx +40 -0
- package/docs/03-ai-sdk-core/01-overview.mdx +33 -0
- package/docs/03-ai-sdk-core/05-generating-text.mdx +600 -0
- package/docs/03-ai-sdk-core/10-generating-structured-data.mdx +662 -0
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1102 -0
- package/docs/03-ai-sdk-core/16-mcp-tools.mdx +375 -0
- package/docs/03-ai-sdk-core/20-prompt-engineering.mdx +144 -0
- package/docs/03-ai-sdk-core/25-settings.mdx +198 -0
- package/docs/03-ai-sdk-core/30-embeddings.mdx +247 -0
- package/docs/03-ai-sdk-core/31-reranking.mdx +218 -0
- package/docs/03-ai-sdk-core/35-image-generation.mdx +341 -0
- package/docs/03-ai-sdk-core/36-transcription.mdx +173 -0
- package/docs/03-ai-sdk-core/37-speech.mdx +167 -0
- package/docs/03-ai-sdk-core/40-middleware.mdx +480 -0
- package/docs/03-ai-sdk-core/45-provider-management.mdx +349 -0
- package/docs/03-ai-sdk-core/50-error-handling.mdx +149 -0
- package/docs/03-ai-sdk-core/55-testing.mdx +218 -0
- package/docs/03-ai-sdk-core/60-telemetry.mdx +313 -0
- package/docs/03-ai-sdk-core/65-devtools.mdx +107 -0
- package/docs/03-ai-sdk-core/index.mdx +88 -0
- package/docs/04-ai-sdk-ui/01-overview.mdx +44 -0
- package/docs/04-ai-sdk-ui/02-chatbot.mdx +1313 -0
- package/docs/04-ai-sdk-ui/03-chatbot-message-persistence.mdx +535 -0
- package/docs/04-ai-sdk-ui/03-chatbot-resume-streams.mdx +263 -0
- package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +682 -0
- package/docs/04-ai-sdk-ui/04-generative-user-interfaces.mdx +389 -0
- package/docs/04-ai-sdk-ui/05-completion.mdx +186 -0
- package/docs/04-ai-sdk-ui/08-object-generation.mdx +344 -0
- package/docs/04-ai-sdk-ui/20-streaming-data.mdx +397 -0
- package/docs/04-ai-sdk-ui/21-error-handling.mdx +190 -0
- package/docs/04-ai-sdk-ui/21-transport.mdx +174 -0
- package/docs/04-ai-sdk-ui/24-reading-ui-message-streams.mdx +104 -0
- package/docs/04-ai-sdk-ui/25-message-metadata.mdx +152 -0
- package/docs/04-ai-sdk-ui/50-stream-protocol.mdx +477 -0
- package/docs/04-ai-sdk-ui/index.mdx +64 -0
- package/docs/05-ai-sdk-rsc/01-overview.mdx +45 -0
- package/docs/05-ai-sdk-rsc/02-streaming-react-components.mdx +209 -0
- package/docs/05-ai-sdk-rsc/03-generative-ui-state.mdx +279 -0
- package/docs/05-ai-sdk-rsc/03-saving-and-restoring-states.mdx +105 -0
- package/docs/05-ai-sdk-rsc/04-multistep-interfaces.mdx +282 -0
- package/docs/05-ai-sdk-rsc/05-streaming-values.mdx +158 -0
- package/docs/05-ai-sdk-rsc/06-loading-state.mdx +273 -0
- package/docs/05-ai-sdk-rsc/08-error-handling.mdx +96 -0
- package/docs/05-ai-sdk-rsc/09-authentication.mdx +42 -0
- package/docs/05-ai-sdk-rsc/10-migrating-to-ui.mdx +722 -0
- package/docs/05-ai-sdk-rsc/index.mdx +58 -0
- package/docs/06-advanced/01-prompt-engineering.mdx +96 -0
- package/docs/06-advanced/02-stopping-streams.mdx +184 -0
- package/docs/06-advanced/03-backpressure.mdx +173 -0
- package/docs/06-advanced/04-caching.mdx +169 -0
- package/docs/06-advanced/05-multiple-streamables.mdx +68 -0
- package/docs/06-advanced/06-rate-limiting.mdx +60 -0
- package/docs/06-advanced/07-rendering-ui-with-language-models.mdx +213 -0
- package/docs/06-advanced/08-model-as-router.mdx +120 -0
- package/docs/06-advanced/09-multistep-interfaces.mdx +115 -0
- package/docs/06-advanced/09-sequential-generations.mdx +55 -0
- package/docs/06-advanced/10-vercel-deployment-guide.mdx +117 -0
- package/docs/06-advanced/index.mdx +11 -0
- package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +2142 -0
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +3215 -0
- package/docs/07-reference/01-ai-sdk-core/03-generate-object.mdx +780 -0
- package/docs/07-reference/01-ai-sdk-core/04-stream-object.mdx +1140 -0
- package/docs/07-reference/01-ai-sdk-core/05-embed.mdx +190 -0
- package/docs/07-reference/01-ai-sdk-core/06-embed-many.mdx +171 -0
- package/docs/07-reference/01-ai-sdk-core/06-rerank.mdx +309 -0
- package/docs/07-reference/01-ai-sdk-core/10-generate-image.mdx +227 -0
- package/docs/07-reference/01-ai-sdk-core/11-transcribe.mdx +138 -0
- package/docs/07-reference/01-ai-sdk-core/12-generate-speech.mdx +214 -0
- package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +203 -0
- package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +449 -0
- package/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx +148 -0
- package/docs/07-reference/01-ai-sdk-core/18-create-agent-ui-stream-response.mdx +168 -0
- package/docs/07-reference/01-ai-sdk-core/18-pipe-agent-ui-stream-to-response.mdx +144 -0
- package/docs/07-reference/01-ai-sdk-core/20-tool.mdx +196 -0
- package/docs/07-reference/01-ai-sdk-core/22-dynamic-tool.mdx +175 -0
- package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +410 -0
- package/docs/07-reference/01-ai-sdk-core/24-mcp-stdio-transport.mdx +68 -0
- package/docs/07-reference/01-ai-sdk-core/25-json-schema.mdx +94 -0
- package/docs/07-reference/01-ai-sdk-core/26-zod-schema.mdx +109 -0
- package/docs/07-reference/01-ai-sdk-core/27-valibot-schema.mdx +55 -0
- package/docs/07-reference/01-ai-sdk-core/28-output.mdx +342 -0
- package/docs/07-reference/01-ai-sdk-core/30-model-message.mdx +415 -0
- package/docs/07-reference/01-ai-sdk-core/31-ui-message.mdx +246 -0
- package/docs/07-reference/01-ai-sdk-core/32-validate-ui-messages.mdx +101 -0
- package/docs/07-reference/01-ai-sdk-core/33-safe-validate-ui-messages.mdx +113 -0
- package/docs/07-reference/01-ai-sdk-core/40-provider-registry.mdx +182 -0
- package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +121 -0
- package/docs/07-reference/01-ai-sdk-core/50-cosine-similarity.mdx +52 -0
- package/docs/07-reference/01-ai-sdk-core/60-wrap-language-model.mdx +59 -0
- package/docs/07-reference/01-ai-sdk-core/61-wrap-image-model.mdx +64 -0
- package/docs/07-reference/01-ai-sdk-core/65-language-model-v2-middleware.mdx +46 -0
- package/docs/07-reference/01-ai-sdk-core/66-extract-reasoning-middleware.mdx +68 -0
- package/docs/07-reference/01-ai-sdk-core/67-simulate-streaming-middleware.mdx +71 -0
- package/docs/07-reference/01-ai-sdk-core/68-default-settings-middleware.mdx +80 -0
- package/docs/07-reference/01-ai-sdk-core/69-add-tool-input-examples-middleware.mdx +155 -0
- package/docs/07-reference/01-ai-sdk-core/70-extract-json-middleware.mdx +147 -0
- package/docs/07-reference/01-ai-sdk-core/70-step-count-is.mdx +84 -0
- package/docs/07-reference/01-ai-sdk-core/71-has-tool-call.mdx +120 -0
- package/docs/07-reference/01-ai-sdk-core/75-simulate-readable-stream.mdx +94 -0
- package/docs/07-reference/01-ai-sdk-core/80-smooth-stream.mdx +145 -0
- package/docs/07-reference/01-ai-sdk-core/90-generate-id.mdx +43 -0
- package/docs/07-reference/01-ai-sdk-core/91-create-id-generator.mdx +89 -0
- package/docs/07-reference/01-ai-sdk-core/index.mdx +159 -0
- package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +446 -0
- package/docs/07-reference/02-ai-sdk-ui/02-use-completion.mdx +179 -0
- package/docs/07-reference/02-ai-sdk-ui/03-use-object.mdx +178 -0
- package/docs/07-reference/02-ai-sdk-ui/31-convert-to-model-messages.mdx +230 -0
- package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +108 -0
- package/docs/07-reference/02-ai-sdk-ui/40-create-ui-message-stream.mdx +151 -0
- package/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx +113 -0
- package/docs/07-reference/02-ai-sdk-ui/42-pipe-ui-message-stream-to-response.mdx +73 -0
- package/docs/07-reference/02-ai-sdk-ui/43-read-ui-message-stream.mdx +57 -0
- package/docs/07-reference/02-ai-sdk-ui/46-infer-ui-tools.mdx +99 -0
- package/docs/07-reference/02-ai-sdk-ui/47-infer-ui-tool.mdx +75 -0
- package/docs/07-reference/02-ai-sdk-ui/50-direct-chat-transport.mdx +333 -0
- package/docs/07-reference/02-ai-sdk-ui/index.mdx +89 -0
- package/docs/07-reference/03-ai-sdk-rsc/01-stream-ui.mdx +767 -0
- package/docs/07-reference/03-ai-sdk-rsc/02-create-ai.mdx +90 -0
- package/docs/07-reference/03-ai-sdk-rsc/03-create-streamable-ui.mdx +91 -0
- package/docs/07-reference/03-ai-sdk-rsc/04-create-streamable-value.mdx +48 -0
- package/docs/07-reference/03-ai-sdk-rsc/05-read-streamable-value.mdx +78 -0
- package/docs/07-reference/03-ai-sdk-rsc/06-get-ai-state.mdx +50 -0
- package/docs/07-reference/03-ai-sdk-rsc/07-get-mutable-ai-state.mdx +70 -0
- package/docs/07-reference/03-ai-sdk-rsc/08-use-ai-state.mdx +26 -0
- package/docs/07-reference/03-ai-sdk-rsc/09-use-actions.mdx +42 -0
- package/docs/07-reference/03-ai-sdk-rsc/10-use-ui-state.mdx +35 -0
- package/docs/07-reference/03-ai-sdk-rsc/11-use-streamable-value.mdx +46 -0
- package/docs/07-reference/03-ai-sdk-rsc/20-render.mdx +262 -0
- package/docs/07-reference/03-ai-sdk-rsc/index.mdx +67 -0
- package/docs/07-reference/04-stream-helpers/01-ai-stream.mdx +89 -0
- package/docs/07-reference/04-stream-helpers/02-streaming-text-response.mdx +79 -0
- package/docs/07-reference/04-stream-helpers/05-stream-to-response.mdx +108 -0
- package/docs/07-reference/04-stream-helpers/07-openai-stream.mdx +77 -0
- package/docs/07-reference/04-stream-helpers/08-anthropic-stream.mdx +79 -0
- package/docs/07-reference/04-stream-helpers/09-aws-bedrock-stream.mdx +91 -0
- package/docs/07-reference/04-stream-helpers/10-aws-bedrock-anthropic-stream.mdx +96 -0
- package/docs/07-reference/04-stream-helpers/10-aws-bedrock-messages-stream.mdx +96 -0
- package/docs/07-reference/04-stream-helpers/11-aws-bedrock-cohere-stream.mdx +93 -0
- package/docs/07-reference/04-stream-helpers/12-aws-bedrock-llama-2-stream.mdx +93 -0
- package/docs/07-reference/04-stream-helpers/13-cohere-stream.mdx +78 -0
- package/docs/07-reference/04-stream-helpers/14-google-generative-ai-stream.mdx +85 -0
- package/docs/07-reference/04-stream-helpers/15-hugging-face-stream.mdx +84 -0
- package/docs/07-reference/04-stream-helpers/16-langchain-adapter.mdx +98 -0
- package/docs/07-reference/04-stream-helpers/16-llamaindex-adapter.mdx +70 -0
- package/docs/07-reference/04-stream-helpers/17-mistral-stream.mdx +81 -0
- package/docs/07-reference/04-stream-helpers/18-replicate-stream.mdx +83 -0
- package/docs/07-reference/04-stream-helpers/19-inkeep-stream.mdx +80 -0
- package/docs/07-reference/04-stream-helpers/index.mdx +103 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-api-call-error.mdx +30 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-download-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-empty-response-body-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-argument-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-message-role-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-prompt-error.mdx +47 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-response-data-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-approval-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-input-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-json-parse-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-load-api-key-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-load-setting-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-message-conversion-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-content-generated-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-image-generated-error.mdx +36 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-object-generated-error.mdx +43 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-speech-generated-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-model-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-provider-error.mdx +28 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-tool-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-transcript-generated-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-retry-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-too-many-embedding-values-for-call-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-not-found-for-approval-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-repair-error.mdx +28 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-type-validation-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-unsupported-functionality-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/index.mdx +38 -0
- package/docs/07-reference/index.mdx +34 -0
- package/docs/08-migration-guides/00-versioning.mdx +46 -0
- package/docs/08-migration-guides/24-migration-guide-6-0.mdx +823 -0
- package/docs/08-migration-guides/25-migration-guide-5-0-data.mdx +882 -0
- package/docs/08-migration-guides/26-migration-guide-5-0.mdx +3427 -0
- package/docs/08-migration-guides/27-migration-guide-4-2.mdx +99 -0
- package/docs/08-migration-guides/28-migration-guide-4-1.mdx +14 -0
- package/docs/08-migration-guides/29-migration-guide-4-0.mdx +1157 -0
- package/docs/08-migration-guides/36-migration-guide-3-4.mdx +14 -0
- package/docs/08-migration-guides/37-migration-guide-3-3.mdx +64 -0
- package/docs/08-migration-guides/38-migration-guide-3-2.mdx +46 -0
- package/docs/08-migration-guides/39-migration-guide-3-1.mdx +168 -0
- package/docs/08-migration-guides/index.mdx +22 -0
- package/docs/09-troubleshooting/01-azure-stream-slow.mdx +33 -0
- package/docs/09-troubleshooting/02-client-side-function-calls-not-invoked.mdx +22 -0
- package/docs/09-troubleshooting/03-server-actions-in-client-components.mdx +40 -0
- package/docs/09-troubleshooting/04-strange-stream-output.mdx +36 -0
- package/docs/09-troubleshooting/05-streamable-ui-errors.mdx +16 -0
- package/docs/09-troubleshooting/05-tool-invocation-missing-result.mdx +106 -0
- package/docs/09-troubleshooting/06-streaming-not-working-when-deployed.mdx +31 -0
- package/docs/09-troubleshooting/06-streaming-not-working-when-proxied.mdx +31 -0
- package/docs/09-troubleshooting/06-timeout-on-vercel.mdx +60 -0
- package/docs/09-troubleshooting/07-unclosed-streams.mdx +34 -0
- package/docs/09-troubleshooting/08-use-chat-failed-to-parse-stream.mdx +26 -0
- package/docs/09-troubleshooting/09-client-stream-error.mdx +25 -0
- package/docs/09-troubleshooting/10-use-chat-tools-no-response.mdx +32 -0
- package/docs/09-troubleshooting/11-use-chat-custom-request-options.mdx +149 -0
- package/docs/09-troubleshooting/12-typescript-performance-zod.mdx +46 -0
- package/docs/09-troubleshooting/12-use-chat-an-error-occurred.mdx +59 -0
- package/docs/09-troubleshooting/13-repeated-assistant-messages.mdx +73 -0
- package/docs/09-troubleshooting/14-stream-abort-handling.mdx +73 -0
- package/docs/09-troubleshooting/14-tool-calling-with-structured-outputs.mdx +48 -0
- package/docs/09-troubleshooting/15-abort-breaks-resumable-streams.mdx +55 -0
- package/docs/09-troubleshooting/15-stream-text-not-working.mdx +33 -0
- package/docs/09-troubleshooting/16-streaming-status-delay.mdx +63 -0
- package/docs/09-troubleshooting/17-use-chat-stale-body-data.mdx +141 -0
- package/docs/09-troubleshooting/18-ontoolcall-type-narrowing.mdx +66 -0
- package/docs/09-troubleshooting/19-unsupported-model-version.mdx +50 -0
- package/docs/09-troubleshooting/20-no-object-generated-content-filter.mdx +72 -0
- package/docs/09-troubleshooting/30-model-is-not-assignable-to-type.mdx +21 -0
- package/docs/09-troubleshooting/40-typescript-cannot-find-namespace-jsx.mdx +24 -0
- package/docs/09-troubleshooting/50-react-maximum-update-depth-exceeded.mdx +39 -0
- package/docs/09-troubleshooting/60-jest-cannot-find-module-ai-rsc.mdx +22 -0
- package/docs/09-troubleshooting/index.mdx +11 -0
- package/package.json +8 -4
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Object Generation
|
|
3
|
+
description: Learn how to use the useObject hook.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Object Generation
|
|
7
|
+
|
|
8
|
+
<Note>
|
|
9
|
+
`useObject` is an experimental feature and only available in React, Svelte,
|
|
10
|
+
and Vue.
|
|
11
|
+
</Note>
|
|
12
|
+
|
|
13
|
+
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.
|
|
14
|
+
|
|
15
|
+
In this guide, you will learn how to use the `useObject` hook in your application to generate UIs for structured data on the fly.
|
|
16
|
+
|
|
17
|
+
## Example
|
|
18
|
+
|
|
19
|
+
The example shows a small notifications demo app that generates fake notifications in real-time.
|
|
20
|
+
|
|
21
|
+
### Schema
|
|
22
|
+
|
|
23
|
+
It is helpful to set up the schema in a separate file that is imported on both the client and server.
|
|
24
|
+
|
|
25
|
+
```ts filename='app/api/notifications/schema.ts'
|
|
26
|
+
import { z } from 'zod';
|
|
27
|
+
|
|
28
|
+
// define a schema for the notifications
|
|
29
|
+
export const notificationSchema = z.object({
|
|
30
|
+
notifications: z.array(
|
|
31
|
+
z.object({
|
|
32
|
+
name: z.string().describe('Name of a fictional person.'),
|
|
33
|
+
message: z.string().describe('Message. Do not use emojis or links.'),
|
|
34
|
+
}),
|
|
35
|
+
),
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Client
|
|
40
|
+
|
|
41
|
+
The client uses [`useObject`](/docs/reference/ai-sdk-ui/use-object) to stream the object generation process.
|
|
42
|
+
|
|
43
|
+
The results are partial and are displayed as they are received.
|
|
44
|
+
Please note the code for handling `undefined` values in the JSX.
|
|
45
|
+
|
|
46
|
+
```tsx filename='app/page.tsx'
|
|
47
|
+
'use client';
|
|
48
|
+
|
|
49
|
+
import { experimental_useObject as useObject } from '@ai-sdk/react';
|
|
50
|
+
import { notificationSchema } from './api/notifications/schema';
|
|
51
|
+
|
|
52
|
+
export default function Page() {
|
|
53
|
+
const { object, submit } = useObject({
|
|
54
|
+
api: '/api/notifications',
|
|
55
|
+
schema: notificationSchema,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<>
|
|
60
|
+
<button onClick={() => submit('Messages during finals week.')}>
|
|
61
|
+
Generate notifications
|
|
62
|
+
</button>
|
|
63
|
+
|
|
64
|
+
{object?.notifications?.map((notification, index) => (
|
|
65
|
+
<div key={index}>
|
|
66
|
+
<p>{notification?.name}</p>
|
|
67
|
+
<p>{notification?.message}</p>
|
|
68
|
+
</div>
|
|
69
|
+
))}
|
|
70
|
+
</>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Server
|
|
76
|
+
|
|
77
|
+
On the server, we use [`streamText`](/docs/reference/ai-sdk-core/stream-text) with [`Output.object()`](/docs/reference/ai-sdk-core/output#output-object) to stream the object generation process.
|
|
78
|
+
|
|
79
|
+
```typescript filename='app/api/notifications/route.ts'
|
|
80
|
+
import { streamText, Output } from 'ai';
|
|
81
|
+
__PROVIDER_IMPORT__;
|
|
82
|
+
import { notificationSchema } from './schema';
|
|
83
|
+
|
|
84
|
+
// Allow streaming responses up to 30 seconds
|
|
85
|
+
export const maxDuration = 30;
|
|
86
|
+
|
|
87
|
+
export async function POST(req: Request) {
|
|
88
|
+
const context = await req.json();
|
|
89
|
+
|
|
90
|
+
const result = streamText({
|
|
91
|
+
model: __MODEL__,
|
|
92
|
+
output: Output.object({ schema: notificationSchema }),
|
|
93
|
+
prompt:
|
|
94
|
+
`Generate 3 notifications for a messages app in this context:` + context,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return result.toTextStreamResponse();
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Enum Output Mode
|
|
102
|
+
|
|
103
|
+
When you need to classify or categorize input into predefined options, you can use the `enum` output mode with `useObject`. This requires a specific schema structure where the object has `enum` as a key with `z.enum` containing your possible values.
|
|
104
|
+
|
|
105
|
+
### Example: Text Classification
|
|
106
|
+
|
|
107
|
+
This example shows how to build a simple text classifier that categorizes statements as true or false.
|
|
108
|
+
|
|
109
|
+
#### Client
|
|
110
|
+
|
|
111
|
+
When using `useObject` with enum output mode, your schema must be an object with `enum` as the key:
|
|
112
|
+
|
|
113
|
+
```tsx filename='app/classify/page.tsx'
|
|
114
|
+
'use client';
|
|
115
|
+
|
|
116
|
+
import { experimental_useObject as useObject } from '@ai-sdk/react';
|
|
117
|
+
import { z } from 'zod';
|
|
118
|
+
|
|
119
|
+
export default function ClassifyPage() {
|
|
120
|
+
const { object, submit, isLoading } = useObject({
|
|
121
|
+
api: '/api/classify',
|
|
122
|
+
schema: z.object({ enum: z.enum(['true', 'false']) }),
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
return (
|
|
126
|
+
<>
|
|
127
|
+
<button onClick={() => submit('The earth is flat')} disabled={isLoading}>
|
|
128
|
+
Classify statement
|
|
129
|
+
</button>
|
|
130
|
+
|
|
131
|
+
{object && <div>Classification: {object.enum}</div>}
|
|
132
|
+
</>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### Server
|
|
138
|
+
|
|
139
|
+
On the server, use `streamText` with `Output.choice()` to stream the classification result:
|
|
140
|
+
|
|
141
|
+
```typescript filename='app/api/classify/route.ts'
|
|
142
|
+
import { streamText, Output } from 'ai';
|
|
143
|
+
__PROVIDER_IMPORT__;
|
|
144
|
+
|
|
145
|
+
export async function POST(req: Request) {
|
|
146
|
+
const context = await req.json();
|
|
147
|
+
|
|
148
|
+
const result = streamText({
|
|
149
|
+
model: __MODEL__,
|
|
150
|
+
output: Output.choice({ options: ['true', 'false'] }),
|
|
151
|
+
prompt: `Classify this statement as true or false: ${context}`,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
return result.toTextStreamResponse();
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Customized UI
|
|
159
|
+
|
|
160
|
+
`useObject` also provides ways to show loading and error states:
|
|
161
|
+
|
|
162
|
+
### Loading State
|
|
163
|
+
|
|
164
|
+
The `isLoading` state returned by the `useObject` hook can be used for several
|
|
165
|
+
purposes:
|
|
166
|
+
|
|
167
|
+
- To show a loading spinner while the object is generated.
|
|
168
|
+
- To disable the submit button.
|
|
169
|
+
|
|
170
|
+
```tsx filename='app/page.tsx' highlight="6,13-20,24"
|
|
171
|
+
'use client';
|
|
172
|
+
|
|
173
|
+
import { useObject } from '@ai-sdk/react';
|
|
174
|
+
|
|
175
|
+
export default function Page() {
|
|
176
|
+
const { isLoading, object, submit } = useObject({
|
|
177
|
+
api: '/api/notifications',
|
|
178
|
+
schema: notificationSchema,
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
return (
|
|
182
|
+
<>
|
|
183
|
+
{isLoading && <Spinner />}
|
|
184
|
+
|
|
185
|
+
<button
|
|
186
|
+
onClick={() => submit('Messages during finals week.')}
|
|
187
|
+
disabled={isLoading}
|
|
188
|
+
>
|
|
189
|
+
Generate notifications
|
|
190
|
+
</button>
|
|
191
|
+
|
|
192
|
+
{object?.notifications?.map((notification, index) => (
|
|
193
|
+
<div key={index}>
|
|
194
|
+
<p>{notification?.name}</p>
|
|
195
|
+
<p>{notification?.message}</p>
|
|
196
|
+
</div>
|
|
197
|
+
))}
|
|
198
|
+
</>
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Stop Handler
|
|
204
|
+
|
|
205
|
+
The `stop` function can be used to stop the object generation process. This can be useful if the user wants to cancel the request or if the server is taking too long to respond.
|
|
206
|
+
|
|
207
|
+
```tsx filename='app/page.tsx' highlight="6,14-16"
|
|
208
|
+
'use client';
|
|
209
|
+
|
|
210
|
+
import { useObject } from '@ai-sdk/react';
|
|
211
|
+
|
|
212
|
+
export default function Page() {
|
|
213
|
+
const { isLoading, stop, object, submit } = useObject({
|
|
214
|
+
api: '/api/notifications',
|
|
215
|
+
schema: notificationSchema,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
return (
|
|
219
|
+
<>
|
|
220
|
+
{isLoading && (
|
|
221
|
+
<button type="button" onClick={() => stop()}>
|
|
222
|
+
Stop
|
|
223
|
+
</button>
|
|
224
|
+
)}
|
|
225
|
+
|
|
226
|
+
<button onClick={() => submit('Messages during finals week.')}>
|
|
227
|
+
Generate notifications
|
|
228
|
+
</button>
|
|
229
|
+
|
|
230
|
+
{object?.notifications?.map((notification, index) => (
|
|
231
|
+
<div key={index}>
|
|
232
|
+
<p>{notification?.name}</p>
|
|
233
|
+
<p>{notification?.message}</p>
|
|
234
|
+
</div>
|
|
235
|
+
))}
|
|
236
|
+
</>
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Error State
|
|
242
|
+
|
|
243
|
+
Similarly, the `error` state reflects the error object thrown during the fetch request.
|
|
244
|
+
It can be used to display an error message, or to disable the submit button:
|
|
245
|
+
|
|
246
|
+
<Note>
|
|
247
|
+
We recommend showing a generic error message to the user, such as "Something
|
|
248
|
+
went wrong." This is a good practice to avoid leaking information from the
|
|
249
|
+
server.
|
|
250
|
+
</Note>
|
|
251
|
+
|
|
252
|
+
```tsx file="app/page.tsx" highlight="6,13"
|
|
253
|
+
'use client';
|
|
254
|
+
|
|
255
|
+
import { useObject } from '@ai-sdk/react';
|
|
256
|
+
|
|
257
|
+
export default function Page() {
|
|
258
|
+
const { error, object, submit } = useObject({
|
|
259
|
+
api: '/api/notifications',
|
|
260
|
+
schema: notificationSchema,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
return (
|
|
264
|
+
<>
|
|
265
|
+
{error && <div>An error occurred.</div>}
|
|
266
|
+
|
|
267
|
+
<button onClick={() => submit('Messages during finals week.')}>
|
|
268
|
+
Generate notifications
|
|
269
|
+
</button>
|
|
270
|
+
|
|
271
|
+
{object?.notifications?.map((notification, index) => (
|
|
272
|
+
<div key={index}>
|
|
273
|
+
<p>{notification?.name}</p>
|
|
274
|
+
<p>{notification?.message}</p>
|
|
275
|
+
</div>
|
|
276
|
+
))}
|
|
277
|
+
</>
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
## Event Callbacks
|
|
283
|
+
|
|
284
|
+
`useObject` provides optional event callbacks that you can use to handle life-cycle events.
|
|
285
|
+
|
|
286
|
+
- `onFinish`: Called when the object generation is completed.
|
|
287
|
+
- `onError`: Called when an error occurs during the fetch request.
|
|
288
|
+
|
|
289
|
+
These callbacks can be used to trigger additional actions, such as logging, analytics, or custom UI updates.
|
|
290
|
+
|
|
291
|
+
```tsx filename='app/page.tsx' highlight="10-20"
|
|
292
|
+
'use client';
|
|
293
|
+
|
|
294
|
+
import { experimental_useObject as useObject } from '@ai-sdk/react';
|
|
295
|
+
import { notificationSchema } from './api/notifications/schema';
|
|
296
|
+
|
|
297
|
+
export default function Page() {
|
|
298
|
+
const { object, submit } = useObject({
|
|
299
|
+
api: '/api/notifications',
|
|
300
|
+
schema: notificationSchema,
|
|
301
|
+
onFinish({ object, error }) {
|
|
302
|
+
// typed object, undefined if schema validation fails:
|
|
303
|
+
console.log('Object generation completed:', object);
|
|
304
|
+
|
|
305
|
+
// error, undefined if schema validation succeeds:
|
|
306
|
+
console.log('Schema validation error:', error);
|
|
307
|
+
},
|
|
308
|
+
onError(error) {
|
|
309
|
+
// error during fetch request:
|
|
310
|
+
console.error('An error occurred:', error);
|
|
311
|
+
},
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
return (
|
|
315
|
+
<div>
|
|
316
|
+
<button onClick={() => submit('Messages during finals week.')}>
|
|
317
|
+
Generate notifications
|
|
318
|
+
</button>
|
|
319
|
+
|
|
320
|
+
{object?.notifications?.map((notification, index) => (
|
|
321
|
+
<div key={index}>
|
|
322
|
+
<p>{notification?.name}</p>
|
|
323
|
+
<p>{notification?.message}</p>
|
|
324
|
+
</div>
|
|
325
|
+
))}
|
|
326
|
+
</div>
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
## Configure Request Options
|
|
332
|
+
|
|
333
|
+
You can configure the API endpoint, optional headers and credentials using the `api`, `headers` and `credentials` settings.
|
|
334
|
+
|
|
335
|
+
```tsx highlight="2-5"
|
|
336
|
+
const { submit, object } = useObject({
|
|
337
|
+
api: '/api/use-object',
|
|
338
|
+
headers: {
|
|
339
|
+
'X-Custom-Header': 'CustomValue',
|
|
340
|
+
},
|
|
341
|
+
credentials: 'include',
|
|
342
|
+
schema: yourSchema,
|
|
343
|
+
});
|
|
344
|
+
```
|