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,397 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Streaming Custom Data
|
|
3
|
+
description: Learn how to stream custom data from the server to the client.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Streaming Custom Data
|
|
7
|
+
|
|
8
|
+
It is often useful to send additional data alongside the model's response.
|
|
9
|
+
For example, you may want to send status information, the message ids after storing them,
|
|
10
|
+
or references to content that the language model is referring to.
|
|
11
|
+
|
|
12
|
+
The AI SDK provides several helpers that allows you to stream additional data to the client
|
|
13
|
+
and attach it to the `UIMessage` parts array:
|
|
14
|
+
|
|
15
|
+
- `createUIMessageStream`: creates a data stream
|
|
16
|
+
- `createUIMessageStreamResponse`: creates a response object that streams data
|
|
17
|
+
- `pipeUIMessageStreamToResponse`: pipes a data stream to a server response object
|
|
18
|
+
|
|
19
|
+
The data is streamed as part of the response stream using Server-Sent Events.
|
|
20
|
+
|
|
21
|
+
## Setting Up Type-Safe Data Streaming
|
|
22
|
+
|
|
23
|
+
First, define your custom message type with data part schemas for type safety:
|
|
24
|
+
|
|
25
|
+
```tsx filename="ai/types.ts"
|
|
26
|
+
import { UIMessage } from 'ai';
|
|
27
|
+
|
|
28
|
+
// Define your custom message type with data part schemas
|
|
29
|
+
export type MyUIMessage = UIMessage<
|
|
30
|
+
never, // metadata type
|
|
31
|
+
{
|
|
32
|
+
weather: {
|
|
33
|
+
city: string;
|
|
34
|
+
weather?: string;
|
|
35
|
+
status: 'loading' | 'success';
|
|
36
|
+
};
|
|
37
|
+
notification: {
|
|
38
|
+
message: string;
|
|
39
|
+
level: 'info' | 'warning' | 'error';
|
|
40
|
+
};
|
|
41
|
+
} // data parts type
|
|
42
|
+
>;
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Streaming Data from the Server
|
|
46
|
+
|
|
47
|
+
In your server-side route handler, you can create a `UIMessageStream` and then pass it to `createUIMessageStreamResponse`:
|
|
48
|
+
|
|
49
|
+
```tsx filename="route.ts"
|
|
50
|
+
import { openai } from '@ai-sdk/openai';
|
|
51
|
+
import {
|
|
52
|
+
createUIMessageStream,
|
|
53
|
+
createUIMessageStreamResponse,
|
|
54
|
+
streamText,
|
|
55
|
+
convertToModelMessages,
|
|
56
|
+
} from 'ai';
|
|
57
|
+
__PROVIDER_IMPORT__;
|
|
58
|
+
import type { MyUIMessage } from '@/ai/types';
|
|
59
|
+
|
|
60
|
+
export async function POST(req: Request) {
|
|
61
|
+
const { messages } = await req.json();
|
|
62
|
+
|
|
63
|
+
const stream = createUIMessageStream<MyUIMessage>({
|
|
64
|
+
execute: ({ writer }) => {
|
|
65
|
+
// 1. Send initial status (transient - won't be added to message history)
|
|
66
|
+
writer.write({
|
|
67
|
+
type: 'data-notification',
|
|
68
|
+
data: { message: 'Processing your request...', level: 'info' },
|
|
69
|
+
transient: true, // This part won't be added to message history
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// 2. Send sources (useful for RAG use cases)
|
|
73
|
+
writer.write({
|
|
74
|
+
type: 'source',
|
|
75
|
+
value: {
|
|
76
|
+
type: 'source',
|
|
77
|
+
sourceType: 'url',
|
|
78
|
+
id: 'source-1',
|
|
79
|
+
url: 'https://weather.com',
|
|
80
|
+
title: 'Weather Data Source',
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// 3. Send data parts with loading state
|
|
85
|
+
writer.write({
|
|
86
|
+
type: 'data-weather',
|
|
87
|
+
id: 'weather-1',
|
|
88
|
+
data: { city: 'San Francisco', status: 'loading' },
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const result = streamText({
|
|
92
|
+
model: __MODEL__,
|
|
93
|
+
messages: await convertToModelMessages(messages),
|
|
94
|
+
onFinish() {
|
|
95
|
+
// 4. Update the same data part (reconciliation)
|
|
96
|
+
writer.write({
|
|
97
|
+
type: 'data-weather',
|
|
98
|
+
id: 'weather-1', // Same ID = update existing part
|
|
99
|
+
data: {
|
|
100
|
+
city: 'San Francisco',
|
|
101
|
+
weather: 'sunny',
|
|
102
|
+
status: 'success',
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// 5. Send completion notification (transient)
|
|
107
|
+
writer.write({
|
|
108
|
+
type: 'data-notification',
|
|
109
|
+
data: { message: 'Request completed', level: 'info' },
|
|
110
|
+
transient: true, // Won't be added to message history
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
writer.merge(result.toUIMessageStream());
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
return createUIMessageStreamResponse({ stream });
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
<Note>
|
|
124
|
+
You can also send stream data from custom backends, e.g. Python / FastAPI,
|
|
125
|
+
using the [UI Message Stream
|
|
126
|
+
Protocol](/docs/ai-sdk-ui/stream-protocol#ui-message-stream-protocol).
|
|
127
|
+
</Note>
|
|
128
|
+
|
|
129
|
+
## Types of Streamable Data
|
|
130
|
+
|
|
131
|
+
### Data Parts (Persistent)
|
|
132
|
+
|
|
133
|
+
Regular data parts are added to the message history and appear in `message.parts`:
|
|
134
|
+
|
|
135
|
+
```tsx
|
|
136
|
+
writer.write({
|
|
137
|
+
type: 'data-weather',
|
|
138
|
+
id: 'weather-1', // Optional: enables reconciliation
|
|
139
|
+
data: { city: 'San Francisco', status: 'loading' },
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Sources
|
|
144
|
+
|
|
145
|
+
Sources are useful for RAG implementations where you want to show which documents or URLs were referenced:
|
|
146
|
+
|
|
147
|
+
```tsx
|
|
148
|
+
writer.write({
|
|
149
|
+
type: 'source',
|
|
150
|
+
value: {
|
|
151
|
+
type: 'source',
|
|
152
|
+
sourceType: 'url',
|
|
153
|
+
id: 'source-1',
|
|
154
|
+
url: 'https://example.com',
|
|
155
|
+
title: 'Example Source',
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Transient Data Parts (Ephemeral)
|
|
161
|
+
|
|
162
|
+
Transient parts are sent to the client but not added to the message history. They are only accessible via the `onData` useChat handler:
|
|
163
|
+
|
|
164
|
+
```tsx
|
|
165
|
+
// server
|
|
166
|
+
writer.write({
|
|
167
|
+
type: 'data-notification',
|
|
168
|
+
data: { message: 'Processing...', level: 'info' },
|
|
169
|
+
transient: true, // Won't be added to message history
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// client
|
|
173
|
+
const [notification, setNotification] = useState();
|
|
174
|
+
|
|
175
|
+
const { messages } = useChat({
|
|
176
|
+
onData: ({ data, type }) => {
|
|
177
|
+
if (type === 'data-notification') {
|
|
178
|
+
setNotification({ message: data.message, level: data.level });
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Data Part Reconciliation
|
|
185
|
+
|
|
186
|
+
When you write to a data part with the same ID, the client automatically reconciles and updates that part. This enables powerful dynamic experiences like:
|
|
187
|
+
|
|
188
|
+
- **Collaborative artifacts** - Update code, documents, or designs in real-time
|
|
189
|
+
- **Progressive data loading** - Show loading states that transform into final results
|
|
190
|
+
- **Live status updates** - Update progress bars, counters, or status indicators
|
|
191
|
+
- **Interactive components** - Build UI elements that evolve based on user interaction
|
|
192
|
+
|
|
193
|
+
The reconciliation happens automatically - simply use the same `id` when writing to the stream.
|
|
194
|
+
|
|
195
|
+
## Processing Data on the Client
|
|
196
|
+
|
|
197
|
+
### Using the onData Callback
|
|
198
|
+
|
|
199
|
+
The `onData` callback is essential for handling streaming data, especially transient parts:
|
|
200
|
+
|
|
201
|
+
```tsx filename="page.tsx"
|
|
202
|
+
import { useChat } from '@ai-sdk/react';
|
|
203
|
+
import type { MyUIMessage } from '@/ai/types';
|
|
204
|
+
|
|
205
|
+
const { messages } = useChat<MyUIMessage>({
|
|
206
|
+
api: '/api/chat',
|
|
207
|
+
onData: dataPart => {
|
|
208
|
+
// Handle all data parts as they arrive (including transient parts)
|
|
209
|
+
console.log('Received data part:', dataPart);
|
|
210
|
+
|
|
211
|
+
// Handle different data part types
|
|
212
|
+
if (dataPart.type === 'data-weather') {
|
|
213
|
+
console.log('Weather update:', dataPart.data);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Handle transient notifications (ONLY available here, not in message.parts)
|
|
217
|
+
if (dataPart.type === 'data-notification') {
|
|
218
|
+
showToast(dataPart.data.message, dataPart.data.level);
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
**Important:** Transient data parts are **only** available through the `onData` callback. They will not appear in the `message.parts` array since they're not added to message history.
|
|
225
|
+
|
|
226
|
+
### Rendering Persistent Data Parts
|
|
227
|
+
|
|
228
|
+
You can filter and render data parts from the message parts array:
|
|
229
|
+
|
|
230
|
+
```tsx filename="page.tsx"
|
|
231
|
+
const result = (
|
|
232
|
+
<>
|
|
233
|
+
{messages?.map(message => (
|
|
234
|
+
<div key={message.id}>
|
|
235
|
+
{/* Render weather data parts */}
|
|
236
|
+
{message.parts
|
|
237
|
+
.filter(part => part.type === 'data-weather')
|
|
238
|
+
.map((part, index) => (
|
|
239
|
+
<div key={index} className="weather-widget">
|
|
240
|
+
{part.data.status === 'loading' ? (
|
|
241
|
+
<>Getting weather for {part.data.city}...</>
|
|
242
|
+
) : (
|
|
243
|
+
<>
|
|
244
|
+
Weather in {part.data.city}: {part.data.weather}
|
|
245
|
+
</>
|
|
246
|
+
)}
|
|
247
|
+
</div>
|
|
248
|
+
))}
|
|
249
|
+
|
|
250
|
+
{/* Render text content */}
|
|
251
|
+
{message.parts
|
|
252
|
+
.filter(part => part.type === 'text')
|
|
253
|
+
.map((part, index) => (
|
|
254
|
+
<div key={index}>{part.text}</div>
|
|
255
|
+
))}
|
|
256
|
+
|
|
257
|
+
{/* Render sources */}
|
|
258
|
+
{message.parts
|
|
259
|
+
.filter(part => part.type === 'source')
|
|
260
|
+
.map((part, index) => (
|
|
261
|
+
<div key={index} className="source">
|
|
262
|
+
Source: <a href={part.url}>{part.title}</a>
|
|
263
|
+
</div>
|
|
264
|
+
))}
|
|
265
|
+
</div>
|
|
266
|
+
))}
|
|
267
|
+
</>
|
|
268
|
+
);
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### Complete Example
|
|
272
|
+
|
|
273
|
+
```tsx filename="page.tsx"
|
|
274
|
+
'use client';
|
|
275
|
+
|
|
276
|
+
import { useChat } from '@ai-sdk/react';
|
|
277
|
+
import { useState } from 'react';
|
|
278
|
+
import type { MyUIMessage } from '@/ai/types';
|
|
279
|
+
|
|
280
|
+
export default function Chat() {
|
|
281
|
+
const [input, setInput] = useState('');
|
|
282
|
+
|
|
283
|
+
const { messages, sendMessage } = useChat<MyUIMessage>({
|
|
284
|
+
api: '/api/chat',
|
|
285
|
+
onData: dataPart => {
|
|
286
|
+
// Handle transient notifications
|
|
287
|
+
if (dataPart.type === 'data-notification') {
|
|
288
|
+
console.log('Notification:', dataPart.data.message);
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
const handleSubmit = (e: React.FormEvent) => {
|
|
294
|
+
e.preventDefault();
|
|
295
|
+
sendMessage({ text: input });
|
|
296
|
+
setInput('');
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
return (
|
|
300
|
+
<>
|
|
301
|
+
{messages?.map(message => (
|
|
302
|
+
<div key={message.id}>
|
|
303
|
+
{message.role === 'user' ? 'User: ' : 'AI: '}
|
|
304
|
+
|
|
305
|
+
{/* Render weather data */}
|
|
306
|
+
{message.parts
|
|
307
|
+
.filter(part => part.type === 'data-weather')
|
|
308
|
+
.map((part, index) => (
|
|
309
|
+
<span key={index} className="weather-update">
|
|
310
|
+
{part.data.status === 'loading' ? (
|
|
311
|
+
<>Getting weather for {part.data.city}...</>
|
|
312
|
+
) : (
|
|
313
|
+
<>
|
|
314
|
+
Weather in {part.data.city}: {part.data.weather}
|
|
315
|
+
</>
|
|
316
|
+
)}
|
|
317
|
+
</span>
|
|
318
|
+
))}
|
|
319
|
+
|
|
320
|
+
{/* Render text content */}
|
|
321
|
+
{message.parts
|
|
322
|
+
.filter(part => part.type === 'text')
|
|
323
|
+
.map((part, index) => (
|
|
324
|
+
<div key={index}>{part.text}</div>
|
|
325
|
+
))}
|
|
326
|
+
</div>
|
|
327
|
+
))}
|
|
328
|
+
|
|
329
|
+
<form onSubmit={handleSubmit}>
|
|
330
|
+
<input
|
|
331
|
+
value={input}
|
|
332
|
+
onChange={e => setInput(e.target.value)}
|
|
333
|
+
placeholder="Ask about the weather..."
|
|
334
|
+
/>
|
|
335
|
+
<button type="submit">Send</button>
|
|
336
|
+
</form>
|
|
337
|
+
</>
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
## Use Cases
|
|
343
|
+
|
|
344
|
+
- **RAG Applications** - Stream sources and retrieved documents
|
|
345
|
+
- **Real-time Status** - Show loading states and progress updates
|
|
346
|
+
- **Collaborative Tools** - Stream live updates to shared artifacts
|
|
347
|
+
- **Analytics** - Send usage data without cluttering message history
|
|
348
|
+
- **Notifications** - Display temporary alerts and status messages
|
|
349
|
+
|
|
350
|
+
## Message Metadata vs Data Parts
|
|
351
|
+
|
|
352
|
+
Both [message metadata](/docs/ai-sdk-ui/message-metadata) and data parts allow you to send additional information alongside messages, but they serve different purposes:
|
|
353
|
+
|
|
354
|
+
### Message Metadata
|
|
355
|
+
|
|
356
|
+
Message metadata is best for **message-level information** that describes the message as a whole:
|
|
357
|
+
|
|
358
|
+
- Attached at the message level via `message.metadata`
|
|
359
|
+
- Sent using the `messageMetadata` callback in `toUIMessageStreamResponse`
|
|
360
|
+
- Ideal for: timestamps, model info, token usage, user context
|
|
361
|
+
- Type-safe with custom metadata types
|
|
362
|
+
|
|
363
|
+
```ts
|
|
364
|
+
// Server: Send metadata about the message
|
|
365
|
+
return result.toUIMessageStreamResponse({
|
|
366
|
+
messageMetadata: ({ part }) => {
|
|
367
|
+
if (part.type === 'finish') {
|
|
368
|
+
return {
|
|
369
|
+
model: part.response.modelId,
|
|
370
|
+
totalTokens: part.totalUsage.totalTokens,
|
|
371
|
+
createdAt: Date.now(),
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
},
|
|
375
|
+
});
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
### Data Parts
|
|
379
|
+
|
|
380
|
+
Data parts are best for streaming **dynamic arbitrary data**:
|
|
381
|
+
|
|
382
|
+
- Added to the message parts array via `message.parts`
|
|
383
|
+
- Streamed using `createUIMessageStream` and `writer.write()`
|
|
384
|
+
- Can be reconciled/updated using the same ID
|
|
385
|
+
- Support transient parts that don't persist
|
|
386
|
+
- Ideal for: dynamic content, loading states, interactive components
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
// Server: Stream data as part of message content
|
|
390
|
+
writer.write({
|
|
391
|
+
type: 'data-weather',
|
|
392
|
+
id: 'weather-1',
|
|
393
|
+
data: { city: 'San Francisco', status: 'loading' },
|
|
394
|
+
});
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
For more details on message metadata, see the [Message Metadata documentation](/docs/ai-sdk-ui/message-metadata).
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Error Handling
|
|
3
|
+
description: Learn how to handle errors in the AI SDK UI
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Error Handling and warnings
|
|
7
|
+
|
|
8
|
+
## Warnings
|
|
9
|
+
|
|
10
|
+
The AI SDK shows warnings when something might not work as expected.
|
|
11
|
+
These warnings help you fix problems before they cause errors.
|
|
12
|
+
|
|
13
|
+
### When Warnings Appear
|
|
14
|
+
|
|
15
|
+
Warnings are shown in the browser console when:
|
|
16
|
+
|
|
17
|
+
- **Unsupported features**: You use a feature or setting that is not supported by the AI model (e.g., certain options or parameters).
|
|
18
|
+
- **Compatibility warnings**: A feature is used in a compatibility mode, which might work differently or less optimally than intended.
|
|
19
|
+
- **Other warnings**: The AI model reports another type of issue, such as general problems or advisory messages.
|
|
20
|
+
|
|
21
|
+
### Warning Messages
|
|
22
|
+
|
|
23
|
+
All warnings start with "AI SDK Warning:" so you can easily find them. For example:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
AI SDK Warning: The feature "temperature" is not supported by this model
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Turning Off Warnings
|
|
30
|
+
|
|
31
|
+
By default, warnings are shown in the console. You can control this behavior:
|
|
32
|
+
|
|
33
|
+
#### Turn Off All Warnings
|
|
34
|
+
|
|
35
|
+
Set a global variable to turn off warnings completely:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
globalThis.AI_SDK_LOG_WARNINGS = false;
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
#### Custom Warning Handler
|
|
42
|
+
|
|
43
|
+
You can also provide your own function to handle warnings.
|
|
44
|
+
It receives provider id, model id, and a list of warnings.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
globalThis.AI_SDK_LOG_WARNINGS = ({ warnings, provider, model }) => {
|
|
48
|
+
// Handle warnings your own way
|
|
49
|
+
};
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Error Handling
|
|
53
|
+
|
|
54
|
+
### Error Helper Object
|
|
55
|
+
|
|
56
|
+
Each AI SDK UI hook also returns an [error](/docs/reference/ai-sdk-ui/use-chat#error) object that you can use to render the error in your UI.
|
|
57
|
+
You can use the error object to show an error message, disable the submit button, or show a retry button.
|
|
58
|
+
|
|
59
|
+
<Note>
|
|
60
|
+
We recommend showing a generic error message to the user, such as "Something
|
|
61
|
+
went wrong." This is a good practice to avoid leaking information from the
|
|
62
|
+
server.
|
|
63
|
+
</Note>
|
|
64
|
+
|
|
65
|
+
```tsx file="app/page.tsx" highlight="7,18-25,31"
|
|
66
|
+
'use client';
|
|
67
|
+
|
|
68
|
+
import { useChat } from '@ai-sdk/react';
|
|
69
|
+
import { useState } from 'react';
|
|
70
|
+
|
|
71
|
+
export default function Chat() {
|
|
72
|
+
const [input, setInput] = useState('');
|
|
73
|
+
const { messages, sendMessage, error, regenerate } = useChat();
|
|
74
|
+
|
|
75
|
+
const handleSubmit = (e: React.FormEvent) => {
|
|
76
|
+
e.preventDefault();
|
|
77
|
+
sendMessage({ text: input });
|
|
78
|
+
setInput('');
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<div>
|
|
83
|
+
{messages.map(m => (
|
|
84
|
+
<div key={m.id}>
|
|
85
|
+
{m.role}:{' '}
|
|
86
|
+
{m.parts
|
|
87
|
+
.filter(part => part.type === 'text')
|
|
88
|
+
.map(part => part.text)
|
|
89
|
+
.join('')}
|
|
90
|
+
</div>
|
|
91
|
+
))}
|
|
92
|
+
|
|
93
|
+
{error && (
|
|
94
|
+
<>
|
|
95
|
+
<div>An error occurred.</div>
|
|
96
|
+
<button type="button" onClick={() => regenerate()}>
|
|
97
|
+
Retry
|
|
98
|
+
</button>
|
|
99
|
+
</>
|
|
100
|
+
)}
|
|
101
|
+
|
|
102
|
+
<form onSubmit={handleSubmit}>
|
|
103
|
+
<input
|
|
104
|
+
value={input}
|
|
105
|
+
onChange={e => setInput(e.target.value)}
|
|
106
|
+
disabled={error != null}
|
|
107
|
+
/>
|
|
108
|
+
</form>
|
|
109
|
+
</div>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### Alternative: replace last message
|
|
115
|
+
|
|
116
|
+
Alternatively you can write a custom submit handler that replaces the last message when an error is present.
|
|
117
|
+
|
|
118
|
+
```tsx file="app/page.tsx" highlight="17-23,35"
|
|
119
|
+
'use client';
|
|
120
|
+
|
|
121
|
+
import { useChat } from '@ai-sdk/react';
|
|
122
|
+
import { useState } from 'react';
|
|
123
|
+
|
|
124
|
+
export default function Chat() {
|
|
125
|
+
const [input, setInput] = useState('');
|
|
126
|
+
const { sendMessage, error, messages, setMessages } = useChat();
|
|
127
|
+
|
|
128
|
+
function customSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
129
|
+
event.preventDefault();
|
|
130
|
+
|
|
131
|
+
if (error != null) {
|
|
132
|
+
setMessages(messages.slice(0, -1)); // remove last message
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
sendMessage({ text: input });
|
|
136
|
+
setInput('');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<div>
|
|
141
|
+
{messages.map(m => (
|
|
142
|
+
<div key={m.id}>
|
|
143
|
+
{m.role}:{' '}
|
|
144
|
+
{m.parts
|
|
145
|
+
.filter(part => part.type === 'text')
|
|
146
|
+
.map(part => part.text)
|
|
147
|
+
.join('')}
|
|
148
|
+
</div>
|
|
149
|
+
))}
|
|
150
|
+
|
|
151
|
+
{error && <div>An error occurred.</div>}
|
|
152
|
+
|
|
153
|
+
<form onSubmit={customSubmit}>
|
|
154
|
+
<input value={input} onChange={e => setInput(e.target.value)} />
|
|
155
|
+
</form>
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### Error Handling Callback
|
|
162
|
+
|
|
163
|
+
Errors can be processed by passing an [`onError`](/docs/reference/ai-sdk-ui/use-chat#on-error) callback function as an option to the [`useChat`](/docs/reference/ai-sdk-ui/use-chat) or [`useCompletion`](/docs/reference/ai-sdk-ui/use-completion) hooks.
|
|
164
|
+
The callback function receives an error object as an argument.
|
|
165
|
+
|
|
166
|
+
```tsx file="app/page.tsx" highlight="6-9"
|
|
167
|
+
import { useChat } from '@ai-sdk/react';
|
|
168
|
+
|
|
169
|
+
export default function Page() {
|
|
170
|
+
const {
|
|
171
|
+
/* ... */
|
|
172
|
+
} = useChat({
|
|
173
|
+
// handle error:
|
|
174
|
+
onError: error => {
|
|
175
|
+
console.error(error);
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Injecting Errors for Testing
|
|
182
|
+
|
|
183
|
+
You might want to create errors for testing.
|
|
184
|
+
You can easily do so by throwing an error in your route handler:
|
|
185
|
+
|
|
186
|
+
```ts file="app/api/chat/route.ts"
|
|
187
|
+
export async function POST(req: Request) {
|
|
188
|
+
throw new Error('This is a test error');
|
|
189
|
+
}
|
|
190
|
+
```
|