ai 6.0.30 → 6.0.32
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 +13 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +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 +7 -3
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Generating Structured Data
|
|
3
|
+
description: Learn how to generate structured data with the AI SDK.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Generating Structured Data
|
|
7
|
+
|
|
8
|
+
While text generation can be useful, your use case will likely call for generating structured data.
|
|
9
|
+
For example, you might want to extract information from text, classify data, or generate synthetic data.
|
|
10
|
+
|
|
11
|
+
Many language models are capable of generating structured data, often defined as using "JSON modes" or "tools".
|
|
12
|
+
However, you need to manually provide schemas and then validate the generated data as LLMs can produce incorrect or incomplete structured data.
|
|
13
|
+
|
|
14
|
+
The AI SDK standardises structured object generation across model providers
|
|
15
|
+
using the `output` property on [`generateText`](/docs/reference/ai-sdk-core/generate-text)
|
|
16
|
+
and [`streamText`](/docs/reference/ai-sdk-core/stream-text).
|
|
17
|
+
You can use [Zod schemas](/docs/reference/ai-sdk-core/zod-schema), [Valibot](/docs/reference/ai-sdk-core/valibot-schema), or [JSON schemas](/docs/reference/ai-sdk-core/json-schema) to specify the shape of the data that you want,
|
|
18
|
+
and the AI model will generate data that conforms to that structure.
|
|
19
|
+
|
|
20
|
+
<Note>
|
|
21
|
+
Structured output generation is part of the `generateText` and `streamText`
|
|
22
|
+
flow. This means you can combine it with tool calling in the same request.
|
|
23
|
+
</Note>
|
|
24
|
+
|
|
25
|
+
## Generating Structured Outputs
|
|
26
|
+
|
|
27
|
+
Use `generateText` with `Output.object()` to generate structured data from a prompt.
|
|
28
|
+
The schema is also used to validate the generated data, ensuring type safety and correctness.
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { generateText, Output } from 'ai';
|
|
32
|
+
__PROVIDER_IMPORT__;
|
|
33
|
+
import { z } from 'zod';
|
|
34
|
+
|
|
35
|
+
const { output } = await generateText({
|
|
36
|
+
model: __MODEL__,
|
|
37
|
+
output: Output.object({
|
|
38
|
+
schema: z.object({
|
|
39
|
+
recipe: z.object({
|
|
40
|
+
name: z.string(),
|
|
41
|
+
ingredients: z.array(
|
|
42
|
+
z.object({ name: z.string(), amount: z.string() }),
|
|
43
|
+
),
|
|
44
|
+
steps: z.array(z.string()),
|
|
45
|
+
}),
|
|
46
|
+
}),
|
|
47
|
+
}),
|
|
48
|
+
prompt: 'Generate a lasagna recipe.',
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
<Note>
|
|
53
|
+
Structured output generation counts as a step in the AI SDK's multi-turn
|
|
54
|
+
execution model (where each model call or tool execution is one step). When
|
|
55
|
+
combining with tools, account for this in your `stopWhen` configuration.
|
|
56
|
+
</Note>
|
|
57
|
+
|
|
58
|
+
### Accessing response headers & body
|
|
59
|
+
|
|
60
|
+
Sometimes you need access to the full response from the model provider,
|
|
61
|
+
e.g. to access some provider-specific headers or body content.
|
|
62
|
+
|
|
63
|
+
You can access the raw response headers and body using the `response` property:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { generateText, Output } from 'ai';
|
|
67
|
+
|
|
68
|
+
const result = await generateText({
|
|
69
|
+
// ...
|
|
70
|
+
output: Output.object({ schema }),
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
console.log(JSON.stringify(result.response.headers, null, 2));
|
|
74
|
+
console.log(JSON.stringify(result.response.body, null, 2));
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Stream Structured Outputs
|
|
78
|
+
|
|
79
|
+
Given the added complexity of returning structured data, model response time can be unacceptable for your interactive use case.
|
|
80
|
+
With `streamText` and `output`, you can stream the model's structured response as it is generated.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { streamText, Output } from 'ai';
|
|
84
|
+
__PROVIDER_IMPORT__;
|
|
85
|
+
import { z } from 'zod';
|
|
86
|
+
|
|
87
|
+
const { partialOutputStream } = streamText({
|
|
88
|
+
model: __MODEL__,
|
|
89
|
+
output: Output.object({
|
|
90
|
+
schema: z.object({
|
|
91
|
+
recipe: z.object({
|
|
92
|
+
name: z.string(),
|
|
93
|
+
ingredients: z.array(
|
|
94
|
+
z.object({ name: z.string(), amount: z.string() }),
|
|
95
|
+
),
|
|
96
|
+
steps: z.array(z.string()),
|
|
97
|
+
}),
|
|
98
|
+
}),
|
|
99
|
+
}),
|
|
100
|
+
prompt: 'Generate a lasagna recipe.',
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// use partialOutputStream as an async iterable
|
|
104
|
+
for await (const partialObject of partialOutputStream) {
|
|
105
|
+
console.log(partialObject);
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
You can consume the structured output on the client with the [`useObject`](/docs/reference/ai-sdk-ui/use-object) hook.
|
|
110
|
+
|
|
111
|
+
### Error Handling in Streams
|
|
112
|
+
|
|
113
|
+
`streamText` starts streaming immediately. When errors occur during streaming, they become part of the stream rather than thrown exceptions (to prevent stream crashes).
|
|
114
|
+
|
|
115
|
+
To handle errors, provide an `onError` callback:
|
|
116
|
+
|
|
117
|
+
```tsx highlight="5-7"
|
|
118
|
+
import { streamText, Output } from 'ai';
|
|
119
|
+
|
|
120
|
+
const result = streamText({
|
|
121
|
+
// ...
|
|
122
|
+
output: Output.object({ schema }),
|
|
123
|
+
onError({ error }) {
|
|
124
|
+
console.error(error); // log to your error tracking service
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
For non-streaming error handling with `generateText`, see the [Error Handling](#error-handling) section below.
|
|
130
|
+
|
|
131
|
+
## Output Types
|
|
132
|
+
|
|
133
|
+
The AI SDK supports multiple ways of specifying the expected structure of generated data via the `Output` object. You can select from various strategies for structured/text generation and validation.
|
|
134
|
+
|
|
135
|
+
### `Output.text()`
|
|
136
|
+
|
|
137
|
+
Use `Output.text()` to generate plain text from a model. This option doesn't enforce any schema on the result: you simply receive the model's text as a string. This is the default behavior when no `output` is specified.
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
import { generateText, Output } from 'ai';
|
|
141
|
+
|
|
142
|
+
const { output } = await generateText({
|
|
143
|
+
// ...
|
|
144
|
+
output: Output.text(),
|
|
145
|
+
prompt: 'Tell me a joke.',
|
|
146
|
+
});
|
|
147
|
+
// output will be a string (the joke)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### `Output.object()`
|
|
151
|
+
|
|
152
|
+
Use `Output.object({ schema })` to generate a structured object based on a schema (for example, a Zod schema). The output is type-validated to ensure the returned result matches the schema.
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
import { generateText, Output } from 'ai';
|
|
156
|
+
import { z } from 'zod';
|
|
157
|
+
|
|
158
|
+
const { output } = await generateText({
|
|
159
|
+
// ...
|
|
160
|
+
output: Output.object({
|
|
161
|
+
schema: z.object({
|
|
162
|
+
name: z.string(),
|
|
163
|
+
age: z.number().nullable(),
|
|
164
|
+
labels: z.array(z.string()),
|
|
165
|
+
}),
|
|
166
|
+
}),
|
|
167
|
+
prompt: 'Generate information for a test user.',
|
|
168
|
+
});
|
|
169
|
+
// output will be an object matching the schema above
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
<Note>
|
|
173
|
+
Partial outputs streamed via `streamText` cannot be validated against your
|
|
174
|
+
provided schema, as incomplete data may not yet conform to the expected
|
|
175
|
+
structure.
|
|
176
|
+
</Note>
|
|
177
|
+
|
|
178
|
+
### `Output.array()`
|
|
179
|
+
|
|
180
|
+
Use `Output.array({ element })` to specify that you expect an array of typed objects from the model, where each element should conform to a schema (defined in the `element` property).
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
import { generateText, Output } from 'ai';
|
|
184
|
+
import { z } from 'zod';
|
|
185
|
+
|
|
186
|
+
const { output } = await generateText({
|
|
187
|
+
// ...
|
|
188
|
+
output: Output.array({
|
|
189
|
+
element: z.object({
|
|
190
|
+
location: z.string(),
|
|
191
|
+
temperature: z.number(),
|
|
192
|
+
condition: z.string(),
|
|
193
|
+
}),
|
|
194
|
+
}),
|
|
195
|
+
prompt: 'List the weather for San Francisco and Paris.',
|
|
196
|
+
});
|
|
197
|
+
// output will be an array of objects like:
|
|
198
|
+
// [
|
|
199
|
+
// { location: 'San Francisco', temperature: 70, condition: 'Sunny' },
|
|
200
|
+
// { location: 'Paris', temperature: 65, condition: 'Cloudy' },
|
|
201
|
+
// ]
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
When streaming arrays with `streamText`, you can use `elementStream` to receive each completed element as it is generated:
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
import { streamText, Output } from 'ai';
|
|
208
|
+
import { z } from 'zod';
|
|
209
|
+
|
|
210
|
+
const { elementStream } = streamText({
|
|
211
|
+
// ...
|
|
212
|
+
output: Output.array({
|
|
213
|
+
element: z.object({
|
|
214
|
+
name: z.string(),
|
|
215
|
+
class: z.string(),
|
|
216
|
+
description: z.string(),
|
|
217
|
+
}),
|
|
218
|
+
}),
|
|
219
|
+
prompt: 'Generate 3 hero descriptions for a fantasy role playing game.',
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
for await (const hero of elementStream) {
|
|
223
|
+
console.log(hero); // Each hero is complete and validated
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
<Note>
|
|
228
|
+
Each element emitted by `elementStream` is complete and validated against your
|
|
229
|
+
element schema. This differs from `partialOutputStream`, which streams the
|
|
230
|
+
entire partial array including incomplete elements.
|
|
231
|
+
</Note>
|
|
232
|
+
|
|
233
|
+
### `Output.choice()`
|
|
234
|
+
|
|
235
|
+
Use `Output.choice({ options })` when you expect the model to choose from a specific set of string options, such as for classification or fixed-enum answers.
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
import { generateText, Output } from 'ai';
|
|
239
|
+
|
|
240
|
+
const { output } = await generateText({
|
|
241
|
+
// ...
|
|
242
|
+
output: Output.choice({
|
|
243
|
+
options: ['sunny', 'rainy', 'snowy'],
|
|
244
|
+
}),
|
|
245
|
+
prompt: 'Is the weather sunny, rainy, or snowy today?',
|
|
246
|
+
});
|
|
247
|
+
// output will be one of: 'sunny', 'rainy', or 'snowy'
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
You can provide any set of string options, and the output will always be a single string value that matches one of the specified options. The AI SDK validates that the result matches one of your options, and will throw if the model returns something invalid.
|
|
251
|
+
|
|
252
|
+
This is especially useful for making classification-style generations or forcing valid values for API compatibility.
|
|
253
|
+
|
|
254
|
+
### `Output.json()`
|
|
255
|
+
|
|
256
|
+
Use `Output.json()` when you want to generate and parse unstructured JSON values from the model, without enforcing a specific schema. This is useful if you want to capture arbitrary objects, flexible structures, or when you want to rely on the model's natural output rather than rigid validation.
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
import { generateText, Output } from 'ai';
|
|
260
|
+
|
|
261
|
+
const { output } = await generateText({
|
|
262
|
+
// ...
|
|
263
|
+
output: Output.json(),
|
|
264
|
+
prompt:
|
|
265
|
+
'For each city, return the current temperature and weather condition as a JSON object.',
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// output could be any valid JSON, for example:
|
|
269
|
+
// {
|
|
270
|
+
// "San Francisco": { "temperature": 70, "condition": "Sunny" },
|
|
271
|
+
// "Paris": { "temperature": 65, "condition": "Cloudy" }
|
|
272
|
+
// }
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
With `Output.json`, the AI SDK only checks that the response is valid JSON; it doesn't validate the structure or types of the values. If you need schema validation, use the `.object` or `.array` outputs instead.
|
|
276
|
+
|
|
277
|
+
For more advanced validation or different structures, see [the Output API reference](/docs/reference/ai-sdk-core/output).
|
|
278
|
+
|
|
279
|
+
## Generating Structured Outputs with Tools
|
|
280
|
+
|
|
281
|
+
One of the key advantages of using structured output with `generateText` and `streamText` is the ability to combine it with tool calling.
|
|
282
|
+
|
|
283
|
+
```ts
|
|
284
|
+
import { generateText, Output, tool, stepCountIs } from 'ai';
|
|
285
|
+
__PROVIDER_IMPORT__;
|
|
286
|
+
import { z } from 'zod';
|
|
287
|
+
|
|
288
|
+
const { output } = await generateText({
|
|
289
|
+
model: __MODEL__,
|
|
290
|
+
tools: {
|
|
291
|
+
weather: tool({
|
|
292
|
+
description: 'Get the weather for a location',
|
|
293
|
+
inputSchema: z.object({ location: z.string() }),
|
|
294
|
+
execute: async ({ location }) => {
|
|
295
|
+
// fetch weather data
|
|
296
|
+
return { temperature: 72, condition: 'sunny' };
|
|
297
|
+
},
|
|
298
|
+
}),
|
|
299
|
+
},
|
|
300
|
+
output: Output.object({
|
|
301
|
+
schema: z.object({
|
|
302
|
+
summary: z.string(),
|
|
303
|
+
recommendation: z.string(),
|
|
304
|
+
}),
|
|
305
|
+
}),
|
|
306
|
+
stopWhen: stepCountIs(5),
|
|
307
|
+
prompt: 'What should I wear in San Francisco today?',
|
|
308
|
+
});
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
<Note>
|
|
312
|
+
When using tools with structured output, remember that generating the
|
|
313
|
+
structured output counts as a step. Configure `stopWhen` to allow enough steps
|
|
314
|
+
for both tool execution and output generation.
|
|
315
|
+
</Note>
|
|
316
|
+
|
|
317
|
+
## Property Descriptions
|
|
318
|
+
|
|
319
|
+
You can add `.describe("...")` to individual schema properties to give the model hints about what each property is for. This helps improve the quality and accuracy of generated structured data:
|
|
320
|
+
|
|
321
|
+
```ts highlight="5,9"
|
|
322
|
+
import { generateText, Output } from 'ai';
|
|
323
|
+
__PROVIDER_IMPORT__;
|
|
324
|
+
import { z } from 'zod';
|
|
325
|
+
|
|
326
|
+
const { output } = await generateText({
|
|
327
|
+
model: __MODEL__,
|
|
328
|
+
output: Output.object({
|
|
329
|
+
schema: z.object({
|
|
330
|
+
name: z.string().describe('The name of the recipe'),
|
|
331
|
+
ingredients: z
|
|
332
|
+
.array(
|
|
333
|
+
z.object({
|
|
334
|
+
name: z.string(),
|
|
335
|
+
amount: z
|
|
336
|
+
.string()
|
|
337
|
+
.describe('The amount of the ingredient (grams or ml)'),
|
|
338
|
+
}),
|
|
339
|
+
)
|
|
340
|
+
.describe('List of ingredients with amounts'),
|
|
341
|
+
steps: z.array(z.string()).describe('Step-by-step cooking instructions'),
|
|
342
|
+
}),
|
|
343
|
+
}),
|
|
344
|
+
prompt: 'Generate a lasagna recipe.',
|
|
345
|
+
});
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
Property descriptions are particularly useful for:
|
|
349
|
+
|
|
350
|
+
- Clarifying ambiguous property names
|
|
351
|
+
- Specifying expected formats or conventions
|
|
352
|
+
- Providing context for complex nested structures
|
|
353
|
+
|
|
354
|
+
## Output Name and Description
|
|
355
|
+
|
|
356
|
+
You can optionally specify a `name` and `description` for the output. These are used by some providers for additional LLM guidance, e.g. via tool or schema name.
|
|
357
|
+
|
|
358
|
+
```ts highlight="6-7"
|
|
359
|
+
import { generateText, Output } from 'ai';
|
|
360
|
+
__PROVIDER_IMPORT__;
|
|
361
|
+
import { z } from 'zod';
|
|
362
|
+
|
|
363
|
+
const { output } = await generateText({
|
|
364
|
+
model: __MODEL__,
|
|
365
|
+
output: Output.object({
|
|
366
|
+
name: 'Recipe',
|
|
367
|
+
description: 'A recipe for a dish.',
|
|
368
|
+
schema: z.object({
|
|
369
|
+
name: z.string(),
|
|
370
|
+
ingredients: z.array(z.object({ name: z.string(), amount: z.string() })),
|
|
371
|
+
steps: z.array(z.string()),
|
|
372
|
+
}),
|
|
373
|
+
}),
|
|
374
|
+
prompt: 'Generate a lasagna recipe.',
|
|
375
|
+
});
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
This works with all output types that support structured generation:
|
|
379
|
+
|
|
380
|
+
- `Output.object({ name, description, schema })`
|
|
381
|
+
- `Output.array({ name, description, element })`
|
|
382
|
+
- `Output.choice({ name, description, options })`
|
|
383
|
+
- `Output.json({ name, description })`
|
|
384
|
+
|
|
385
|
+
## Accessing Reasoning
|
|
386
|
+
|
|
387
|
+
You can access the reasoning used by the language model to generate the object via the `reasoning` property on the result. This property contains a string with the model's thought process, if available.
|
|
388
|
+
|
|
389
|
+
```ts
|
|
390
|
+
import { generateText, Output } from 'ai';
|
|
391
|
+
__PROVIDER_IMPORT__;
|
|
392
|
+
import { z } from 'zod';
|
|
393
|
+
|
|
394
|
+
const result = await generateText({
|
|
395
|
+
model: __MODEL__, // must be a reasoning model
|
|
396
|
+
output: Output.object({
|
|
397
|
+
schema: z.object({
|
|
398
|
+
recipe: z.object({
|
|
399
|
+
name: z.string(),
|
|
400
|
+
ingredients: z.array(
|
|
401
|
+
z.object({
|
|
402
|
+
name: z.string(),
|
|
403
|
+
amount: z.string(),
|
|
404
|
+
}),
|
|
405
|
+
),
|
|
406
|
+
steps: z.array(z.string()),
|
|
407
|
+
}),
|
|
408
|
+
}),
|
|
409
|
+
}),
|
|
410
|
+
prompt: 'Generate a lasagna recipe.',
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
console.log(result.reasoning);
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
## Error Handling
|
|
417
|
+
|
|
418
|
+
When `generateText` with structured output cannot generate a valid object, it throws a [`AI_NoObjectGeneratedError`](/docs/reference/ai-sdk-errors/ai-no-object-generated-error).
|
|
419
|
+
|
|
420
|
+
This error occurs when the AI provider fails to generate a parsable object that conforms to the schema.
|
|
421
|
+
It can arise due to the following reasons:
|
|
422
|
+
|
|
423
|
+
- The model failed to generate a response.
|
|
424
|
+
- The model generated a response that could not be parsed.
|
|
425
|
+
- The model generated a response that could not be validated against the schema.
|
|
426
|
+
|
|
427
|
+
The error preserves the following information to help you log the issue:
|
|
428
|
+
|
|
429
|
+
- `text`: The text that was generated by the model. This can be the raw text or the tool call text, depending on the object generation mode.
|
|
430
|
+
- `response`: Metadata about the language model response, including response id, timestamp, and model.
|
|
431
|
+
- `usage`: Request token usage.
|
|
432
|
+
- `cause`: The cause of the error (e.g. a JSON parsing error). You can use this for more detailed error handling.
|
|
433
|
+
|
|
434
|
+
```ts
|
|
435
|
+
import { generateText, Output, NoObjectGeneratedError } from 'ai';
|
|
436
|
+
|
|
437
|
+
try {
|
|
438
|
+
await generateText({
|
|
439
|
+
model,
|
|
440
|
+
output: Output.object({ schema }),
|
|
441
|
+
prompt,
|
|
442
|
+
});
|
|
443
|
+
} catch (error) {
|
|
444
|
+
if (NoObjectGeneratedError.isInstance(error)) {
|
|
445
|
+
console.log('NoObjectGeneratedError');
|
|
446
|
+
console.log('Cause:', error.cause);
|
|
447
|
+
console.log('Text:', error.text);
|
|
448
|
+
console.log('Response:', error.response);
|
|
449
|
+
console.log('Usage:', error.usage);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
## generateObject and streamObject (Legacy)
|
|
455
|
+
|
|
456
|
+
<Note type="warning">
|
|
457
|
+
`generateObject` and `streamObject` are deprecated. Use `generateText` and
|
|
458
|
+
`streamText` with the `output` property instead. The legacy functions will be
|
|
459
|
+
removed in a future major version.
|
|
460
|
+
</Note>
|
|
461
|
+
|
|
462
|
+
The `generateObject` and `streamObject` functions are the legacy way to generate structured data. They work similarly to `generateText` and `streamText` with `Output.object()`, but as standalone functions.
|
|
463
|
+
|
|
464
|
+
### generateObject
|
|
465
|
+
|
|
466
|
+
```ts
|
|
467
|
+
import { generateObject } from 'ai';
|
|
468
|
+
__PROVIDER_IMPORT__;
|
|
469
|
+
import { z } from 'zod';
|
|
470
|
+
|
|
471
|
+
const { object } = await generateObject({
|
|
472
|
+
model: __MODEL__,
|
|
473
|
+
schema: z.object({
|
|
474
|
+
recipe: z.object({
|
|
475
|
+
name: z.string(),
|
|
476
|
+
ingredients: z.array(z.object({ name: z.string(), amount: z.string() })),
|
|
477
|
+
steps: z.array(z.string()),
|
|
478
|
+
}),
|
|
479
|
+
}),
|
|
480
|
+
prompt: 'Generate a lasagna recipe.',
|
|
481
|
+
});
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
### streamObject
|
|
485
|
+
|
|
486
|
+
```ts
|
|
487
|
+
import { streamObject } from 'ai';
|
|
488
|
+
__PROVIDER_IMPORT__;
|
|
489
|
+
import { z } from 'zod';
|
|
490
|
+
|
|
491
|
+
const { partialObjectStream } = streamObject({
|
|
492
|
+
model: __MODEL__,
|
|
493
|
+
schema: z.object({
|
|
494
|
+
recipe: z.object({
|
|
495
|
+
name: z.string(),
|
|
496
|
+
ingredients: z.array(z.object({ name: z.string(), amount: z.string() })),
|
|
497
|
+
steps: z.array(z.string()),
|
|
498
|
+
}),
|
|
499
|
+
}),
|
|
500
|
+
prompt: 'Generate a lasagna recipe.',
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
for await (const partialObject of partialObjectStream) {
|
|
504
|
+
console.log(partialObject);
|
|
505
|
+
}
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
### Schema Name and Description (Legacy)
|
|
509
|
+
|
|
510
|
+
You can optionally specify a name and description for the schema. These are used by some providers for additional LLM guidance, e.g. via tool or schema name.
|
|
511
|
+
|
|
512
|
+
```ts highlight="4-5"
|
|
513
|
+
import { generateObject } from 'ai';
|
|
514
|
+
__PROVIDER_IMPORT__;
|
|
515
|
+
import { z } from 'zod';
|
|
516
|
+
|
|
517
|
+
const { object } = await generateObject({
|
|
518
|
+
model: __MODEL__,
|
|
519
|
+
schemaName: 'Recipe',
|
|
520
|
+
schemaDescription: 'A recipe for a dish.',
|
|
521
|
+
schema: z.object({
|
|
522
|
+
name: z.string(),
|
|
523
|
+
ingredients: z.array(z.object({ name: z.string(), amount: z.string() })),
|
|
524
|
+
steps: z.array(z.string()),
|
|
525
|
+
}),
|
|
526
|
+
prompt: 'Generate a lasagna recipe.',
|
|
527
|
+
});
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
### Output Strategy (Legacy)
|
|
531
|
+
|
|
532
|
+
The legacy functions support different output strategies via the `output` parameter:
|
|
533
|
+
|
|
534
|
+
#### Array
|
|
535
|
+
|
|
536
|
+
Generate an array of objects. The schema specifies the shape of an array element.
|
|
537
|
+
|
|
538
|
+
```ts highlight="7"
|
|
539
|
+
import { streamObject } from 'ai';
|
|
540
|
+
__PROVIDER_IMPORT__;
|
|
541
|
+
import { z } from 'zod';
|
|
542
|
+
|
|
543
|
+
const { elementStream } = streamObject({
|
|
544
|
+
model: __MODEL__,
|
|
545
|
+
output: 'array',
|
|
546
|
+
schema: z.object({
|
|
547
|
+
name: z.string(),
|
|
548
|
+
class: z
|
|
549
|
+
.string()
|
|
550
|
+
.describe('Character class, e.g. warrior, mage, or thief.'),
|
|
551
|
+
description: z.string(),
|
|
552
|
+
}),
|
|
553
|
+
prompt: 'Generate 3 hero descriptions for a fantasy role playing game.',
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
for await (const hero of elementStream) {
|
|
557
|
+
console.log(hero);
|
|
558
|
+
}
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
#### Enum
|
|
562
|
+
|
|
563
|
+
Generate a specific enum value for classification tasks.
|
|
564
|
+
|
|
565
|
+
```ts highlight="5-6"
|
|
566
|
+
import { generateObject } from 'ai';
|
|
567
|
+
__PROVIDER_IMPORT__;
|
|
568
|
+
|
|
569
|
+
const { object } = await generateObject({
|
|
570
|
+
model: __MODEL__,
|
|
571
|
+
output: 'enum',
|
|
572
|
+
enum: ['action', 'comedy', 'drama', 'horror', 'sci-fi'],
|
|
573
|
+
prompt:
|
|
574
|
+
'Classify the genre of this movie plot: ' +
|
|
575
|
+
'"A group of astronauts travel through a wormhole in search of a ' +
|
|
576
|
+
'new habitable planet for humanity."',
|
|
577
|
+
});
|
|
578
|
+
```
|
|
579
|
+
|
|
580
|
+
#### No Schema
|
|
581
|
+
|
|
582
|
+
Generate unstructured JSON without a schema.
|
|
583
|
+
|
|
584
|
+
```ts highlight="6"
|
|
585
|
+
import { generateObject } from 'ai';
|
|
586
|
+
__PROVIDER_IMPORT__;
|
|
587
|
+
|
|
588
|
+
const { object } = await generateObject({
|
|
589
|
+
model: __MODEL__,
|
|
590
|
+
output: 'no-schema',
|
|
591
|
+
prompt: 'Generate a lasagna recipe.',
|
|
592
|
+
});
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
### Repairing Invalid JSON (Legacy)
|
|
596
|
+
|
|
597
|
+
<Note type="warning">
|
|
598
|
+
The `repairText` function is experimental and may change in the future.
|
|
599
|
+
</Note>
|
|
600
|
+
|
|
601
|
+
Sometimes the model will generate invalid or malformed JSON.
|
|
602
|
+
You can use the `repairText` function to attempt to repair the JSON.
|
|
603
|
+
|
|
604
|
+
```ts highlight="7-10"
|
|
605
|
+
import { generateObject } from 'ai';
|
|
606
|
+
|
|
607
|
+
const { object } = await generateObject({
|
|
608
|
+
model,
|
|
609
|
+
schema,
|
|
610
|
+
prompt,
|
|
611
|
+
experimental_repairText: async ({ text, error }) => {
|
|
612
|
+
// example: add a closing brace to the text
|
|
613
|
+
return text + '}';
|
|
614
|
+
},
|
|
615
|
+
});
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
## More Examples
|
|
619
|
+
|
|
620
|
+
You can see `generateObject` and `streamObject` in action using various frameworks in the following examples:
|
|
621
|
+
|
|
622
|
+
### `generateObject`
|
|
623
|
+
|
|
624
|
+
<ExampleLinks
|
|
625
|
+
examples={[
|
|
626
|
+
{
|
|
627
|
+
title: 'Learn to generate objects in Node.js',
|
|
628
|
+
link: '/examples/node/generating-structured-data/generate-object',
|
|
629
|
+
},
|
|
630
|
+
{
|
|
631
|
+
title:
|
|
632
|
+
'Learn to generate objects in Next.js with Route Handlers (AI SDK UI)',
|
|
633
|
+
link: '/examples/next-pages/basics/generating-object',
|
|
634
|
+
},
|
|
635
|
+
{
|
|
636
|
+
title:
|
|
637
|
+
'Learn to generate objects in Next.js with Server Actions (AI SDK RSC)',
|
|
638
|
+
link: '/examples/next-app/basics/generating-object',
|
|
639
|
+
},
|
|
640
|
+
]}
|
|
641
|
+
/>
|
|
642
|
+
|
|
643
|
+
### `streamText` with Output
|
|
644
|
+
|
|
645
|
+
<ExampleLinks
|
|
646
|
+
examples={[
|
|
647
|
+
{
|
|
648
|
+
title: 'Learn to stream objects in Node.js',
|
|
649
|
+
link: '/examples/node/streaming-structured-data/stream-object',
|
|
650
|
+
},
|
|
651
|
+
{
|
|
652
|
+
title:
|
|
653
|
+
'Learn to stream objects in Next.js with Route Handlers (AI SDK UI)',
|
|
654
|
+
link: '/examples/next-pages/basics/streaming-object-generation',
|
|
655
|
+
},
|
|
656
|
+
{
|
|
657
|
+
title:
|
|
658
|
+
'Learn to stream objects in Next.js with Server Actions (AI SDK RSC)',
|
|
659
|
+
link: '/examples/next-app/basics/streaming-object-generation',
|
|
660
|
+
},
|
|
661
|
+
]}
|
|
662
|
+
/>
|